Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python 3 in virtualenv

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
like image 751
Prometheus Avatar asked May 24 '14 08:05

Prometheus


People also ask

Does virtualenv work with Python 3?

Virtualenv is only installed on DreamHost servers for Python 2. If you're working with Python 3, you must install virtualenv using pip3. pip3 is not installed on the server by default. You must first install a custom version of Python 3.

How do I install a specific version of Python in virtualenv?

By default, that will be the version of python that is used for any new environment you create. However, you can specify any version of python installed on your computer to use inside a new environment with the -p flag : $ virtualenv -p python3. 2 my_env Running virtualenv with interpreter /usr/local/bin/python3.


3 Answers

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
like image 124
tbrisker Avatar answered Oct 23 '22 17:10

tbrisker


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.

like image 63
Johnner Avatar answered Oct 23 '22 17:10

Johnner


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
like image 288
geckon Avatar answered Oct 23 '22 19:10

geckon