How to specify python version used to create Virtual Environment?

PythonPython 3.xVirtualenv

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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 

Solution 2 - Python

Alternatively, I think you could use the specific version of Python itself to create the virtual environment. That way, you'll know for sure it's the correct version:

$ python3.5 -m venv test35
$ ./test35/bin/python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build ) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Reference at https://docs.python.org/3.5/library/venv.html

Solution 3 - Python

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:

Solution 4 - Python

I working on all ubuntu and MacOS

> Ubuntu : virtualenv -p python3.6 environment_file > > Mac OS : virtualenv -p python3.6 environment_file

I think it be same

Solution 5 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanoramView Question on Stackoverflow
Solution 1 - PythonnehemView Answer on Stackoverflow
Solution 2 - PythonMariano AnayaView Answer on Stackoverflow
Solution 3 - PythonThomas B in BDXView Answer on Stackoverflow
Solution 4 - PythonViktorView Answer on Stackoverflow
Solution 5 - PythonBruceView Answer on Stackoverflow