Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify python version used to create Virtual Environment?

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?

like image 858
Danoram Avatar asked Jul 25 '17 03:07

Danoram


People also ask

How do I use a specific version of Python?

As a standard, it is recommended to use the python3 command or python3. 7 to select a specific version. The py.exe launcher will automatically select the most recent version of Python you've installed. You can also use commands like py -3.7 to select a particular version, or py --list to see which versions can be used.


2 Answers

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  
like image 88
nehem Avatar answered Sep 29 '22 10:09

nehem


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

like image 38
Mariano Anaya Avatar answered Sep 29 '22 10:09

Mariano Anaya