Saturday, March 21, 2009

Python split packages

If you split a python package over several dirs, and make all dirs available on the PYTHONPATH, python will only be able to see one of these dirs, and you will get ImportError for all modules residing in the other dirs.

This is because python stops at the first physical dir on the path which contains a subdir matching the top-level package name. In other words, python assumes that all modules belonging to a top-level package name reside in the same directory.

Example synopsis:

/home/a/python/mycompany/a.py
/home/b/python/mycompany/b.py

a.py:
-------
import mycompany.b
-------
b.py:
-------
pass
-------

>export PYTHONPATH=/home/a/python/:/home/b/python/
>cd /home/a/python/
>python mycompany.a

This will give you an ImportError.

This has the implication that all separate "compilation units" (eggs etc) must have a UNIQUE top level python package name! Thus having mycompany.a in a.egg and mycompany.b in b.egg will fail. Depending on the order which the eggs occur on the PYTHONPATH, only one of them will be seen by the module loader.