Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get virtualenv's bin folder path from script

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?

like image 713
José Tomás Tocino Avatar asked Feb 25 '14 03:02

José Tomás Tocino


People also ask

How do I get Virtualenv path?

To install virtualenv, just use pip install virtualenv . To create a virtual environment directory with it, type virtualenv /path/to/directory . Activating and deactivating the virtual environment works the same way as it does for virtual environments in Python 3 (see above).

How do I see all virtual environments in Python?

To see a list of the Python virtual environments that you have created, you can use the 'conda env list' command. This command will give you the names as well as the filesystem paths for the location of your virtual environments.


2 Answers

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

echo $VIRTUAL_ENV 
like image 186
Brad Culberson Avatar answered Oct 29 '22 16:10

Brad Culberson


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) 
like image 28
Laurent LAPORTE Avatar answered Oct 29 '22 16:10

Laurent LAPORTE