next up previous contents
Next: 2.2.3 ``Compiled'' Python files Up: 2.2 The Interpreter and Previous: 2.2.1 Error Handling

2.2.2 The Module Search Path

When a module named spam is imported, the interpreter searches for a file named spam.py in the current directory, and then in the list of directories specified by the environment variable PYTHONPATH. This has the same syntax as the Unix shell variable PATH, i.e., a list of colon-separated directory names. When PYTHONPATH is not set, or when the file is not found there, the search continues in an installation-dependent default path, usually .:/usr/local/lib/python.

Actually, modules are searched in the list of directories given by the variable sys.path which is initialized from the directory containing the input script (or the current directory), PYTHONPATH and the installation-dependent default. This allows Python programs that know what they're doing to modify or replace the module search path. See the section on Standard Modules later.



guido@cnri.reston.va.us