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

我怕爱的太早我们不能终老 提交于 2020-05-05 18:25:44

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/60873454/how-can-i-list-all-the-virtual-environments-created-with-venv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!