简体   繁体   中英

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.

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

The standard library venv does not keep track of any of the created virtual environments. Therefore, the only possibility to list all of them is to search for your hard drive for folders that meet certain criterion.

The PEP 405 gives quite good listing about what should be included in a folder so that it is a virtual environment. Also this blog post explains part of the virtual environment internals quite well. The definition of a virtual environment is

A Python virtual environment in its simplest form would consist of nothing more than a copy or symlink of the Python binary accompanied by a pyvenv.cfg file and a site-packages directory. (PEP 405)

In summary, you will have to search your hard drive for folders that:

Linux / macOS

  • Has pyvenv.cfg with home key*
  • Has bin/python3 or bin/python
  • Has lib/<python-version>/site-packages/ , where <python-version> is for example python3.3 .
  • Optional: If created with venv , has also bin/activate ( source ). A folder is considered virtual environment even if this would be lacking. ( PEP 405 )

Windows

  • Has pyvenv.cfg with home key*
  • Has Script/python.exe
  • Has lib/<python-version>/site-packages/ , where <python-version> is for example python3.3 .
  • Optional: If created with venv , has also Scripts/activate.bat and Scripts/Activate.ps1 ( source ). A folder is considered virtual environment even if these would be lacking. ( PEP 405 )

* pyvenv.cfg

The pyvenv.cfg can actually be in the same folder or one subfolder above the python executable. The pyvenv.cfg that belongs to a virtual environment must have home = <home> row, where <home> is the directory containing the Python executable used to create the virtual environment. (PEP 405).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM