138๐
โ
The path to the virtual env is in the environment variable VIRTUAL_ENV
echo $VIRTUAL_ENV
๐คBrad Culberson
24๐
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)
๐คLaurent LAPORTE
- [Django]-Django: How to get related objects of a queryset?
- [Django]-URL-parameters and logic in Django class-based views (TemplateView)
- [Django]-Adding new custom permissions in Django
6๐
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
๐คLyle
- [Django]-How can I subtract or add 100 years to a datetime field in the database in Django?
- [Django]-How to save pillow image object to Django ImageField?
- [Django]-Add a non-model field on a ModelSerializer in DRF 3
0๐
You can use fabric to do such things from python
>>> from fabric.api import local
>>> local('which celery')
๐คglmvrml
- [Django]-Where's my JSON data in my incoming Django request?
- [Django]-Best practices for adding .gitignore file for Python projects?
- [Django]-Django migration with uuid field generates duplicated values
Source:stackexchange.com