Virtualenv python

From wiki.techunit.org
Jump to: navigation, search

Configuration

Installation

pip install virtualenv

virtualenv creation

On your devel environment, create a venv directory, containing all your projects venv.

mkdir /opt/venv

Basic

cd /opt/venv
virtualenv project_name
# or 
virtualenv --no-site-packages project_name

Change python interpreter

cd /opt/venv
virtualenv -p /usr/bin/python2.7 project_name

venv usage

Activate/deactivate

#Activation
source /opt/venv/project_name/bin/activate

Once virtualenv activate, all packages installed from pip will be installed in the venv.

  • To install package with pip:
pip install package

To deactivate simply type

deactivate

freeze packages with pip

In order to keep your environment consistent, it’s a good idea to “freeze” the current state of the environment packages. To do this, run

pip freeze > requirements.txt

Recreate venv

This will install the same packages using the same versions

pip install -r requirements.txt

Sources

python-guide.org

Elliot
techUnit's cofounder