How to Install Another Version of Python to Virtualenv

Is it possible to install another version of Python to Virtualenv?

Here are the options for virtualenv

$ virtualenv
You must provide a DEST_DIR
Usage: virtualenv [OPTIONS] DEST_DIR

Options:
--version show program's version number and exit.
-h, --help show this help message and exit.
-v, --verbose Increase verbosity.
-q, --quiet Decrease verbosity.
-p PYTHON_EXE, --python=PYTHON_EXE
The Python interpreter to use, e.g.,
--python=python2.5 will use the python2.5 interpreter
to create the new environment. The default is the
interpreter that virtualenv was installed with
(/usr/bin/python)
--clear Clear out the non-root install and start from scratch
--no-site-packages Don't give access to the global site-packages dir to
the virtual environment
--unzip-setuptools Unzip Setuptools or Distribute when installing it
--relocatable Make an EXISTING virtualenv environment relocatable.
This fixes up scripts and makes all .pth files
relative
--distribute Use Distribute instead of Setuptools. Set environ
variable VIRTUALENV_USE_DISTRIBUTE to make it the
default
--prompt==PROMPT Provides an alternative prompt prefix for this
environment

1) What you want to do is install python to a directory that you are able to write too.

You can follow the instructions here.

For Python 2.7.1
Python source

mkdir ~/src
mkdir ~/.localpython
cd ~/src
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tgz
tar -zxvf Python-2.7.1.tgz
cd Python-2.7.1

make clean
./configure --prefix=/home/${USER}/.localpython
make
make install

2) Install virtualenv
virtualenv source

cd ~/src
wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.5.2.tar.gz#md5=fbcefbd8520bb64bc24a560c6019a73c
tar -zxvf virtualenv-1.5.2.tar.gz
cd virtualenv-1.5.2/
~/.localpython/bin/python setup.py install

3) Create a virtualenv using your local python
virtualenv docs

mkdir /home/${USER}/virtualenvs
cd /home/${USER}/virtualenvs
~/.localpython/bin/virtualenv py2.7 --python=/home/${USER}/.localpython/bin/python2.7

4) Activate the environment

cd ~/virtualenvs/py2.7/bin
source ./activate

5) Check

(py2.7)$ python
Python 2.7.1 (r271:86832, Mar 31 2011, 15:31:37)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

(py2.7)$ deactivate
$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Use different Python version with virtualenv

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/"

How do I install a previous version of Python into a virtualenv?

First and foremost, it most certainly is. If we take a look at virtualenv --help, we will see that we have the option to specify the python executable using the -p flag. However, your problem is slightly different since you already have python3 linked to your python executable. In another question is is talked about. However, it isn't a very good anwer since it involves making, and then symlinking a new python installation.

Instead, it is better to use a python version manage live pyenv or pythonz. I myself prefer pyenv. If you are on a *nix machine, then simply follow the instructions outlined here.

Once this installation is complete, you should see that you get a few instructions towards the end of the installation.

# Load pyenv automatically by adding
# the following to ~/.zshrc:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Simply at this to the end of your .bashrc or .zshrc. If you're using windows, then this process will be a lot harder. There is a pip installer. However, please note that it is not fully stable yet.

Once all of this is done, all you need to do is install the version of python that you want.

pyenv install 2.7.10

Once this is done, run pyenv rehash, then run pyenv global <version you installed> (which maybe different from 2.7.10).

Now you can go ahead just create a virtual environment with virtualenv, and it should have the version of python you want.

how to create a venv with a different python version

You can have multiple python versions installed at the same time and you can create virtual environments with the needed version. Make sure you have installed the python version you need and then specify its location when you create the virtual environment:

virtualenv -p <path-to-new-python-installation> <new-venv-name>

Example:

virtualenv -p  C:\Users\ssharma\AppData\Local\Programs\Python\Python38\python.exe venv38

This will create a virtual environment called venv38 with Python 3.8.

How to create virtual environments with different Python versions

With python3.8:- virtualenv -p python3.8 env_name
replace python version like python3.9, python3.7..

How to specify python version used to create Virtual Environment?

Assuming that you have installed python3 or any desired version of Python (2.6, 2.7, 3.5, 3.6), Now while creating the virtual environment directly pass the python executable path. Hence here are few valid example

