This page looks best with JavaScript enabled

Activate python virtualenv programmatically

 ·   ·  β˜• 1 min read

    Putting the script into the bin of your virtualenv, and then adding that bin location to your global PATH will not automatically source your virtualenv. You do need to source it first to make it active.

    All that your system knows is to check that extra path for the executable and run it. There isn’t anything in that script indicating a virtualenv.

    You could, however, hardcode the she-bang line to your virtualenv python, in which case the site-packages will end up on the path:

    #!/Users/foo/environments/project/env/bin/python
    

    Or another option is to simply create a tiny bash wrapper that calls your original pythons script, which will allow you to leave your original script with a generic she-bang..

    So if myscript.py is: #!/usr/bin/env python

    Then you can make a myscript :

    #!/bin/bash
    
    /Users/foo/environments/project/env/bin/python myscript.py
    

    When you do myscript, it will explicitly call your python script with the interpreter you set up.

    activate_this = './venv/Scripts/activate_this.py'
    exec(open(activate_this).read())
    

    Ohidur Rahman Bappy
    WRITTEN BY
    Ohidur Rahman Bappy
    πŸ“šLearner 🐍 Developer