Tuesday, July 2, 2013

Proper Way to Start Programming Python

So after quite a few missteps I've fallen into the correct pattern for programming python. There are two main tools for installing python modules: pip and setuptools' easy_install script. On a virgin system you'll want to the following packages from the OS distribution. I've repeated this process on my macbook air, windows machine under cgywin, and my Ubuntu virtual machine.

1. Install python and python-setuptools

You'll need to install python and python-setuptools before you begin. Macs come with these packages pre-installed so you can skip this step if you'd like.


2. easy_install virtualenvwrapper

Then you'll want to install virtualenvwrapper via setuptool's easy_install. It should be the only package you should ever install with easy_install.

jbisbee@benny:~$ easy_install virtualenvwrapper
Searching for virtualenvwrapper
Reading http://pypi.python.org/simple/virtualenvwrapper/
Best match: virtualenvwrapper 4.0
Finished processing dependencies for virtualenvwrapper

3. Find locaiton of virtualenvwrapper.sh

Once you have virtualenvwrapper installed you'll want to see where it was installed in your path. Please note that it is a shell script and does end with the .sh file extension.

jbisbee@benny:~$ which virtualenvwrapper.sh

4. Add virtualenvwrapper.sh to Your Shell Startup

# your ~/.bashrc or ~/.profile file
export WORKON_HOME=~/.virtualenvs
# virtualenvwrapper.sh may be installed in 
#     /usr/local/bin on your system
source /usr/bin/virtualenvwrapper.sh  

5. Reload Your Shell

jbisbee@benny:~$ source ~/.bashrc
# source .profile

6. Create a New virtualenv Environment

Now you're in business to start creating virtual workspaces and installing libraries within them with pip. Using django as an example :)

jbisbee@benny:~$ mkvirtualenv tutorial
New python executable in tutorial/bin/python2.7
Also creating executable in tutorial/bin/python
Installing setuptools............done.
Installing pip...............done.

7. Install Python Modules with pip

Now install python modules with pip within your virtualenv sandbox:

(tutorial)jbisbee@benny:~$ pip install django
Downloading/unpacking django
  Downloading Django-1.5.1.tar.gz (8.0MB): 8.0MB downloaded
  Running setup.py egg_info for package django
Successfully installed django
Cleaning up...

Have fun and let me know if you run into any issues as I wrote this post pretty quickly and may have left something out.

Edit: This blog post has been translated to Spanish by +Apokalyptica Painkiller. Thank you!

1 comment:

  1. Great overview of starting up a project from scratch.

    After installing the modules, what about making a requirements.txt file? That has proved very helpful to me at times as my number and variations of virtualenvs piles up. To make a requirements.txt file simply:

    pip freeze > requirements.txt

    When it is time to reconstitute your dev virtual environment (either for yourself on another machine or for a team member):

    pip install -r requirements.txt