873

Using virtualenv, I run my projects with the default version of Python (2.7). On one project, I need to use Python 3.4.

I used brew install python3 to install it on my Mac. Now, how do I create a virtualenv that uses the new version?

e.g. sudo virtualenv envPython3

If I try:

virtualenv -p python3 test

I get:

Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.4.0_1/Frameworks/Python.framework/Versions/3.4'
New python executable in test/bin/python3.4
Also creating executable in test/bin/python
Failed to import the site module
Traceback (most recent call last):
  File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/site.py", line 67, in <module>
    import os
  File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/os.py", line 634, in <module>
    from _collections_abc import MutableMapping
ImportError: No module named '_collections_abc'
ERROR: The executable test/bin/python3.4 is not functioning
ERROR: It thinks sys.prefix is '/Users/user/Documents/workspace/test' (should be '/Users/user/Documents/workspace/test/test')
ERROR: virtualenv is not compatible with this system or executable
Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
Prometheus
  • 32,405
  • 54
  • 166
  • 302
  • 2
    did you try only updating the interpreter in your current environment or did you just decide to start from scratch? – Charlie Parker Aug 28 '16 at 21:37
  • Is it possible to only install python3 in virtual environment without updating system python (keeping in python2.x) on mac? – user Mar 16 '20 at 16:31
  • @User Yes. You can have any number of separate Python installations (the only thing special about the system-installed one is that it is pre-installed with the operating system), and `virtualenv` doesn't care about *how* any particular installation was installed. – chepner Dec 22 '22 at 13:45

23 Answers23

1493

simply run

virtualenv -p python3 envname

Update after OP's edit:

There was a bug in the OP's version of virtualenv, as described here. The problem was fixed by running:

pip install --upgrade virtualenv
tbrisker
  • 15,518
  • 1
  • 16
  • 17
  • 25
    The virtualenv upgrade worked, but for some reason I also had to be explicit about python3 executable with `virtualenv -p $(which python3) envname` – dkamins Nov 07 '14 at 22:15
  • 2
    is it possible to just change the current environment's python interpreter rather than creating a new environment? – Charlie Parker Aug 28 '16 at 21:36
  • On Raspbian Jessie 'virtualenv --no-site-packages python3.4' worked. --distribute is deprecated and -p doesn't seem necessary since virtualenv uses python3. – alkopop79 Nov 10 '17 at 13:04
  • 1
    Why is it that after running `virtualenv -p python3 my_virtual_env`, `python3` is accessible from outside the virtual environment also? – Bishwas Mishra Apr 17 '18 at 09:57
  • @CharlieParker You can perform the following two steps in sequence: 1. Delete the `bin`, `lib`, `include`, `local` and `share` directories in your project's root directory. 2. In terminal run: `virtualenv -p python3 .` which initializes a new Python3 virtualenv in the current directory. – Joshua T Jan 06 '19 at 16:24
  • For windows I use: `py -3.7 -m venv .` – Omar Gonzales Jan 04 '20 at 17:18
  • 2
    After that you need to use `cd envname` and run `source ./bin/activate` - this will run virtual environment. Use `deactivate` in order to exit from this virtual environment. – Amaimersion Mar 12 '20 at 12:21
293

Python 3 has a built-in support for virtual environments - venv. It might be better to use that instead. Referring to the docs:

Creation of virtual environments is done by executing the pyvenv script:

pyvenv /path/to/new/virtual/environment

Update for Python 3.6 and newer:

As pawciobiel correctly comments, pyvenv is deprecated as of Python 3.6 and the new way is:

python3 -m venv /path/to/new/virtual/environment
Community
  • 1
  • 1
