Setting your PYTHONPATH environment variable

This tutorial was generated from an IPython notebook. You can download the notebook here.

The PYTHONPATH environment variable lets Python know where to look for files when using import. For a default Canopy installation on Mac OS X, it may look something like this (all on a single line):

:.:/Users/Justin/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages:

This means that it will look for modules first in the present working directory (./) and then where Canopy keeps all of the packages managed by its package manager. The directories are separated by colons in Mac OS X and Linux. The directories are separated by semicolons in Windows.

If you write your own module, Python will find it, so long as you are running Python in the same directory as the module (./). But this can be cumbersome and inconvenient. We'd like to add another directory for other modules. To do this, you need to change the PYTHONPATH environment variable. For this demonstration, I will make my PYTHONPATH environment variable the following (all as a single line):

:.:/Users/Justin/bebi103:/Users/Justin/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages:/usr/local/lib/python2.7/site-packages:

This means that the Python interpreter will first look in the present working directort to find a file with a name matching the import statement. Next, it will look in the directory /Users/Justin/bebi103. This would be a directory where I will keep all of my modules that I write for this class. Then, it will look in the place where Canopy keeps its own packages. Finally, I also include /usr/local/lib/python2.7/site-packages because I have some packages in there that are not part of Canopy.

Naturally, you'll want to change your PYTHONPATH to fit the name of your own home directory, the place you keep your own modules, etc.

Linux

If you are using Linux, you should launch Canopy from the command line. To set your PYTHONPATH, either edit your ~/.profile file or the appropriate rc file for the shell you are using. Here ~/ is an abbreviation for your home directory. In my case, I would add the following line to my ~/.profile file (all as a single line):

export PYTHONPATH=:.:$HOME/bebi103:$HOME/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages:/usr/local/lib/python2.7/site-packages:

Alternatively, if I just wanted to prepend the directory for BE/Bi 103, I would add the line:

export PYTHONPATH=:.:$HOME/bebi103:$PYTHONPATH

where I have kept :.: explicitly to make sure the present working directory is searched first.

After you have adjusted your ~/.profile file, the next time you fire up a shell, your environment variable will be properly set. When you launch Canopy from the command line, Canopy will be aware of your PYTHONPATH.

Mac OS X

The instructions for Mac OS X is similar to Linux. In fact, do exactly what the Linux instructions say. If you then launch Canopy from the command line, everything will work fine.

However, it might be nice to just click on the icon and not have to use the command line. To enable Canopy to see your PYTHONPATH when you launch it by clicking on the icon, you can either create or edit the file ~/.launchd.conf (in your home directory). Add the following line to this file (all as one line):

launchctl setenv PYTHONPATH :.:$HOME/bebi103:$HOME/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages:/usr/local/lib/python2.7/site-packages:

This will make Canopy (and any other program that you launch by clicking on the icon) aware of your PYTHONPATH.

Windows

Andrey wrote these instructions on how to set your PYTHONPATH in Windows 7 and Windows 8.