Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list all the virtual environments created with venv?

Someone's just asked me how to list all the virtual environments created with venv.

I could only think of searching for pyvenv.cfg files to find them. Something like:

from pathlib import Path

venv_list = [str(p.parent) for p in Path.home().rglob('pyvenv.cfg')]

This could potentially include some false positives. Is there a better way to list all the virtual environment created with venv?

NB: The question is about venv specifically, NOT Anaconda, virtualenv, etc.

like image 910
Jacques Gaudin Avatar asked Mar 26 '20 18:03

Jacques Gaudin


People also ask

How do I see all virtual environments in Python?

To see a list of the Python virtual environments that you have created, you can use the 'conda env list' command. This command will give you the names as well as the filesystem paths for the location of your virtual environments.

How do I get a list of venv?

You can use the lsvirtualenv , in which you have two options "long" or "brief": "long" option is the default one, it searches for any hook you may have around this command and executes it, which takes more time. "brief" just take the virtualenvs names and prints it.

Where can I find virtual environment name?

The name of the current virtual environment will now appear on the left of the prompt (e.g. (venv)Your-Computer:project_folder UserName$ ) to let you know that it's active. From now on, any package that you install using pip will be placed in the venv folder, isolated from the global Python installation.

Where are my virtual environments stored?

The virtual environment tool creates a folder inside the project directory. By default, the folder is called venv , but you can give it a custom name too. It keeps Python and pip executable files inside the virtual environment folder.


1 Answers

On Linux/macOS this should get most of it

find ~ -d -name "site-packages" 2>/dev/null

Looking for directories under your home that are named "site-packages" which is where venv puts its pip-installed stuff. the /dev/null bit cuts down on the chattiness of things you don't have permission to look into.

Or you can look at the specifics of a particular expected file. For example, activate has nondestructive as content. Then you need to look for a pattern than matches venv but not anaconda and the rest.

find ~ -type f -name "activate" -exec egrep -l nondestructive /dev/null {} \; 2>/dev/null

like image 102
JL Peyret Avatar answered Sep 30 '22 04:09

JL Peyret