The Python Oracle

Use different Python version with virtualenv

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Secret Catacombs

--

Chapters
00:00 Question
00:18 Accepted answer (Score 1918)
00:49 Answer 2 (Score 519)
01:34 Answer 3 (Score 218)
02:26 Answer 4 (Score 185)
03:17 Thank you

--

Full question
https://stackoverflow.com/questions/1534...

Accepted answer links:
[answer]: https://stackoverflow.com/a/39713544/145...

Answer 2 links:
[documentation]: https://docs.python.org/3/library/venv.h...
[virtualenv]: https://pypi.python.org/pypi/virtualenv
[pyvenv]: https://docs.python.org/3/library/venv.h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #virtualenv #virtualenvwrapper

#avk47



ACCEPTED ANSWER

Score 2025


NOTE: For Python 3.3+, see The Aelfinn's answer below.


Use the --python (or short -p) option when creating a virtualenv instance to specify the Python executable you want to use, e.g.:

virtualenv --python="/usr/bin/python2.6" "/path/to/new/virtualenv/"



ANSWER 2

Score 712


Since Python 3.3, the documentation suggests creating the virtual environment using stdlib:

python3 -m venv "my_env_name"

Also, if we want a particular version of python, lets say 3.6, then we can use

python3.6 -m venv "my_env_name"

Make sure to install the referenced version of Python along with your existing system Python.




ANSWER 3

Score 230


These are the steps you can follow when you are on a shared hosting environment and need to install & compile Python from source and then create venv from your Python version. For Python 2.7.9. you would do something along these lines:

mkdir ~/src
wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -zxvf Python-2.7.9.tgz
cd Python-2.7.9
mkdir ~/.localpython
./configure --prefix=$HOME/.localpython
make
make install

virtual env

cd ~/src
wget https://pypi.python.org/packages/5c/79/5dae7494b9f5ed061cff9a8ab8d6e1f02db352f3facf907d9eb614fb80e9/virtualenv-15.0.2.tar.gz#md5=0ed59863994daf1292827ffdbba80a63
tar -zxvf virtualenv-15.0.2.tar.gz
cd virtualenv-15.0.2/
~/.localpython/bin/python setup.py install
virtualenv ve -p $HOME/.localpython/bin/python2.7
source ve/bin/activate   

Naturally, this can be applicable to any situation where you want to replicate the exact environment you work and deploy on.




ANSWER 4

Score 116


virtualenv --python=/usr/bin/python2.6 <path/to/myvirtualenv>