virtualenvwrapper and Python 3
Asked Answered
T

9

122

I installed python 3.3.1 on ubuntu lucid and successfully created a virtualenv as below

virtualenv envpy331 --python=/usr/local/bin/python3.3

this created a folder envpy331 on my home dir.

I also have virtualenvwrapper installed.But in the docs only 2.4-2.7 versions of python are supported..Has anyone tried to organize the python3 virtualenv ? If so, can you tell me how ?

Twodimensional answered 20/4, 2013 at 17:57 Comment(1)
The problem with that is that pip is still the python2 pip, isn't it? I think what you want is virtualenvwrapper to use pyvenv instead of virtualenvOshinski
K
27

The latest version of virtualenvwrapper is tested under Python3.2. Chances are good it will work with Python3.3 too.

Kagoshima answered 20/4, 2013 at 18:12 Comment(0)
N
253

If you already have python3 installed as well virtualenvwrapper the only thing you would need to do to use python3 with the virtual environment is creating an environment using:

which python3 #Output: /usr/bin/python3
mkvirtualenv --python=/usr/bin/python3 nameOfEnvironment

Or, (at least on OSX using brew):

mkvirtualenv --python=`which python3` nameOfEnvironment

Start using the environment and you'll see that as soon as you type python you'll start using python3

Nimmons answered 9/6, 2014 at 11:25 Comment(8)
Didn't quite work for me on OSX with 2.7.8 and 3.4.1 installed side-by-side via brew. Modifying it to "mkvirtualenv --python=`which python3` nameOfEnvironment" worked perfectly, though.Rafaelarafaelia
@ChrisDoggett same on Ubuntu 14.10.Cerulean
On Ubuntu the command will be: mkvirtualenv --python=/usr/bin/python3 your-env-nameDanicadanice
If you use bash, you can do substitution to save a line - mkvirtualenv --python=$(which python3) nameOfEnvironmentContrive
what's really great is to create the following bash alias alias mkvirtualenv3="mkvirtualenv --python=$(which python3.6)" - then it's easy to create virtualenvs for python 2 or 3Interweave
If you want to use which python3 or $(which python3) make sure you ARE NOT in another virtualenv -- or you'll get a virtual path to python 3 which isn't really what you want, i.e. ~/.virtualenvs/the_other_env/bin/python3Neri
I'm getting error ImportError: No module named virtualenv. I have python 3.7 installed beside python 2.7.15Congratulatory
@Congratulatory You have to pip install virtualenv, it's not a built in module.Nimmons
K
53

You can make virtualenvwrapper use a custom Python binary instead of the one virtualenvwrapper is run with. To do that you need to use VIRTUALENV_PYTHON variable which is utilized by virtualenv:

$ export VIRTUALENV_PYTHON=/usr/bin/python3
$ mkvirtualenv -a myproject myenv
Running virtualenv with interpreter /usr/bin/python3
New python executable in myenv/bin/python3
Also creating executable in myenv/bin/python
(myenv)$ python
Python 3.2.3 (default, Oct 19 2012, 19:53:16) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Keffiyeh answered 23/8, 2013 at 15:45 Comment(3)
This is effectively out of date. Rather than setting an environment variable you should use the --python parameter that is currently suggested by Jonathan.Mantelet
@Rawrgulmuffins Why? This prevents having to specify the python version for each new env.Limon
@Limon That's a fair point. I personally feel like that's a downside but I can see why people would prefer that.Mantelet
K
27

The latest version of virtualenvwrapper is tested under Python3.2. Chances are good it will work with Python3.3 too.

Kagoshima answered 20/4, 2013 at 18:12 Comment(0)
R
23

On Ubuntu; using mkvirtualenv -p python3 env_name loads the virtualenv with python3.

Inside the env, use python --version to verify.

Rosaniline answered 23/5, 2017 at 8:40 Comment(1)
python --version to display the versionRiannon
K
22

virtualenvwrapper now lets you specify the python executable without the path.

So (on OSX at least)mkvirtualenv --python=python3 nameOfEnvironment will suffice.

Kwangchowan answered 6/12, 2016 at 7:37 Comment(1)
Needs an two dashes. Would edit, but SE requires 6 characters and the change only needs one.Reproof
C
22

You can add this to your .bash_profile or similar:

alias mkvirtualenv3='mkvirtualenv --python=`which python3`'

Then use mkvirtualenv3 instead of mkvirtualenv when you want to create a python 3 environment.

Cool answered 17/1, 2018 at 4:16 Comment(0)
B
13

I find that running

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

and

export VIRTUALENVWRAPPER_VIRTUALENV=/usr/bin/virtualenv-3.4

in the command line on Ubuntu forces mkvirtualenv to use python3 and virtualenv-3.4. One still has to do

mkvirtualenv --python=/usr/bin/python3 nameOfEnvironment

to create the environment. This is assuming that you have python3 in /usr/bin/python3 and virtualenv-3.4 in /usr/local/bin/virtualenv-3.4.

Beautifully answered 14/4, 2015 at 10:16 Comment(1)
Just use which python2.4 or which python3.5 or which python3.6 to find the directory for --python=DIRECTORYNutt
B
5

This post on the bitbucket issue tracker of virtualenvwrapper may be of interest. It is mentioned there that most of virtualenvwrapper's functions work with the venv virtual environments in Python 3.3.

Butterandeggs answered 10/7, 2013 at 0:9 Comment(0)
I
0

I added export VIRTUALENV_PYTHON=/usr/bin/python3 to my ~/.bashrc like this:

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENV_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

then run source .bashrc

and you can specify the python version for each new env mkvirtualenv --python=python2 env_name

Ivonne answered 5/4, 2020 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.