Wednesday, August 3, 2016

Tidbits on Python's virtualenv

Python being a mess that it is - *cough* Python 2 vs 3 *cough* - has a thing for virtual dev environments.

Virtual dev enviroments or virtualenv solves the mess by creating isolated dev environments. Each environment then can have it's own Python and lib versions, dependencies and even permissions and settings. Virtualenv is complimented by virtualenvwrapper.

This is all well and good, unfortunately, virtualenv + wrapper are terminal (command line) driven and I often forget the basic commands. Hence, this blog post:

Basic assumption here is that you've setup and configured virtualenv with virtualenvwrapper.

1. Make a new virtual environment with a specific version of Python: use the -p flag

mkvirutalenv -p [path-to-python] [project name]

example: mkvirtualenv -p /usr/bin/python project1

2. List all existing virtual environments; workon command without arguments also works

lsvirtualenv -b

-b flag = brief mode, disables verbose output

3. Delete or remove an existing virtual environment; only deletes environments found in the WORKON_HOME path

rmvirtualenv [env_name]

Virtual environment must be deactivated before removing.

4. Deactivate current active virtual environment

deactivate

Also:

- Command ref for virtualenvwrapper
- Ref for virtualenv