Virtualenv how to default to python3

Need help with C, C++, perl, python, etc?

Virtualenv how to default to python3

Postby Mike » 2015-05-16 18:12

I have a few questions regarding virtualenv and virtualenvwrappers. The following packages are available, I am currently using python3 for my project. However I would like have the option to use either 2.7 or 3.x for my virtual environments.

Note: During the creating of this post the actual problem was solved, but I decided to post it anyways just incase anyone has any comments there might clear any of my confusion further. My path to getting this working may also be useful for others. :)

Given this do I install both python-virtualenv and python3-virtualenv? What is that third package all about, does virtualenv support both versions by chance? I chose to install both python-virtualenv, and python3-virtualenv (not sure if both were actually needed). These packages where installed via apt-get.

python-virtualenv - Python virtual environment creator
python3-virtualenv - Python virtual environment creator
virtualenv - Python virtual environment creator
virtualenvwrapper - extension to virtualenv for managing multiple virtual Python environments

So far so good, "virtualenv test" creates a python2 env, and "virtualenv --python=/usr/bin/python3 test2" creates a python3 environment.

After installing virutalenvwrapper (also installed with apt-get) is where I hit the problem. In an attempt to default ot python3 I added the following to my .bashrc file.

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Projects
source /usr/share/virtualenvwrapper/

Sourcing the .bashrc file cases the following error. What am I missing here?

/usr/bin/python3: Error while finding spec for 'virtualenvwrapper.hook_loader' (<class 'ImportError'>: No module named 'virtualenvwrapper') There was a problem running the initialization hooks.

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 and that PATH is
set properly.

Further searching found a post on stackoverflow which led me to change the .bashrc to the three lines below. After this change it all works!!!

export VIRTUALENV_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Projects

Finally the commands for the two versions of environments.

Python3 (default) "mkvirtualenv test"
Python2 "mkvirtualenv --python=/usr/bin/python2 test2"
Posts: 57
Joined: 2005-11-16 19:12
Location: Sunshine Coast, BC

Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests