Get virtualenv's bin folder path from script

61,173

Solution 1

The path to the virtual env is in the environment variable VIRTUAL_ENV

echo $VIRTUAL_ENV

Solution 2

The VIRTUAL_ENV environment variable is only available if the virtual environment is activated.

For instance:

$ python3 -m venv myapp
$ source myapp/bin/activate
(myapp) $ python  -c "import os; print(os.environ['VIRTUAL_ENV'])"
/path/to/virtualenv/myapp

If not activated, you have an exception:

(myapp) $ deactivate
$ myapp/bin/python -c "import os; print(os.environ['VIRTUAL_ENV'])"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python3.4/os.py", line 635, in __getitem__
    raise KeyError(key) from None
KeyError: 'VIRTUAL_ENV'

IMO, you should use sys.executable to get the path of your Python executable, and then build the path to celery:

import sys
import os

celery_name = {'linux': 'celery', 'win32': 'celery.exe'}[sys.platform]
celery_path = os.path.join(os.path.dirname(sys.executable), celery_name)

Solution 3

How about referencing sys.prefix? It always outputs a result regardless of a virtualenv is activated or not, and also it's more convenient than getting grand parent position of sys.executable.

$ python -c 'import sys;print(sys.prefix)'
/usr
$ . venv/bin/activate
(venv) $ python -c 'import sys;print(sys.prefix)'
path/to/venv
Share:
61,173
José Tomás Tocino
Author by

José Tomás Tocino

I'm a software engineer from Cádiz, Spain. I love coding! I've been programming for 15+ years and have worked on a wide variety of projects, ranging from military-grade, based naval combat systems, to autonomous anti-piracy engines powered by , to industrial-level automated devices powered by . I have also built videogames, done a lot of web development (esp. using Django) and many more things, most of which you can check right here at my GitHub profile. Fav tech: Check my projects on View my profile at LinkedIn. Read my ramblings on twitter. Check my photography website 📷. View my trips on my motorcycle 🏍.

Updated on July 09, 2022

Comments

  • José Tomás Tocino
    José Tomás Tocino almost 2 years

    I'm using virtualenvwrapper with a django project that has a management task that automatically writes some config files, so the user just has to

    ./manage.py generate_configuration > much_nice.conf
    

    And then move the file elsewhere. One of the generated config files is a task for supervisord that launches a celery worker. The problem I'm getting is that I don't know how to output the path of the celery executable that is within the bin folder of the virtualenv. Essentially, I'd like to have the output of the command

    which celery
    

    One option is using sys.executable, get the folder (which seems to be the bin folder of the virtualenv) and that's it... but I'm not sure.

    Doesn't virtualenv have any kind of method to get the path itself?

  • José Tomás Tocino
    José Tomás Tocino about 10 years
    That's it, thanks. I managed to get the value in python using os.environ['VIRTUAL_ENV'].
  • Tyler Eaves
    Tyler Eaves over 9 years
    This will probably not work. A sub-shell started is not going to have the virtual env on $PATH so which won't work.
  • ForeverWintr
    ForeverWintr over 7 years
    Note that VIRTUAL_ENV is set by the virtualenv's activate script, and it's possible to use the virtualenv python without activating the virtualenv. See: stackoverflow.com/a/1883251/1286571
  • Christian O'Reilly
    Christian O'Reilly over 4 years
    Also, conda do not use this variable. This will beak for users using CONDA or any other ways to manage their python distributions. I don't think this is a robust approach. Using sys.executable as proposed by @laurent-laporte seems more reliable.