Quick Start




Lecture Graphics




Python Links

Python Home


Basic install


Learning Python


On Linux and Mac OSX systems, the business of telling Python where to look for modules is handled by setting the PYTHONPATH environment variable, which lists the directories Python should look in (besides the default ones) when a script imports a module. Get ready for some truly geeky stuff below, which I wish you didn't have to know. If you are running on a server, your instructor will have relieved you of the burden of knowing this stuff, but if you are running on your own machine, you need to know how this works. I am working on an install procedure that carries out the following stuff automatically, but that is not available yet. In any event, it will always be helpful to some users to know what is really going on under the hood. [**NOTE: The simplification could be handled by modifying the clickable UpdateShellProfile.command script that's part of the MacPython distribution.]

Lets suppose that your home directory is /Users/frodo , and you have decided to put all your commonly used modules in /Users/frodo/MyModules . Then, you want to set PYTHONPATH to this directory. (To list multiple directories to be searched, just separate them by a colon, e.g. dir1:dir2:dir3 ). A slight complication is that Unix-like operating systems (including Linux and Mac OSX) offer various different flavors of the command-line processor, called "shells," and the way you set environment variables depends on which shell you are using.

The default shell on Mac OS is bash . The name of the shell you are running appears at the top of the terminal window you open with the Terminal utility. You can also tell if you are running bash (or its relative sh) because the command-line prompt ends with a $. You can also determine the default shell by typing the command

echo $SHELL

In sh and bash, you set the environment variable using the command

export PYTHONPATH=/Users/frodo/MyModules

However, it's a bit of a nuisance to rember to do this every time to want to work with Python, so the more usual thing is to create a configuration file in your home directory, using any text editor, and put the above command into that file. For bash, the name of the configuration file is


You can put as many commands in this file as you want; they will all be executed each time you create a new terminal window. Note the period at the beginning of the file name. (Files that begin with a period are invisible in the Finder, so it can be a bit tricky to open them in some text editors)

The other commonly used shell is csh (pronounced "c-shell" -- get the joke?). In this shell, and its relative tcsh, to set PYTHONPATH you would type

setenv PYTHONPATH /Users/frodo/MyModules

You can tell if your shell is csh or some flavor of it because the command line prompt ends with a % sign. The configuration file for csh is


If you prefer to run csh, on Mac OSX you can change your default shell by typing:

chsh -s /bin/csh

On a Linux system you do the same, except you don't need the -s flag. Be warned, though, that if you do this after you have installed Python, then the default python command may not point to the Python you installed, because the installer may not have set up a configuration file for the new shell you are using. [**ADD LINK to "Which python am I using," PATH variable and how to fix this if necessary.] Note that you only have to issue the chsh command once to change your default shell; you don't have to do it every time you log in. If you are doing your work on a server set up for a class, your instructor should have set things up so you get the desired shell automatically when you log in.

Microsoft Windows also has environment variables. Instructions for how to set PYTHONPATH in Windows are given here (see Section 3.3.1). The dialog box method of setting environment variables seems to work more reliably on some systems than the command-line method.

Note that for modules put on the directories listed in PYTHONPATH, Python won't search recursively down into the directory tree for things to import. For example, if you put a folder (directory) called Goodies in your MyModules directory, then if you type import Goodies into the Python interpeter, Python will import all the modules contained in that folder, but it will not find modules that are stored in subfolders of Goodies. Also, there's no provision to specify a directory path in the import of a module, i.e. things like import /Users/rtp1/MyStuff/rabble.py won't work. ]

On a Mac, you can start up idle by double-clicking on its icon in the Python folder (found in your Applications folder). However, the Finder does not examine the PYTHONPATH variable, so it won't find modules installed in nonstandard places. There is a workaround for this, but to keep things simple, I recommend starting up idle by typing the command into a terminal window.

[**Other useful information about environment variables. The PATH variable, and where the OS looks for executables.]

Hint to the instructor: You can make life easier for your students running Python on their own computers by telling them how to set the default shell to the one you prefer, having them all use the same name for the modules directory in their home directory, and making a .cshrc or .shrc file with the appropriate command in it, which they can just copy into their home directory. If you are setting up the courseware on a server, then you can just have the sysadmin set the shell for each student account, and copy an appropriate .cshrc or .shrc file into each student home directory. Then the student won't need to know any of this stuff. In preparing this file, you must also make sure to set the PATH enviroment variable if necessary so that the OS will find the version of Python that you want the student to use.