How to specify python version used to create Virtual Environment?

Viewed 105861

My Python virtual environments use python3.6 when I create them using virtualenv

~ $ virtualenv my_env

but I need to use python3.5 as 3.6 is not currently supported by Opencv3.

I've tried using the --python=<py_version> flag when creating a virtual environment but this doesn't work.

How do I specify the python (3.x) version to install using virtualenv for Mac and/or Linux?

7 Answers

As of version 3.3, python includes a package named venv. However that package doesn't provide the same functionalities as the traditional virtualenv package.

venv allows creating virtual environments only for the version of python it's installed for. virtualenv allows creating virtual environments for different versions of python by providing the path to the binary.

Creating virtual envs for different versions of python:

So assuming one has python 2.7 and python 3.6 installed in /path/to/ and wants to create the virtual env named respectively env-py36 with python 3.6 and env-py27 with python 2.7

# create a virtual env with python3's venv :
/path/to/python36/bin/python3 -m venv /my/python-venvs/env-py36
. /my/python-venvs/env-py36/bin/activate
# we're now running python 3's "env-py36" virtual env, we want to install the "virtualenv" package
pip install virtualenv
deactivate
# now use virtualenv to create a virtual environment for python 2.7
/my/python-venvs/env-py36/bin/virtualenv --python=/path/to/python27/bin/python /my/python-venvs/env-py27

Using python 3.3+ venv

Python 3.3+ :

/path/to/python3/bin/python3 -m venv ENV_DIR

Python 3.3 to 3.5 (deprecated in 3.6+) :

/path/to/python3/bin/pyvenv ENV_DIR

Sources:

I had this issue (and came here) but under Windows. Python 3.9 was installed on one system but it had issues with code developed under 3.7. I wanted to use a virtual environment to downgrade to 3.7 to help debug the issue. Using Python Launcher for Windows:

py -3.7 -m venv my_env

in the python project folder did the trick for me.

Simple and direct solution: Just see this video (https://www.youtube.com/watch?v=hC9FBQnOv6o) and follow the python setup download instructions of a particular python version and then use virtualenv <folder_name> -p /python.exe
This command is also shown in the video too.

In Linux:

Suppose you have python 3.8 (or higher) installed on the system, but for a specific task, you need python 3.7 (or lower). The best idea is (not to downgrade) to Create a virtual environment with python 3.7(or any 3.x, change the commands below according to your desired version. Below is an implementation of a virtual environment with python 3.7)

Steps:

  1. Install python 3.7 and it’s virtual environment packages.

    sudo apt-get install python3.7-dev python3.7-venv

  2. Find out where your python 3.7 is located by this command:

    which python3.7 (Should be something like /usr/bin/python3.7)

  3. Create Virtual Environment in the Home directory.

    cd

    mkdir virtual_env

    /usr/bin/python3.7 -m venv ~/virtual_env/venv_with_python3.7

    source ~/virtual_env/venv_with_python3.7/bin/activate

  4. python --version (Should be python 3.7 now)

  5. Done. Python 3.7 can be used in this virtual environment. Type which python, you’ll see you have created python 3.7 in a virtual environment, rather than in the system globally.

    Run deactivate when you need to deactivate.

Related