virtualenvwrapper

Date: February 9th 2016
Last updated: February 9th 2016

virtualenvwrapper is used to manage virtual environments.

Useful resources:

Make sure virtualenv version > 1.7

If virtualenv is old... pre-1.7... you need to use --no-site-packages to get a clean environment. This is the default configuration in all versions after 1.7.

sudo pip install virtualenv --upgrade #python2
sudo pip3 install virtualenv --upgrade #python3

install virtualenvwrapper

pip install --user virtualenvwrapper #python2
pip3 install --user virtualenvwrapper #python3

make virtualenvwrapper work on bootup

sudo nano .bash_profile #if this exists, bash won't read .bash_login or .profile
sudo nano . bash_login # analogous to .bash_profile
sudo nano .profile
sudo nano .bashrc #<==== I used this file. It is a non empty file.

# From the docs:
# http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html
# looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, 
# and reads and executes commands from the first one that exists and is readable.

# Add to the bottom of the file
export WORKON_HOME=$HOME/.virtualenvs
# note: Devel must exist... (run "mkdir Devel" before executing virtualenvwrapper functions)
export PROJECT_HOME=$HOME/Devel
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 #specify a version of python to use
# note: python must be set before running source...
source /usr/local/bin/virtualenvwrapper.sh

# save and reload the script
source ~/.bashrc

output from source ~/.bashrc:

virtualenvwrapper.user_scripts creating /home/ray/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/ray/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/ray/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/ray/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/ray/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/ray/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/ray/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/ray/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/ray/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/ray/.virtualenvs/get_env_details
virtualenvwrapper.user_scripts creating /home/ray/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/ray/.virtualenvs/postmkproject

Basic workflow: from the docs

1. Run: workon
2. A list of environments, empty, is printed.
3. Run: mkvirtualenv temp
4. A new environment, temp is created and activated.
5. Run: workon
6. This time, the temp environment is included.

Create a new project

# projects at home
# Devel has to exist according to the line added above in .bashrc
mkdir Devel 
mkproject myproject
# automatically activates in $HOME/Devel/myproject

Create virtualenv

# doesnt go to Devel
mkvirtualenv test-env

# if you find that you can still access global site-packages:
mkvirtualenv --no-site-packages test-env

# to create a virtualenv with specific python version
mkvirtualenv -p python3 --no-site-packages test-env

Check site-packages

workon #<empty>

mkproject -p python3 --no-site-packages Foo
...
#Installing setuptools, pip, wheel...done.
#bash: cd: /home/ray/Devel/Foo: No such file or directory
#Creating /home/ray/Devel/Foo
#Setting project for Foo to /home/ray/Devel/Foo

# check python install
>>> (Foo) ray@mycomputer:~/Devel/Foo$ python --version
#>>> python 3.4.3
>>> (Foo) ray@mycomputer:~/Devel/Foo$ python3 --version
#>>> python 3.4.3

# environment must be activated
lssitepackages
# >>>
#easy_install.py  pip-8.0.2.dist-info  setuptools                 wheel-0.29.0.dist-info
#_markerlib       pkg_resources        setuptools-20.0.dist-info
#pip              __pycache__          wheel

pip freeze
wheel==0.29.0

workon existing project

# Foo is in .virtualenvs/Devel/Foo
deactivate
>>> ray@mycomputer:~/Devel/Foo$

workon
>>> Foo

workon Foo
>>> (Foo) ray@mycomputer:~/Devel/Foo$

Get rid of myproject

# remove environment
rmvirtualenv Foo
# remove folder
rm -r ~/Devel/Foo

results matching ""

    No results matching ""