geckon
  • 8,316
  • 4
  • 35
  • 59
  • 2
    i hope you are correct since i just left virtualenv to work with pyenv along with python 3.5.2 – Jayesh Sep 27 '16 at 19:40
  • 1
    I have not found any information confirming the location related concerns, relocatable was removed as an option.... is it now unnecessary? is their a workaround? or is it just now completely useless to prepare an app? – J. M. Becker Sep 27 '16 at 21:33
  • 7
    Exactly but you could also run: `python3 -m venv /path/v3` since pyvenv script has been deprecated as of Python 3.6. – pawciobiel Nov 19 '16 at 00:37
  • 5
    using the latest anaconda python 3.6 on mac, and `python3 -m venv /path/v3` errors here. `Error: Command '['/path/v3/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit` Am I missing something? With the `--without-pip` option, it at least creates the venv with activate/deactivate scripts correctly. – monkut Mar 06 '17 at 02:15
  • you're right, but it's more of a config problem I think.. not really a programming question. – monkut Mar 06 '17 at 09:34
  • 1
    "venv is available by default in Python 3.3 and later, and installs pip and setuptools into created virtual environments in Python 3.4 and later." https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments Just commenting that it is available before the mentioned 3.6 version. – Sn3akyP3t3 Dec 10 '17 at 18:32
63

I'v tried pyenv and it's very handy for switching python versions (global, local in folder or in the virtualenv):

brew install pyenv

then install Python version you want:

pyenv install 3.5.0

and simply create virtualenv with path to needed interpreter version:

virtualenv -p /Users/johnny/.pyenv/versions/3.5.0/bin/python3.5 myenv

That's it, check the version:

. ./myenv/bin/activate && python -V

There are also plugin for pyenv pyenv-virtualenv but it didn't work for me somehow.

Johnner
  • 3,545
  • 1
  • 23
  • 23
  • it seems that your method requires to create a new python environment. Can pyenv work with virtualenv and simple change my python version whenever I want? – Charlie Parker Aug 28 '16 at 22:17
60

Install prerequisites.

sudo apt-get install python3 python3-pip virtualenvwrapper

Create a Python3 based virtual environment. Optionally enable --system-site-packages flag.

mkvirtualenv -p /usr/bin/python3 <venv-name>

Set into the virtual environment.

workon <venv-name>

Install other requirements using pip package manager.

pip install -r requirements.txt
pip install <package_name>

When working on multiple python projects simultaneously it is usually recommended to install common packages like pdbpp globally and then reuse them in virtualenvs.

Using this technique saves a lot of time spent on fetching packages and installing them, apart from consuming minimal disk space and network bandwidth.

sudo -H pip3 -v install pdbpp
mkvirtualenv -p $(which python3) --system-site-packages <venv-name>

Django specific instructions

If there are a lot of system wide python packages then it is recommended to not use --system-site-packages flag especially during development since I have noticed that it slows down Django startup a lot. I presume Django environment initialisation is manually scanning and appending all site packages from the system path which might be the reason. Even python manage.py shell becomes very slow.

Having said that experiment which option works better. Might be safe to just skip --system-site-packages flag for Django projects.

Sandeep
  • 28,307
  • 3
  • 32
  • 24
  • 1
    I applied this to OSX, it worked (except didn't use `apt-get` but `brew`) –  Oct 31 '17 at 05:43
43
virtualenv --python=/usr/bin/python3 <name of env>

worked for me.

bewithaman
  • 768
  • 8
  • 16
  • I tried that but is said `The executable python3 (from --python=python3) does not exist`, what would you do? I wanted to brew isntall python3 but was unsure if that was a good idea or if my system would get confused of which python version I am using – Charlie Parker Aug 28 '16 at 21:47
36

This is all you need, in order to run a virtual environment in python / python3

First if virtualenv not installed, run

pip3 install virtualenv 

Now Run:

virtualenv -p python3 <env name> # you can specify full path instead <env_name> to install the files in a different location other than the current location

Sometime the cmd virtualenv fails, if so use this:

python3 -m virtualenv <env_name>  # you can specify full path instead <env_name> to install the files in a different location other than the current location

Now activate the virtual env:

source <env_name>/bin/activate

Or:

source `pwd`/<env_name>/bin/activate

Now run

which python

You should see the full path to your dir and <env_name>/bin/python suffix

To exit the virtualenv, run:

deactivate 

To troubleshoot Python location got to here

Kohn1001
  • 3,507
  • 1
  • 24
  • 26
25

You can specify specific Version of Python while creating environment.
It's mentioned in virtualenv.py

virtualenv --python=python3.5 envname

In some cases this has to be the full path to the executable:

virtualenv --python=/Users/username/.pyenv/versions/3.6.0/bin/python3.6 envname

How -p works

parser.add_option(
    '-p', '--python',
    dest='python',
    metavar='PYTHON_EXE',
    help='The Python interpreter to use, e.g., --python=python3.5 will use the python3.5 '
    'interpreter to create the new environment.  The default is the interpreter that '
    'virtualenv was installed with (%s)' % sys.executable)
clankill3r
  • 9,146
  • 20
  • 70
  • 126
Kiran Jasvanee
  • 6,362
  • 1
  • 36
  • 52
16

I had the same ERROR message. tbrisker's solution did not work in my case. Instead this solved the issue:

$ python3 -m venv .env
Aziz Alto
  • 19,057
  • 5
  • 77
  • 60
9

In addition to the other answers, I recommend checking what instance of virtualenv you are executing:

which virtualenv

If this turns up something in /usr/local/bin, then it is possible - even likely - that you installed virtualenv (possibly using an instance of easy_tools or pip) without using your system's package manager (brew in OP's case). This was my problem.

Years ago - when I was even more ignorant - I had installed virtualenv and it was masking my system's package-provided virtualenv.

After removing this old, broken virtualenv, my problems went away.

sage
  • 4,863
  • 2
  • 44
  • 47
9

The below simple commands can create a virtual env with version 3.5

apt-get install python3-venv

python3.5 -m venv <your env name>

if you want virtual env version as 3.6

python3.6 -m venv <your env name>
Hariharan AR
  • 1,386
  • 11
  • 20
8

Python now comes with its own implementation of virtual environment, by the name of "venv". I would suggest using that, instead of virtualenv.

Quoting from venv - docs,

Deprecated since version 3.6: pyvenv was the recommended tool for creating virtual environments for Python 3.3 and 3.4, and is deprecated in Python 3.6.

Changed in version 3.5: The use of venv is now recommended for creating virtual environments.

For windows, to initiate venv on some project, open cmd:

python -m venv "c:\path\to\myenv"

(Would suggest using double quote around directory path if it contains any spaces. Ex: "C:/My Dox/Spaced Directory/Something")

Once venv is set up, you will see some new folders inside your project directory. One of them would be "Scripts".

To activate or invoke venv you need:

C:\> <venv>\Scripts\activate.bat

You can deactivate a virtual environment by typing “deactivate” in your shell. With this, you are now ready to install your project specific libraries, which will reside under the folder "Lib".

================================ Edit 1 ==================================== The scenario which will be discussed below is not what originally asked, just adding this in case someone use vscode with python extension

In case, you use vs code with its python extension, you might face an issue with its pylint which points to the global installation. In this case, pylint won't be able to see the modules that are installed in your virtual environment and hence will show errors while importing.

Here is a simple method to get past this.

cd Workspace\Scripts
.\Activate.ps1
code .

We are basically activating the environment first and then invoking vs-code so that pylint starts within the environment and can see all local packages.

Blaze
  • 1,642
  • 2
  • 22
  • 20
  • I use `virtualenv` for day-to-day use of virtual environments (`workon` , etc.), but for reliable creation of new virtual environments in `python3.x` this is the only way I found that works. – Bobble Apr 12 '17 at 10:09
6

In python3.6 I tried python3 -m venv myenv, as per the documentation, but it was taking so long. So the very simple and quick command is python -m venv yourenv It worked for me on python3.6.

sujeet
  • 3,480
  • 3
  • 28
  • 60
5

On Mac I had to do the following to get it to work.

mkvirtualenv --python=/usr/bin/python3 YourEnvNameHere
Stryker
  • 5,732
  • 1
  • 57
  • 70
  • To find exactly where your desired python version is location use `which python2.7` or `which python3.5` or `which python3.6`, then replace the about `--python=DIRECTORY` – Santhosh Mar 08 '18 at 08:57
4

If you install python3 (brew install python3) along with virtualenv burrito, you can then do mkvirtualenv -p $(which python3) env_name

Of course, I know virtualenv burrito is just a wrapper, but it has served me well over the years, reducing some learning curves.

Antony
  • 5,414
  • 7
  • 27
  • 32
3

virtualenv --python=/usr/local/bin/python3 <VIRTUAL ENV NAME> this will add python3 path for your virtual enviroment.

