Linux/Unix > Python Essential Reference, Second Edition > 8. Modules and Packages > Module Loading and Compilation | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238111161105084124003001139038249

### Module Loading and Compilation

So far, this chapter has presented modules as files containing Python code. However, modules loaded with `import` really fall into four general categories:

- Programs written in Python (`.py` files)

- C or C++ extensions that have been compiled into shared libraries or DLLs

- Packages containing a collection of modules

- Built-in modules written in C and linked into the Python interpreter

When looking for a module `foo`, the interpreter searches each of the directories in `sys.path` for the following files (listed in search order):

1. A directory `foo` defining a package

2. `foo.so`, `foomodule.so`, `foomodule.sl`, or `foomodule.dll` (compiled extensions)

3. `foo.pyo` (only if the `-O` or `-OO` option has been used)

4. `foo.pyc`

5. `foo.py`

Packages are described shortly; compiled extensions are described in Appendix B, “Extending and Embedding Python.” For `.py` files, when a module is first imported, it’s compiled into bytecode and written back to disk as a `.pyc` file. On subsequent imports, the interpreter loads this precompiled bytecode unless the modification date of the `.py` file is more recent (in which case, the `.pyc` file is regenerated). Files ending in `.pyo` are used in conjunction with the interpreter’s `-O` option. These files contain bytecode stripped of line numbers, assertions, and other debugging information. As a result, they’re somewhat smaller and allow the interpreter to run slightly faster. If the `-OO` option is specified instead of `-O`, documentation strings are also stripped from the file. This removal of documentation strings occurs only when `.pyo` files are created—not when they’re loaded. If none of these files exists in any of the directories in `sys.path`, the interpreter checks whether the name corresponds to a built-in module name. If no match exists, an `ImportError` exception is raised.

The compilation of files into `.pyc` and `.pyo` files occurs only in conjunction with the `import` statement. Programs specified on the command line or standard input don’t produce such files.

When `import` searches for files, it matches filenames in a case-sensitive manner— even on machines where the underlying filesystem is not case-sensitive, such as Windows. (Such systems are case-preserving, however.) Thus, `import foo` will only import a file `foo.py` and not a file `FOO.PY`. However, note that versions of Python prior to 2.1 didn’t handle this case correctly on all platforms. If you’re concerned about backward compatibility, avoid the use of module names that differ in case only.

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.