Beginners Guide : pip and virtualenv

Requirements

I will be basing this tutorial on the setup I have at home. So for the best results using this guide I would recommend using Ubuntu 10.10. I will try and keep the instruction as generic as possible. Overall goal will be to introduce you to the magic of pip and virtualenv.

Install easy_install

Now it may seem unintuitive to install a tool with the very tool its trying to out date, but it easiest way I have found to do it. 

You will need to install easy_install 

1 $ sudo apt-get install python-setuptools python-dev build-essential

 Once that is installed you are ready to get pip

1 sudo easy_install -U pip

Since were installing things you might as well install virtualenv. 

1 sudo pip install -U virtualenv

Creating a virtual environment

The problem that virtualenv is trying to solve is that when you install multiple python package often with various installation methods.  It will become increasingly more difficult to keep your core system in order. The idea with virtualenv is to create disposable and quickly reproducible environments to run your python projects. 

For ease on my system I have mapped /srv/virtual/ the space where I keep all my virtualenv. So lets say I have Django project I am working on I would name the virtualenv something along the lines of django-<projectname>.

1 $ virtualenv --no-site-packages django-mim

The —no-site-packages option tells virtualenv not to inherit any libraries from your existing Python installation’s site-packages. To activate the newly created virtualenv, you will need to do the following. 

 1 # activating newly create virtualenv
 2 $ source django-mimo/bin/activate
 3  
 4 # your prompt should change to something similar the following
 5 (django-mimo)$ 
 6  
 7 # in order to get back to system all you need to do is 
 8 (django-mimo)$ deactivate
 9  
10 $

Once you have activated the virtualenv you are running within a system that has all the packages you have installed to that particular virtualenv. You can freely browse your file system and run tools as normal; but with the added value that you have the newly installed packages in separated and clean environment maintained by you. 

So know we have just created a pristine environment to begin customizing for our projects needs. Enter pip. Pip is designed to make sense of all the various ways you can install python packages on your system. Its trying to solve the problem of how to consistently add and remove desired python packages without any adverse affect on system. I cant tell you how many times I have forgotten the install method for some package and I have to go rummaging through my python path to attempt to remove all the installed eggs. Ugh not fun. 

We can use pip to directly install into the virtualenv just by specifying it. In order to do the following your working directory will be the directory that you just created your virtualenv within. In this case it will be the parent directory of the django-mimo virtualenv

1 $ pip install -E django-mimo Django

Now this package wont be accessible outside of the virtualenv, which is exactly the desired outcome. 

Now lets say you have a friend and he wants a copy of your pristine django virtualenv with all the correct package for a project you guys are working on. Do the following.

# creating requirements file from a virtualenv setup
$ pip freeze -E django-mimo > requirements.txt
 
# installing from a requirements file
$ pip install -E django-mimo -r requirements.txt

Thats gives him everything you have installed and he can then turn around and create his own virtualenv with the same exact setup. Everything just works.

 

From:http://www.mahdiyusuf.com/post/5282169518/beginners-guide-easy-install-pip-and-virtualenv-1?853d4018

posted on 2013-05-21 21:13  hellopython  阅读(390)  评论(0编辑  收藏  举报

导航