Table Of Contents

Previous topic

C++ Introduction to wxWidgets

Next topic

Using Python on Mac OS X

This Page

Python

Python is a dynamic, interactive, object oriented languages with functional and procedural capabilities. It is well known and documented elsewhere. Also, it is a lot of fun.

In short, it is a general purpose programming language that can be used for scripting, desktop applications, web applications, and more (just about anything except device drivers and the like). It is easy to learn and simple to use. I document here a few things I’ve learned that make Python easier to work with.

Packages

Use distribute (which provides the easy_install command) and pip to install packages. Sometimes you may also need to download packages directly from the developers and follow the directions (usually extract, then ‘python setup.py install’). Pip and easy_install are two different commands that go about downloading and installing packages for you from the package index. While easy_install seems more ubiquitous, you may like pip more and sometimes there are packages it handles better.

It’s a good idea to organize your packages per project or experiment, or at least keep from installing packages into the system site-packages directory. The best tool available for this purpose is a package called virtualenv. I recommend installing distribute, pip, and virtualenv into your system wide site-packages directory, then using a virtualenv to organize additional installations.

For Linux users, there may be system packages (RPMs, Debs, etc) for distribute, pip, and virtualenv. Distribute may be listed as setuptools, since it is a fork/replacement for that package. As long as the versions are not ancient they probably will work fine.

If you are on Windows or Mac (or an OS lacking usable packages), download and install the three from their Python sources. If you don’t have administrative rights and permissions needed for the installation and have a recent enough version of Python, you can perform a user installation.

System Wide Installation

These instructions describe installing distribute, pip, and virtualenv to the system wide site-packages directory.

Windows

  • Install a recent version of Python if you have not already. I will use version 2.7 in the examples below.
  • Download distribute-a.b.c.tar.gz, pip-l.m.n.tar.gz, and virtualenv-x.y.z.tar.gz. Use an application like 7-zip to extract the three tar.gz source files so that you have three directories in your Download directory: distribute-a.b.c, pip-l.m.n, and virtualenv-x.y.z.
  • Open the command line by clicking on the Start or Windows menu and running cmd. Depending on your security settings you may need to search for cmd in the Windows menu, then right click on it and ‘run as administrator’.
  • Install the packages.
cd Downloads\distribute-a.b.c
c:\Python27\python.exe setup.py install
cd ..\pip-l.m.n
c:\Python27\python.exe setup.py install
cd ..\virtualenv-x.y.z
c:\Python27\python.exe setup.py install
  • Now you can create virtualenv directories and activate them.
cd %HOMEDRIVE%%HOMEPATH%
mkdir virtualenvs
cd virtualenvs
c:\Python27\Scripts\virtualenv.exe MyVirtEnv
cd MyVirtEnv
Scripts\activate.bat
  • If you want to ensure that your virtualenv is pristine, and contains no other packages initially available to it other than the standard library, give the –no-site-packages argument when creating it.
c:\Python27\Scripts\virtualenv.exe --no-site-packages MyVirtEnv
  • You’ll notice that your command line now starts with the name of the active virtualenv in parentheses. Python and all of the Python scripts in your installation are now in your path. This means you can simply run python to start the interpreter or execute scripts. You can also run easy_install <package name> or pip <package name> to install packages. Installed packages will go into the virtualenv.
  • To deactivate the currently active virtualenv, run ‘deactivate’.

Linux, Mac OS X, BSD, etc

  • Install a recent version of Python if you have not already. On Linux and BSD, you can probably find suitable packages for your distribution. Mac OS X comes with 2.6 now, which is new enough for most purposes.
  • Download distribute-a.b.c.tar.gz, pip-l.m.n.tar.gz, and virtualenv-x.y.z.tar.gz to a directory (I use Downloads), or install these packages using RPMs, Debs, etc.
  • If you downloaded the tar.gz files, open a terminal and install the packages.
cd Downloads
tar zxf distribute-a.b.c.tar.gz
tar zxf pip-l.m.n.tar.gz
tar zxf virtualenv-x.y.z.tar.gz
cd distribute-a.b.c
python setup.py install
cd ..\pip-l.m.n
python setup.py install
cd ..\virtualenv-x.y.z
python setup.py install
  • Now you can create virtualenv directories and activate them.
cd
mkdir virtualenvs
virtualenv MyVirtEnv
cd MyVirtEnv
source bin/activate
  • If you want to ensure that your virtualenv is pristine, and contains no other packages initially available to it other than the standard library, give the –no-site-packages argument as shown below.
virtualenv --no-site-packages MyVirtEnv
  • You’ll notice that your command line now starts with the name of the active virtualenv in parentheses. Python and all of the Python scripts in your installation are now in your path. This means you can simply run python to start the interpreter or execute scripts. You can also run easy_install <package name> or pip <package name> to install packages. Installed packages will go into the virtualenv.
  • To deactivate the currently active virtualenv, run ‘deactivate’.
  • Since you’re on a UNIX like OS, you may wish to also install virtualenvwrapper into the system wide site-packages to make working with virtualenv easier.

User account installation

In the above instructions distribute, pip, and virtualenv are installed to the system wide site-packages directory. If you do not have permission to install packages there, you can instead install them into your user account directory.

When you come to the steps involving c:\Python27\python.exe setup.py install on Windows or python setup.py install on other platforms, simply add the --user option to setup.py:

c:\Python27\python.exe setup.py install --user
python setup.py install --user

Take note in the output where these files are going. The executable scripts should land in $HOME/.local/bin on UNIX style systems and in %HOMEDRIVE%%HOMEPATH%\AppData\Roaming\Python\Scripts on Windows. You can then run virtualenv from these paths to get set up.

%HOMEDRIVE%%HOMEPATH%\AppData\Roaming\Python\Scripts\virtualenv.exe MyVirtEnv
~/.local/bin/virtualenv MyVirtualEnv

Using pydistutils.cfg

For completeness sake, it should be mentioned that it is also possible to use another file to control package installation per user. On UNIX like systems this file is $HOME/.pydistutils.cfg and on Windows it is %HOMEDRIVE%%HOMEPATH%\pydistutils.cfg. Use of a pydistutils.cfg should generally be discouraged in favor of the --user and virtualenv methods explained above. You should especially note that using both virtualenv and pydistutils.cfg will not work!

Nevertheless, here is an example of its setup and use on Mac OS X.

The commands below download and install distribute_setup.py, creates the file ~/.pydistutils.cfg (that is, $HOME/.pydistutils.cfg), then installs pip. This will put everything installed by easy_install and pip in your home directory for easy management (and by management I mean wiping out and starting over usually).

# On Mac OS X this would be pasted into the Terminal
echo "[install]" > ~/.pydistutils.cfg
echo "install_lib = ~/Library/Python/\$py_version_short/site-packages" >> ~/.pydistutils.cfg
echo "install_scripts = ~/bin" >> ~/.pydistutils.cfg
mkdir -p ~/Library/Python/2.6/site-packages
curl -O http://python-distribute.org/distribute_setup.py
python distribute_setup.py
rm distribute_setup.py
~/bin/easy_install pip

With this file in place, additional packages installed with easy_install, pip, or setup.py install will go into the user’s home directory.