Use different Python version with virtualenv Use different Python version with virtualenv python python

Use different Python version with virtualenv

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

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

N.B. For Python 3.3 or later, refer to The Aelfinn's answer below.

Since Python 3, the Python Docs suggest creating the virtual environment with the following command:

python3 -m venv <myenvname>

Please note that venv does not permit creating virtual environments with other versions of Python. For that, install and use the virtualenv package.

Obsolete information

The pyvenv script can be used to create a virtual environment

pyvenv /path/to/new/virtual/environment

but it has been deprecated since Python 3.6.

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 ~/srcwget -zxvf Python-2.7.9.tgzcd Python-2.7.9mkdir ~/.localpython./configure --prefix=$HOME/.localpythonmakemake install

virtual env

cd ~/srcwget -zxvf virtualenv-15.0.2.tar.gzcd virtualenv-15.0.2/~/.localpython/bin/python installvirtualenv ve -p $HOME/.localpython/bin/python2.7source ve/bin/activate   

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