Search code examples
pythonpython-2.7python-3.xvirtualenvvirtualenvwrapper

How to force 'mkproject' (virtualenvwrapper) to use python3 as default?


I've added the following lines to my bash, but mkproject keeps creating python 2.7 folders into the virtual env, therefore I still need to use -p python3, which I'd like to not have to do.

export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
export VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
source /usr/local/bin/virtualenvwrapper_lazy.sh

Solution

  • virtualenvwrapper understands the VIRTUALENVWRAPPER_VIRTUALENV environment variable, you need to set it to the virtualenv appropriate to the python version you're using. For example:

    export VIRTUALENVWRAPPER_VIRTUALENV=virtualenv3
    

    This is needed because virtualenvwrapper executes virtualenv as a shell script (without adding python2 or python3 in the front of the command).

    This way the virtualenv script is executed with the interpreter defined in its shebang (#!).

    Most Linux distros provide two packages: virtualenv2 and virtualenv3 each containing one script:

    virtualenv2:

    #!/usr/bin/python2
    import virtualenv
    virtualenv.main()
    

    virtualenv3:

    #!/usr/bin/python3
    import virtualenv
    virtualenv.main()
    

    On a Mac you use brew for the python installation. Therefore there is nothing wrong with copying the virtualenv script into two instances: vritualenv2 and virtualenv3 and change the shebang to the correct python version.

    (You need to install the virtualenv eggs, through pip, for each python version.)