How to List all virtual environments in Python

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
2 min

banner

# Table of Contents

  1. How to List all virtual environments in Python
  2. List all virtual environments created by Conda
  3. List all virtual environments created by virtualenvwrapper

# How to List all virtual environments in Python

If you use the native Python venv command to create virtual environments or the virtualenv package, you can list the virtual environments by running a find command that looks for Python installations.

For example, if you are on Linux, you can use the following command.

shell
# For Linux locate -b '\activate' | grep "/home"

If you are on macOS, you can use the following command.

shell
# For macOS find $HOME -name "*activate" -type f

The following command works on both Linux and macOS.

shell
# For macOS and Linux find ~ -d -name "site-packages" 2>/dev/null

The command looks for directories under $HOME that are named site-packages.

The venv module places its pip-installed modules in a folder named site-packages.

On macOS and Linux, you can also use the following command which finds all symlinks to the Python binary.

shell
# For macOS and Linux find ~ -type l -name python

If you are on macOS, you can also use the mdfind command.

shell
# For macOS mdfind -name activate | egrep /bin/activate$| xargs -o egrep -l nondestructive 2>/dev/null | xargs -L 1 dirname | xargs -L 1 dirname

# List all virtual environments created by Conda

If you need to list all virtual environments that were created using conda, use the conda info --envs command.

shell
conda info --envs

list all virtual environments created by anaconda

You can also use the conda env list command.

shell
conda env list

using conda env list command

Both commands list all virtual environments created by conda.

# List all virtual environments created by virtualenvwrapper

If you need to list all virtual environments that were created by the virtualenvwrapper module, use the lsvirtualenv command.

shell
lsvirtualenv -b

The -b flag stands for brief mode and disables the verbose output.

If you want to run the command in long mode, use the -l flag.

shell
lsvirtualenv -l

Note that the command only lists the virtual environments that were created by virtualenvwrapper (with the mkvirtualenv command).

The environment variables that were created using the mkvirtualenv command are usually all stored in ~/.virtualenvs by default.

In order to use the lsvirtualenv, you have to have the virtualenvwrapper module installed as shown in this section of the docs.

You can also use the workon command without passing it any arguments if you use the virtualenvwrapper module.

shell
workon

The workon command is used to list or change the working virtual environments.

If no environment name is given, the command prints the available virtual environments to stdout.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.