List all virtualenv
Silly question. Found that there's a
lsvirtualenv
command which lists all existing virtualenv.
How to list all python virtual environments in Linux?
If only using the lowly virtualenv ...{directory}
to create a virtualenv, then there is just some directory somewhere that has that specific environment in it. You can only "list" these by running find
on your $HOME
directory (or any other list of directories you might have used to create virtualenvs) looking for python installations. Hopefully some convention was followed like storing them all in ~/virtualenvs
. (See also Where should virtualenvs be created? )
If using virtualenvwrapper
, then as mentioned, use the command lsvirtualenv
to list envs that were created with mkvirtualenv
. They are all in ~/.virtualenvs
by default. See https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html
If using conda
, you can list virtual envs created via conda create --name {my_env} [...]
, using either conda info --envs
or conda env list
. See https://conda.io/docs/using/envs.html#list-all-environments
How can I list all the virtual environments created with venv?
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
macos mdfind
On macos, this is is pretty fast, using mdfind
(locate
on Linux would probably have similar performance.
mdfind -name activate | egrep /bin/activate$| xargs -o egrep -l nondestructive 2>/dev/null | xargs -L 1 dirname | xargs -L 1 dirname
So we :
- look for all activate files
- egrep to match only bin/activate files (
mdfind
matches on things like.../bin/ec2-activate-license
) - look for that
nondestructive
and print filename where there is a match. - the 2
xargs -L 1 dirname
allow us to "climb up" from/bin/activate
to the virtual env's root.
Helper function with -v
flag to show details.
jvenvfindall(){ # search for Python virtual envs. -v for verbose details
unset verbose
OPTIND=1
while getopts 'v' OPTION; do
case "$OPTION" in
v)
verbose=1
;;
?)
;;
esac
done
shift "$(($OPTIND -1))"
local bup=$PWD
for dn in $(mdfind -name activate | egrep /bin/activate$| xargs -o egrep -l nondestructive 2>/dev/null | xargs -L 1 dirname | xargs -L 1 dirname)
do
if [[ -z "$verbose" ]]; then
printf "$dn\n"
else
printf "\n\nvenv info for $dn:\n"
cd $dn
echo space usage, $(du -d 0 -h)
#requires the jq and jc utilities... to extract create and modification times
echo create, mod dttm: $(stat . | jc --stat | jq '.[]|{birth_time, change_time}')
tree -d -L 1 lib
fi
done
cd $bup
}
output:
...
venv info for /Users/me/kds2/issues2/067.pip-stripper/010.fixed.p1.check_venv/venvtest:
space usage, 12M .
create, mod dttm: { "birth_time": "Apr 16 13:04:43 2019", "change_time": "Sep 30 00:00:39 2019" }
lib
└── python3.6
...
Hmmm, disk usage is not that bad, but something similar for node_modules might save some real space.
Is there a way to list all python virtual environments created using the venv module?
No, not in an easy way.
Python virtual environments can be stored anywhere on the disk. And AFAIK they are not indexed, they are truely isolated (after all you can just remove venv directory and be done with it, you don't need to do anything special). So that would require entire disk scan. Which potentially can be done (you can search for all Python executables for example) but is rather painful.
It works with Conda because Conda places venvs in concrete path, e.g. /home/username/miniconda/envs/
.
How can I make a list of installed packages in a certain virtualenv?
Calling pip
command inside a virtualenv should list the packages visible/available in the isolated environment. Make sure to use a recent version of virtualenv that uses option --no-site-packages
by default. This way the purpose of using virtualenv is to create a python environment without access to packages installed in system python.
Next, make sure you use pip
command provided inside the virtualenv (YOUR_ENV/bin/pip
). Or just activate the virtualenv (source YOUR_ENV/bin/activate
) as a convenient way to call the proper commands for python interpreter or pip
~/Projects$ virtualenv --version
1.9.1
~/Projects$ virtualenv -p /usr/bin/python2.7 demoenv2.7
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in demoenv2.7/bin/python2.7
Also creating executable in demoenv2.7/bin/python
Installing setuptools............................done.
Installing pip...............done.
~/Projects$ cd demoenv2.7/
~/Projects/demoenv2.7$ bin/pip freeze
wsgiref==0.1.2
~/Projects/demoenv2.7$ bin/pip install commandlineapp
Downloading/unpacking commandlineapp
Downloading CommandLineApp-3.0.7.tar.gz (142kB): 142kB downloaded
Running setup.py egg_info for package commandlineapp
Installing collected packages: commandlineapp
Running setup.py install for commandlineapp
Successfully installed commandlineapp
Cleaning up...
~/Projects/demoenv2.7$ bin/pip freeze
CommandLineApp==3.0.7
wsgiref==0.1.2
What's strange in my answer is that package 'wsgiref' is visible inside the virtualenv. Its from my system python. Currently I do not know why, but maybe it is different on your system.
how can I find out which python virtual environment I am using?
You can use sys.prefix
to determine which virtualenv you're in.
import sys
print(sys.prefix)
from the sys
docs
A string giving the site-specific directory prefix where the platform independent Python files are installed
Python: How do you check what is in virtualenv?
Once you activate your virtual environment, you should be able to list out packages using pip list
and verify version using python --version
.
Related Topics
Accessing Dict Keys Like an Attribute
How to Delete a File or Folder in Python
Python Script to List Users and Groups
High Kernel CPU When Running Multiple Python Programs
Binding Callbacks to Minimize and Maximize Events in Toplevel Windows
Python Portable, Linux & Windows
Why am I Getting Socket.Gaierror: [Errno -2] from Python Httplib
Update Python on Linux 2.7 to 3.5
Convert a Gtk Python Script to C
Convert Utc Datetime String to Local Datetime
How to Find the Mountpoint a File Resides On
Why Does Simple Echo in Subprocess Not Working
Why Not Just Use 'Shell=True' in Subprocess.Popen in Python
Python Logging - Check Location of Log Files
(Still) Cannot Properly Install Lxml 2.3 for Python, But at Least 2.2.8 Works