Hardik Gajjar
  • 1,024
  • 12
  • 27
2

It worked for me

virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3
Dadaso Zanzane
  • 6,039
  • 1
  • 25
  • 25
2

For those having troubles while working with Anaconda3 (Python 3).

You could use

conda create -n name_of_your_virtualenv python=python_version 

To activate the environment ( Linux, MacOS)

source activate name_of_your_virtualenv

For Windows

activate name_of_your_virtualenv
jkhosla
  • 1,677
  • 1
  • 14
  • 12
1

I tried all the above stuff, it still didn't work. So as a brute force, I just re-installed the anaconda, re-installed the virtualenv... and it worked.

Amans-MacBook-Pro:~ amanmadan$ pip install virtualenv
You are using pip version 6.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting virtualenv
  Downloading virtualenv-15.0.3-py2.py3-none-any.whl (3.5MB)
    100% |████████████████████████████████| 3.5MB 114kB/s 
Installing collected packages: virtualenv
Successfully installed virtualenv-15.0.3
Amans-MacBook-Pro:python amanmadan$ virtualenv my_env
New python executable in /Users/amanmadan/Documents/HadoopStuff/python/my_env/bin/python
Installing setuptools, pip, wheel...done.
Amans-MacBook-Pro:python amanmadan$ 
Aman Madan
  • 93
  • 5
1

I wanted to keep python 2.7.5 as default version on Centos 7 but have python 3.6.1 in a virtual environment running alongside other virtual environments in python 2.x

I found the below link the best solution for the newest python version ( python 3.6.1) https://www.digitalocean.com/community/tutorial_series/how-to-install-and-set-up-a-local-programming-environment-for-python-3. It shows the steps for different platforms but the basic steps are

  1. Install python3.x (if not present) for your platform
  2. Install python3.x-devel for your platform
  3. Create virtual environment in python 3.x (for example $ python3.6 -m venv virenv_test_p3/ )
  4. Activate the testenvironment for python 3.x (for example source virenv_test_p3/bin/activate)
  5. Install the packages which you want to use in your new python 3 virtual environment and which are supported ( for example pip install Django==1.11.2)
1

For those of you who are using pipenv and want to install specific version:

pipenv install --python 3.6
Dodge
  • 3,219
  • 3
  • 19
  • 38
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
1

On Windows command line, the following worked for me. First find out where your python executables are located:

where python

This will output the paths to the different python.exe on your system. Here were mine:

C:\Users\carandangc\Anaconda3\python.exe
C:\Python27\python.exe

So for Python3, this was located in the first path for me, so I cd to the root folder of the application where I want to create a virtual environment folder. Then I run the following which includes the path to my Python3 executable, naming my virtual environment 'venv':

virtualenv --python=/Users/carandangc/Anaconda3/python.exe venv

Next, activate the virtual environment:

call venv\Scripts\activate.bat

Finally, install the dependencies for this virtual environment:

pip install -r requirements.txt

This requirements.txt could be populated manually if you know the libraries/modules needed for your application in the virtual environment. If you had the application running in another environment, then you can automatically produce the dependencies by running the following (cd to the application folder in the environment where it is working):

pip freeze > requirements.txt

Then once you have the requirements.txt that you have 'frozen', then you can install the requirements on another machine or clean environment with the following (after cd to the application folder):

pip install -r requirements.txt

To see your python version in the virtual environment, run:

python --version

Then voila...you have your Python3 running in your virtual environment. Output for me:

Python 3.7.2
Carlo Carandang
  • 187
  • 1
  • 8
1

You can use also "venv" to create virtual environment.

Command: python3 -m venv [environment_name]

Example: python3 -m venv my_env

Activate virtual environment:

For Windows:

Command: [virtual environment name]\Scripts\activate

Example: my_env\Scripts\activate

For Linux:

Command: source [virtual environment name]/bin/activate

Example: source my_env/bin/activate

MD. SHIFULLAH
  • 913
  • 10
  • 16
0

I got the same error due to it being a conflict with miniconda3 install so when you type "which virtualenv" and if you've installed miniconda and it's pointing to that install you can either remove it (if your like me and haven't moved to it yet) or change your environment variable to point to the install you want.