Skip to content

Instantly share code, notes, and snippets.

@rubillionaire
Created December 3, 2013 19:11
Show Gist options
  • Save rubillionaire/7775655 to your computer and use it in GitHub Desktop.
Save rubillionaire/7775655 to your computer and use it in GitHub Desktop.
Python using virtualenv and pip.

Python using virtualenv and pip

With every project you write in python, you will have different libraries that you lean on. Some projects might use the same library, but different versions. For this reason, the Python library virtualenv was created. To enable a developer to quickly create new environments.

Every virtualenv instance comes with pip installed, which is a package manager for python. This allows you to do install packages like so.

pip install django

Installing Virtualenv

With pip, it would be:

pip install virtualenv

Or from source. In this case, for virtualenv 1.10:

curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.10.tar.gz
tar xvfz virtualenv-1.10.tar.gz
cd virtualenv-1.10
[sudo] python setup.py install

Using virtualenv and pip

I like to set the virtual env directory as a hidden directory.

virtualenv .penv

To activate the virtualenv

source .penv/bin/activate

Start installing packages

pip install django

View the packages you have installed

pip freeze

Save dependencies to a file, so that you can recreate the environment later.

pip freeze > requirements.txt

The next person who uses the project can then:

cd ~/project/dir/
virtualenv .penv
source .penv/bin/activate
pip install -r requirements.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment