Create a virtualenv with both python2 and python3

22,268

Solution 1

Sorry, virtualenv is designed to support single interpreter version.

If you need to use several python versions on the same codebase please create separate virtual environments.

Solution 2

virtualenv does not support multiple interpreter versions . My suggestion is to use different environment for each of the versions :

virtualenv -p /usr/bin/python3.3 py3env
virtualenv -p /usr/bin/python py2env

Solution 3

virtualenv help you to isolate environments.

It can't support multiple python version in the same time. You can try pyenv and pyenv-virtualenv. It support you change folder to another python version and work environment. It switch version very easily.

If you can't install pyenv and work on Mac. anyenv can help you to install pyenv.

Example:

$ pyenv install 3.4.1
$ pyenv install 2.7.6
$ pyenv virtualenv 3.4.1 mypy3
$ pyenv virtualenv 2.7.6 mypy2
$ pyenv versions
  * system
    3.4.1
    2.7.6
    mypy3

$ cd /work/
$ pyenv local mypy3      # Use Py3 now
$ pyenv local mypy2      # Use Py2 now 
Share:
22,268

Related videos on Youtube

Zweedeend
Author by

Zweedeend

Updated on July 09, 2022

Comments

  • Zweedeend
    Zweedeend almost 2 years

    I tried to use virtualenvwrapper to create a virtualenv with both python2 and python3

    Per virtualenv with python2 and python3 via Homebrew I hoped this would work:

    (The name of the virtualenv is 'double')

    mkvirtualenv double -p `which python`
    mkvirtualenv double -p `which python3`
    

    It mentions that

    Not overwriting existing python script both/bin/python (you must use both/bin/python3.4)
    

    But it that does not seem to be true. Typing python python2.7 python3 and python3.4 all start the python3.4 interpreter.

    • Kevin
      Kevin over 9 years
      What are you trying to accomplish by doing this? A major benefit of virtualenvs is walling off separate worlds for Python 2 and Python 3.
    • Zweedeend
      Zweedeend over 9 years
      I'm working on a project that runs in both python2 and python3. I don't want to switch virtualenv all the time. python2 and python3 have their own version of pip and store site-packages in different places, so I don't see the problem.
    • Simeon Visser
      Simeon Visser over 9 years
      If the purpose is to test the project, use tox instead for running tests in Python 2 and 3. Otherwise I suggest developing it in 3.x and running it in 2 separately. All tools mentioned here were developed with one Python per virtualenv in mind so while things may initially appear to work, there's no guarantee that it'll continue to work.
    • Mikko Ohtamaa
      Mikko Ohtamaa over 9 years
      I am quite sure virtualenv doesn't give this kind of guarantees, as virtualenv by definition is isolated environment for one Python interpreter version. Create one version for each Python interpreter you wish to test on. If you need to run on multiple interpreters, handle this one layer above of virtualenv, like the script (let's call it test.sh) which you use to run the tests.