Download and Install Distribute
curl http://python-distribute.org/distribute_setup.py | sudo python
Ok... now we have access to easy_install. Yay!
Install pip
sudo easy_install pip
Simple, huh? Ok, ok, ok.. Why pip? I'll show you! pip has numerous features, but the one I like the best is the freeze command.
pip freeze
This will dhow you all the currently installed python modules and their version. Why do we care? Numerous reasons, but it's also useful in virtualenv.
Install virtualenv
sudo easy_install virtualenv
Great! Almost there. Now... how do we use virtualenv and why? Let's say that I want to work on a new project and I don't want the hassle of setting up all the required modules I need or I want to share the required modules I need with someone else. There are numerous examples and this is just one.
Let's setup a project.
virtualenv --distribute --no-site-packages game
This sets up a new "workspace" called game including the python distribute module, but does not include the 53 installed python modules on my system. You should now have a game directory.
cd game
ls
bin include lib
Ehhh... I fail to see the point. I know. I haven't told you! if you were to utilize pip right now by issuing a pip freeze command, you would see ALL the modules on your system.
Activate your virtualenv
source bin/activate
(game) sh>
Here is the beauty. virtualenv is now "active" and you can see that by the beginning of your prompt conataining the text "(game)" followed by your normal prompt. Let's do a pip freeze again. WHAT?!?! Where are all my packages? Relax! They are still on your system, but just not part of your virtualenv. Don't belive me?
deactivate
pip freeze
Few! There they are. Ok.... activate your virtual environment again.
source bin/activate
(game) sh>
The "--no-site-packages" command did not copy all of the system's python modules. Now, you can easy_install all the modules you need for your project. Or, if someone has told you what the need from a pip point of view, you can use pip to grab what you need. To export the required modules for your project
pip freeze > requirements.txt
Now, send, give, IM, share, whatever this file with your coworker. They will activate their environment and do a pip install.
pip install -r requirements.txt
Now all the modules are installed that are required. Whoa!
Another nice feature of pip is the search feature
pip search <text>
pip will search PyPi for modules containing <text>.
Work on your project all you want, wand when you are done, run the following:
deactivate
This will put you back in your "normal" environment.
Sweet! Enjoy virtualenv and pip!!!
No comments:
Post a Comment