Use different Python version with virtualenv
I have a Debian system currently running with python 2.5.4. I got virtualenv properly installed, everything is working fine. Is there a possibility that I can use a virtualenv with a different version of Python?
I compiled Python 2.6.2 and would like to use it with some virtualenv. Is it enough to overwrite the binary file? Or do I have to change something in respect to the libraries?
Just use the --python
(or short -p
) option when creating your virtualenv instance to specify the Python executable you want to use, eg:
virtualenv --python=/usr/bin/python2.6 <path/to/new/virtualenv/>
NB For Python 3.3 or later, refer to The Aelfinn's answer below. [Editor's note: I know this should normally be a comment, not an edit, but a new comment would be hidden, and I just spent 45 minutes untangling errors because this important answer was buried under three or four parrot answers. I'm just trying to save everyone time here.]
These are steps when you are on shared hosting environment and need to install & complie 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.
UPDATE: For Python3.6, the below pyvenv
script is deprecated. Instead, the Python Docs suggest creating the virtual environment with the following command:
python3 -m venv <myenvname>
For python3 (3.3+), use either the above method or the script pyvenv
command.
pyvenv /path/to/new/virtual/environment
Please note that venv
does not permit creating virtualenv with other versions of Python. For that, install and use the virtualenv
package.