PYTHONPATH

Python keeps track of a list of directories that it will search for modules to import. By default this list includes the locations of the modules that are distributed with Python. But it does not necessarily include the directories where you would like to keep your own code. So if you want to import a module that you have written in order to run it, it may be necessary to either move the module to a location that Python can see, or to make sure that you start Python from the directory that the module is in (since Python always includes the current directory in its list). Unfortunately, the latter approach might not be possible if you are using a GUI interface like Idle or the MacPython IDE -- since these always start from the directory they are in.

You can check which directories Python knows about as follows:

     >>> import sys
     >>> sys.path

This will return a list of the directories in which Python will look for modules. You can also see this same list in Idle by choosing Path Browser on the File menu. If you want to add your desktop directory to this list you can do so on the fly as follows (Mac):

     >>> sys.path.append('/Users/myusername/desktop')

With Windows you need to be more careful about paths that have spaces in them (see below). There is also the issue that Windows paths use the backslash as a separator and this is often an escape character in strings. So what I have to do on my XP is actually use two backslashes in describing the path:

     >>> sys.path.append('c:\\docume~1\\username\\desktop')

This approach would solve the problem except for one catch -- when you leave Python your change disappears and the next time you start Python you have to make the same path addition again. What we need is a way to change the default PYTHONPATH.

Changing the default PYTHONPATH in Windows

I have tested the following with Windows XP. For other versions of Windows the process should be similar

Before starting, you should check what the path to your desktop (or whatever directory you want Python to know about) actually is. Use the Windows Explorer and find the complete path, e.g. C:\Documents and Settings\username\Desktop. Write this path down.

Go to the Control Panel, open System, Choose Advanced, and click on the Environment Variables button. Check to see if there is already a variable called PYTHONPATH defined. If not, click new, type "PYTHONPATH' for the variable name, and enter the shortened path for the variable value:

   c:\docume~1\username\desktop

where 'username' is the actual name of your user directory. If PYTHONPATH is already defined, just click edit and add the paths you want to the end of the list that is already there (separating paths by semicolons). In both cases the paths listed in PYTHONPATH will be appended to the locations that PYTHON already knows about, i.e., they do not overwrite these locations. But you should be careful anyway not to lose any of the paths that are already there since they may be used by Python extensions or applications.

The above uses what are called shortened path names (= the first six characters of a directory name followed by ~1) in order to handle directories like Documents and Settings that have spaces in their name. This works on XP. Other versions of Windows, though, deal with spaces in obscure and marvelous ways. If the above does not work, you might try enclosing the entire path in quotes like this:

   "c:\Documents and Settings\username\desktop"

You might also try using two backslashes.

Changing the default PYTHONPATH in OS X

In OS X you can change the default PYTHONPATH by adding a directory containing a plist file to your home directory. It will end up in this location:

   /Users/username/.MacOSX/environment.plist

where 'username' is the name of your home directory. You will have to make the .MacOSX directory if one does not already exist.

First go here to download the property list editor. If you are running under a pre-10.4 version of OS X, there are also older versions of the property list edtor available at the Company Site. Put this somewhere reasonable, like in Applications/Utilities. We will use it to make the environment.plist file, and you will want to keep it to have in the future.

Now open the terminal, make sure you are in your home directory, and enter 'ls -la' after the prompt:

   mycomputer:~ username$ ls -la

You should see a listing of your home directory that includes system files that begin with a dot. Look and see if .MacOSX is there. If it is not, enter:

   mycomputer:~ username$ mkdir .MacOSX
   mycomputer:~ username$ ls -la

to make it and double check that it is there.

Now open your property list editor, make a new property list, a new root, and a new child with the following properties:

   name: PYTHONPATH, type: string, value:  /Users/username/desktop

Save this as environment.plist (the suffix is crucial), and then move it into the .MacOSX directory that you just created. You will need to log out and log back in before this takes effect -- but once it does take effect, Python will know about your desktop, whether you are working from the terminal, from Idle, or from the MacPython IDE.

If there was already a .MacOSX directory in your home directory, skip that step and just put environment.plist in the directory that is already there. If there is already an environment.plist, don't make a new one but edit the one that is there and add a child named PYTHONPATH under the root. If there is already a PYTHONPATH, just add your path onto the end of the value (paths are separated by colons).

There is further Apple documentation on this here.

Enjoy!

--Paul