Importing from within a package

In Python you can set your imports to be relative when inside a package - I just found that Python has had this capability since 2.5.

PEP 328 -- Imports: Multi-Line and Absolute/Relative

The example.

The gotcha: it does not work when running a module individually - in other words, you cannot have a test package as a child package and reference the parent package relatively. See "How to do relative imports in Python?" for more detail.


Written by Andrew Ittner in misc on Sun 14 March 2010. Tags: open source, python