$ virtualenv new_p2_env # Creates a new default python environment (usually python 2)

$ virtualenv -p python3 new_p3_env # Creates a new default python3 (python3 must be a valid command i.e found in the PATH)

And last

# Directly point to any version of python binary, this can be even another virtualenv's bin/python. 
$ virtualenv -p /path/to/any/bin/python new_env

How to change the python version of already existing virtualenv?

EDIT 1

Did some testing and found another more "graceful" way to (at least) update the executable. Let's assume the virtual env was initially created like so
virtualenv -p /path/to/my/python2.7 .venv. The executable can be updated to a specific python version like so: virtualenv --clear -p /path/to/my/python3.6 .venv. Please validate the python symlink in .venv/bin/python is updated using ls -la .venv/bin/python. The old executable(s) will still be in ./venv/bin/.

Note: You need to have the specific target version of python installed.


See this link which explains it well.

Virtualenvwrapper comes with some convenient commands for managing your virtualenvs.

To change your Python version:

  1. Deactivate your current environment session.

  2. If you have many packages or libraries installed, it would be a good
    idea to make a requirements.txt file. Remember to edit version as
    necessary.

  3. Remove the virtualenv with the wrapper command: rmvirtualenv

    • This will remove the virtualenv, but leave your project files.
  4. Make a new virtualenv with the Python version you want.

    • Example: mkvirtualenv -p python3 env-name

    • You can specify the Python version with the -p flag and version. If
      you have a requirements.txt file, you can specify that with -r
      requirements.txt

  5. Now bind your new virtualenv to your project directory. You can
    specify the full paths, but it is easier to have your new virtualenv
    activated and be in your project directory. Then, run the command:


Example: setvirtualenvproject

Please let me/us know if this answer was helpful to you!

Install different versions of python 2

There are different ways of creating virtual python environments. Three popular ones are

  • virtualenv
  • pipenv
  • conda

I personally like conda a lot.

virtualenv

Assuming you have pip installed, you get virtualenv with

pip install virtualenv

Once installed, you can change into a directory of your choice and create a virtual environment like this

virtualenv myenvironmentname 

If you want to use a different python version in your virtual environment, you can specify this with the --python flag.

virtualenv --python=/usr/bin/python2.6 myenvironmentname

However, please note that this requires you to have the python version you specify installed in advance, virtualenv will not take care of that for you (have a look at Use different Python version with virtualenv for more details). So you'll need local installations of the versions you desire.

You then can activate the environment with

myenvironmentname/bin/activate

and go ahead to use pip to install packages, etc. Have a look at

pip freeze --help

to find out on how to make your environment reusable.

pipenv

pipenv combines pip and virtualenv.

You can install it using

pip install --user pipenv

Pipenv takes care of dependencies on a project basis

cd myprojectfolder
pipenv install

This will create a Pipfile which will track dependencies and a virtualenv (see https://docs.python-guide.org/dev/virtualenvs/ for more details).

To create an environment using a specific version, you can do

pipenv install --python '/usr/bin/python2.6'

or

pipenv install --python 2.6

Cmp. Set python version when creating virtualenv using pipenv. If you also have pyenv installed, the second form will prompt pipenv to attempt to install non-existing versions, afaik.

conda

Anaconda Python is a python distribution (with a focus on data science) that comes with its own package and virtual environment manager named conda. Anaconda Python is not available in the official package repository of Ubuntu 18.04 LTS but needs to be installed in another way (the official documentation can be found here: https://docs.anaconda.com/anaconda/install/linux/).

To create an environment with conda, do

conda create --name myenvironmentname python=2.7.15 

In contrast to virtualenv, the environments are by default not created in the present working directory, but installed into the envs directory in your conda directory. conda will also take care to install the proper python version, that is at least as long as it is part of the default channel (see below).

You can then activate said environment with

conda activate myenvironmentname

As I wrote above, the python version you specify needs to be available from the configured conda channels. python2.6 however, was removed from the default channel. To remedy this, you can add the free channel back to your default list (see https://docs.conda.io/projects/conda/en/latest/user-guide/configuration/free-channel.html for more details):

conda config --set restore_free_channel true

After that you can

conda create --name myotherenvironmentname python=2.6

And switch between the environments as you like

conda activate myotherenvironmentname


Related Topics



Leave a reply



Submit