Create a virtualenv with both python2 and python3

2019-04-04 06:17发布

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.

4条回答
甜甜的少女心
2楼-- · 2019-04-04 06:55

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-04-04 07:07

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
查看更多
Summer. ? 凉城
4楼-- · 2019-04-04 07:11

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 
查看更多
做个烂人
5楼-- · 2019-04-04 07:13

I have a solution for this involving Vagrant/VirtualBox... (it has my bootstrap setup for starting a django probject, but fork it and go wild with it!)

the package is here, https://github.com/andrewyoung1991/python-3.4.1-vagrant-bootstrap.git a virtual-env is unfortunately a single-python game but with a VirtualBox you can work freely in a sandbox calling python2 or python3

查看更多
登录 后发表回答