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, but most users will not need to know about shells in this amount of detail. If you are running on a server, your instructor will have relieved you of the burden of knowing this stuff. My setpath.py utility, in Courseware attempts to relieve users of the burden of dealing with shells and paths in detail. In any event, it may be helpful to some users to know what is really going on under the hood.

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


but bash will use .profile (the startup file used by sh) if this file isn't found. 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


which will also be used by tcsh if the file .tcsh isn't found . 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 an integrated development application like canopy or 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. For canopy , which uses ipython as its interpreter, the specification of paths can be handled by editing the ipython startup file, as explained in Courseware . This task is also automatically handled by my setpath.py utility.

Hint to the instructor: Since it is recommended that most users install the Enthought Canopy distribution, you should simply make sure all Mac OSX users are using the bash shell, which is currently the default for Macs The setup.py utility in Courseware handles the job of editing the .bash_profile file to set PYTHONPATH . 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 shell configuration file into each student home directory. Then the student won't need to know any of this stuff. In many Linux Python distributions you would also set the PATH environment variable so that the python command executes the version of Python you want to be using, since there may be many versions on the server. However, the Enthought Canopy distribution (which is recommended) uses virtual environments, which are set up individually for each user automatically the first time a user runs the Canopy application. On Mac or Windows, this is normally done by just clicking on the icon, but under most forms of Linux the full path of the Canopy application must be typed into the command line the first time Canopy is used. Thereafter, on subsequent login sessions the user can normally just type canopy at a bash command prompt and the right version will be invoked. See also these notes on how to set up a course server.