Determine if Python is running inside virtualenv
-
18-09-2019 - |
Question
Is it possible to determine if the current script is running inside a virtualenv environment?
Solution
AFAIK the most reliable way to check for this (and the way that is used internally in virtualenv and in pip) is to check for the existence of sys.real_prefix
:
import sys
if hasattr(sys, 'real_prefix'):
#...
Inside a virtualenv, sys.prefix
points to the virtualenv directory, and sys.real_prefix
points to the "real" prefix of the system Python (often /usr
or /usr/local
or some such).
Outside a virtualenv, sys.real_prefix
should not exist.
Using the VIRTUAL_ENV
environment variable is not reliable. It is set by the virtualenv activate
shell script, but a virtualenv can be used without activation by directly running an executable from the virtualenv's bin/
(or Scripts
) directory, in which case $VIRTUAL_ENV
will not be set.
OTHER TIPS
Try using pip -V
(notice capital V)
If you are running the virtual env. it'll show the path to the env.'s location.
This is an improvement of the accepted answer by Carl Meyer. It works with virtualenv for Python 3 and 2 and also for the venv module in Python 3:
import sys
def is_venv():
return (hasattr(sys, 'real_prefix') or
(hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))
The check for sys.real_prefix
covers virtualenv, the equality of non-empty sys.base_prefix
with sys.prefix
covers venv.
Consider a script that uses the function like this:
if is_venv():
print('inside virtualenv or venv')
else:
print('outside virtualenv or venv')
And the following invocation:
$ python2 test.py
outside virtualenv or venv
$ python3 test.py
outside virtualenv or venv
$ python2 -m virtualenv virtualenv2
...
$ . virtualenv2/bin/activate
(virtualenv2) $ python test.py
inside virtualenv or venv
(virtualenv2) $ deactivate
$ python3 -m virtualenv virtualenv3
...
$ . virtualenv3/bin/activate
(virtualenv3) $ python test.py
inside virtualenv or venv
(virtualenv3) $ deactivate
$ python3 -m venv venv3
$ . venv3/bin/activate
(venv3) $ python test.py
inside virtualenv or venv
(venv3) $ deactivate
Check the $VIRTUAL_ENV
environment variable.
The $VIRTUAL_ENV
environment variable contains the virtual environment's directory when in an active virtual environment.
>>> import os
>>> os.environ['VIRTUAL_ENV']
'/some/path/project/venv'
Once you run deactivate
/ leave the virtual environment, the $VIRTUAL_ENV
variable will be cleared/empty. Python will raise a KeyError
because the environment variable was unset.
>>> import os
>>> os.environ['VIRTUAL_ENV']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py", line 678, in __getitem__
raise KeyError(key) from None
KeyError: 'VIRTUAL_ENV'
These same environment variable checks can of course also be done outside of the Python script, in the shell.
According to the virtualenv pep at http://www.python.org/dev/peps/pep-0405/#specification you can just use sys.prefix instead os.environ['VIRTUAL_ENV'].
the sys.real_prefix does not exist in my virtualenv and same with sys.base_prefix.
You can do which python
and see if its pointing to the one in virtual env.
To check whether your inside Virtualenv:
import os
if os.getenv('VIRTUAL_ENV'):
print('Using Virtualenv')
else:
print('Not using Virtualenv')
You can also get more data on your environment:
import sys
import os
print(f'Python Executable: {sys.executable}')
print(f'Python Version: {sys.version}')
print(f'Virtualenv: {os.getenv("VIRTUAL_ENV")}')
I routinely use several Anaconda-installed virtual environments (venv). This code snippet/examples enables you to determine whether or not you are in a venv (or your system environment), and to also require a specific venv for your script.
ADD TO PYTHON SCRIPT (code snippet):
# ----------------------------------------------------------------------------
# Want script to run in Python 3.5 (has required installed OpenCV, imutils, ... packages):
import os
# First, see if we are in a conda venv { py27: Python 2.7 | py35: Python 3.5 | tf: TensorFlow | thee : Theano }
try:
os.environ["CONDA_DEFAULT_ENV"]
except KeyError:
print("\tPlease set the py35 { p3 | Python 3.5 } environment!\n")
exit()
# If we are in a conda venv, require the p3 venv:
if os.environ['CONDA_DEFAULT_ENV'] != "py35":
print("\tPlease set the py35 { p3 | Python 3.5 } environment!\n")
exit()
# See also:
# Python: Determine if running inside virtualenv
# http://stackoverflow.com/questions/1871549/python-determine-if-running-inside-virtualenv
# [ ... SNIP! ... ]
RUN YOUR SCRIPT (examples):
$ python webcam_cv3_v2_fps_v2c.py -n50
Please set the py35 { p3 | Python 3.5 } environment!
$ thee
[Theano in Anaconda Python 2.7 venv (source activate theano-env)]
(theano-env) $ python webcam_cv3_v2_fps_v2c.py -n50
Please set the py35 { p3 | Python 3.5 } environment!
(theano-env) $ tf
[TensorFlow in Anaconda Python 2.7 venv (source activate tf-env]
(tf-env) $ python webcam_cv3_v2_fps_v2c.py -n50
Please set the py35 { p3 | Python 3.5 } environment!
(tf-env) $ p2
[Anaconda Python 2.7 venv (source activate py27)]
(py27) $ python webcam_cv3_v2_fps_v2c.py -n50
Please set the py35 { p3 | Python 3.5 } environment!
(py27) $ p3
[Anaconda Python 3.5 venv (source activate py35)]
(py35) $ python webcam_cv3_v2_fps_v2c.py -n50
current env: py35
processing (live): found 2 faces and 4 eyes in this frame
threaded OpenCV implementation
num_frames: 50
webcam -- approx. FPS: 18.59
Found 2 faces and 4 eyes!
(py35) $ sd
[Anaconda venv deactivate (source deactivate)]
$ python webcam_cv3_v2_fps_v2c.py -n50
Please set the py35 { p3 | Python 3.5 } environment!
$ ## Q.E.D. ;-)
Update: use in bash scripts:
You can also use this approach in bash scripts (e.g., those that must run in a specific virtual environment). Example (added to bash script):
# ----------------------------------------------------------------------------
# Excerpt from: /mnt/Vancouver/Programming/scripts/tf_tb_del.sh ## tf_tb_del: tf_tensorboard_delete
# [bash script run on command-line: calls TensorFlow-related commands, therefore must be run in tf-env venv]
if [ $CONDA_DEFAULT_ENV ] ## << note the spaces (important in bash)!
then
printf '\n\tvenv: tf-env\n'
else
printf '\n\n\t*******************************************************************\n'
printf '\t*** NOTE! Must run this script in tf-env virtual environment! ***\n'
printf '\t*******************************************************************'
exit
fi
## [ ... snip ... ]
Easiest way is to just run: which python
, if you are in a virtualenv it will point to its python instead of the global one
(edited) I found that way, what do you think of it ? (it also returns the venv base path and works even for readthedocs where checking the env variable does not):
import os
import sys
from distutils.sysconfig import get_config_vars
def get_venv_basedir():
"""Returns the base directory of the virtualenv, useful to read configuration and plugins"""
exec_prefix = get_config_vars()['exec_prefix']
if hasattr(sys, 'real_prefix') is False or exec_prefix.startswith(sys.real_prefix):
raise EnvironmentError('You must be in a virtual environment')
return os.path.abspath(get_config_vars()['exec_prefix'] + '/../')
It's not bullet-proof but for UNIX environments simple test like
if run("which python3").find("venv") == -1:
# something when not executed from venv
works great for me. It's simpler then testing existing of some attribute and, anyway, you should name your venv directory venv
.
In windows OS you see something like this:
C:\Users\yourusername\virtualEnvName\Scripts>activate
(virtualEnvName) C:\Users\yourusername\virtualEnvName\Scripts>
Parentheses mean that you are actually in the virtual environment called "virtualEnvName".
A potential solution is:
os.access(sys.executable, os.W_OK)
In my case I really just wanted to detect if I could install items with pip as is. While it might not be the right solution for all cases, consider simply checking if you have write permissions for the location of the Python executable.
Note: this works in all versions of Python, but also returns True
if you run the system Python with sudo
. Here's a potential use case:
import os, sys
can_install_pip_packages = os.access(sys.executable, os.W_OK)
if can_install_pip_packages:
import pip
pip.main(['install', 'mypackage'])
This is an old question, but too many examples above are over-complicated.
Keep It Simple: (in Jupyter Notebook or Python 3.7.1 terminal on Windows 10)
import sys
print(sys.executable)```
# example output: >> `C:\Anaconda3\envs\quantecon\python.exe`
OR
```sys.base_prefix```
# Example output: >> 'C:\\Anaconda3\\envs\\quantecon'
There are a lot of great methods posted here already, but just adding one more:
import site
site.getsitepackages()
tells you where pip
installed the packages.