/>

Thursday, February 26, 2015

pip, virtualenv and virtualenvwrapper-win

I have googled the same information quite often in the past and I would like to collect this information in a post that I can just look up easily. There are lots of tutorials on various aspects of using pip, virtualenv and virtualenvwrapper. This post isn't meant to be a tutorial but rather a reference for certain features/gotchas that fit into my workflow.

An explanatory tutorial video on virtualenv:





An explanatory tutorial video on virtualenvwrapper:




On Windows:

Installing pip

A couple of ways - using easy_install -OR- using the get-pip script

Using easy_install

easy_install is an older package manager that is automatically installed with Python. It is found in <PYTHONHOME>\Scripts. You can just execute


easy_install pip

get_pip script

Download the get-pip script. Once downloaded, you can run at the prompt.


python get-pip.py

Virtualenv

To install:


pip install virtualenv

virtualenvwrapper-win


pip install virtualenvwrapper-win

Main commands of virtualenvwrapper-win:
https://pypi.python.org/pypi/virtualenvwrapper-win

Using a specific python version with a virtual environment

Use the -p or --python argument to point to the python exe that you want the new virtualenv to use


mkvirtualenv <env_name> --python=<path_to_python_exe>


You can also use pywin to switch python versions in an environment

Compiling and linking C/C++ code in a virtual env

pip downloads the source code for a package and if required, kicks off a build using a compatible C/C++ compiler. For additional details on Visual Studio versions used to compile python binaries and consequently, packages visit this link

On Ubuntu:

Installing python, pip, distribute:


sudo apt-get install python
sudo apt-get install python-dev
sudo apt-get install python-setuptools
sudo apt-get install python-pip
sudo easy_install -U distribute

Configuring virtualenvwrapper:

If it doesn't already exist, create ~/.profile and add the following:


$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7
$ export WORKON_HOME=~/Envs
$ export PROJECT_HOME=~/Projects
$ source /usr/local/bin/virtualenvwrapper.sh

Ensure that the WORKON_HOME and PROJECT_HOME directories are created. You can either logout and back in -OR- source ~/.profile to update these changes into your environment.

Some important commands:

From the docs,

Managing Environments

mkvirtualenv
rmvirtualenv
lsvirtualenv

Controlling the active environment

workon
deactivate

Navigating to a virtualenv

cdvirtualenv
  • Change the current working directory to the active virtualenv in $VIRTUAL_ENV

cdsitepackages
  • Change the current working directory to the site-packages directory of the active virtualenv in $VIRTUAL_ENV

lssitepackages

  • lists packages installed in the current virtualenv's sitepackages folder

Path Commands

add2virtualenv
  • Adds the specified directories to the Python path for the currently-active virtualenv.

Project Directory Management

mkproject
  • Create a new virtualenv in the WORKON_HOME and project directory in PROJECT_HOME. The names of the virtualenv and the project dir are the same.


setvirtualenvproject
  • Binds a directory as the project folder for the current virtualenv
cdproject
  • Change the current working directory to the one specified as the project directory for the active virtualenv.

Managing installed packages

wipeenv
  • Remove all of the installed third-party packages in the current virtualenv.