Setting up Python Virtual Environments

From Notepedia
Jump to: navigation, search

Virtualenv

Getting a virtualenv running
$ pip install virtualenv 
Navigate to the target directory, then run this to set up a virtualenv within that directory:
$ virtualenv env 
If you wish to set up a virtualenv from a particular interpreter version use the -p flag as so:
$ virtualenv --no-site-packages --distribute -p /usr/local/bin/python3.4 env 
Next, activate the virtualenv
$ source env/bin/activate 
Install a particular version of a package
$ pip install flask==0.10.1 
Record installed packages
$ pip freeze > requirements.txt 
Install packages from requirements.txt
$ pip install -r /path/to/requirements.txt 
Lastly, when you're done just run
$ deactivate 

Virtualenvwrapper

From http://docs.python-guide.org/en/latest/dev/virtualenvs/

Create a virtual environment (creates the venv folder inside ~/Envs)
$ mkvirtualenv venv 
Create a virtual environment with a specific interpreter version
$ mkvirtualenv -p python2.7 venv 
Work on a virtual environment Workon also deactivates whatever environment you are currently in, so you can quickly switch between environments.
$ workon venv 
Make project, which creates the virtual environment, and also a project directory inside $PROJECT_HOME,
$ mkproject myproject 
Deactivating is still the same:
$ deactivate 
To delete:
$ rmvirtualenv venv