71

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?

Community
  • 1
  • 1
Danoram
  • 8,132
  • 12
  • 51
  • 71
  • Have you installed Python 3.5 on your computer? You're right in passing the `--python` flag, so something else about your environment or invocation is wrong here. Can you give the error message? – Cody Piersall Jul 25 '17 at 03:54
  • Yesterday I tried running `conda install python=3.5` inside of my activated virtual environment thinking that this would change the python dist inside my env but instead it changed the local version. I changed back to 3.6 but now I run into [this problem](https://stackoverflow.com/questions/42190984/dyld-library-not-loaded-error-preventing-virtualenv-from-loading) but the answer there does not work for me. oh god what did I do – Danoram Jul 25 '17 at 03:59
  • @CodyPiersall It seems that installing `miniconda` changed the path var to `/Users/.../miniconda3/bin:` which is incompatible with the python version `virtualenv` was using. Removing `/Users/.../miniconda3/bin` from the path fixes the issue but then I can't use `conda`... I know this is now a separate issue but any ideas how I can add it back to the path without messing up virtualenv? – Danoram Jul 25 '17 at 04:10
  • 2
    I just want to give an hint for those using Windows(and using Powershell). In this case make sure to explicitly write the .exe, that is what I mean: virtualenv -p C:\PythonVersionFolder\Python.exe EnvName. Otherwise you could get the PermissionError: [WinError 5] denied Access – Carmine Tambascia Sep 28 '17 at 16:10
  • 1
    Possible duplicate of [Use different Python version with virtualenv](https://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv) – Trevor Boyd Smith Aug 29 '18 at 14:37

8 Answers8

65

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 
nehem
  • 12,775
  • 6
  • 58
  • 84
  • 10
    this just creates a virtual environment with `python3.6`. I want `python3.5` – Danoram Jul 25 '17 at 04:06
  • 3
    You can easily find a way to uninstall `python3.6` and install `python3.5`. If you do have to keep multiple versions of python on the same machine, use tools like `pyenv` or `pythonbrew` – nehem Jul 25 '17 at 04:12
  • 2
    hey @itsneo thanks for the suggestions! Reading further into your comment would I be right in understanding that when you create a virtual env that `virtualenv` uses the local python version (2.x/3.x) specified in the `$PATH` and that there is no way to change the specific python version through the `virtualenv` command itself? – Danoram Jul 25 '17 at 04:20
  • 3
    You can still create one directly mentioning the python executable path like this `virtualenv -p /usr/local/bin/python35 env_name` – nehem Jul 25 '17 at 04:22
  • Complementing @nehem 's comment, which worked perfectly for me: to find out the python path, see [here](https://stackoverflow.com/a/2589722) – J0ANMM Jun 24 '21 at 14:53
  • You could also use your specific python identifier's name e.g. `virtualenv -p python3.9 venv` – Ari Jul 18 '21 at 09:45
15

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

Mariano Anaya
  • 1,246
  • 10
  • 11
  • 1
    nice. yeah I ended up creating the venv with `conda`. Huge relief to have it all working now – Danoram Jul 25 '17 at 09:52
  • If all your Python versions are simply called `python`, you can still refer to each one separately if you know the full path. So `/opt/srv/whatever/path/to/python345/bin/python -m venv test345` would create a virtual environment for Python 3.4.5 if you have it installed in that particular path. – tripleee May 17 '21 at 06:00
8

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:

Thomas BDX
  • 2,632
  • 2
  • 27
  • 31
7

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

Viktor
  • 237
  • 1
  • 2
4

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.

Bruce
  • 121
  • 3
0

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.

0

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.

0

Using anaconda we can create a virtual environment called "py35_env" with Python 3.5 version by running:

conda create --name py35_env python=3.5
George Pipis
  • 1,452
  • 16
  • 12