Get virtualenv's bin folder path from script

前端 未结 3 494
情深已故
情深已故 2020-12-25 10:51

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.p         


        
相关标签:
3条回答
  • 2020-12-25 11:26

    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)
    
    0 讨论(0)
  • 2020-12-25 11:34

    You can use fabric to do such things from python

    >>> from fabric.api import local
    >>> local('which celery')
    
    0 讨论(0)
  • 2020-12-25 11:36

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

    echo $VIRTUAL_ENV
    
    0 讨论(0)
提交回复
热议问题