Python modules

Ben Finney ben+python at
Tue Nov 11 23:18:50 CET 2014

Will Acheson <willyach07 at> writes:

> I have had a lot of trouble with executing relative imports with some
> of my projects in python.
> Are there any best practices or methods besides '../../' type
> hard-coding?

The important point to learn with Python's import system, as contrasted
with various other languages, is that it is *intentionally* abstracted
from the filesystem hierarchy.

That allows all kinds of helpful infrastructure, but it also comes with
the cost of learning how to let Python know where modules are to be
imported from; it's not a simple case of stating filesystem locations.

The point which tripped me up for a long time, and which I have to keep
reminding myself of:

Python looks for modules in its import search path. Corollary: structure
your project into a hierarchy of packages, including the top level, and
specify all relative imports starting at that top-level package.

Often the cause of my intra-project import woes comes from having
several *discrete* packages, but trying to do relative imports between

    hurgen/            # Note: ‘hurgen’ is not a Python package.

This is a poor structure, because Python needs to be told about each of
‘foo’ and ‘bar’ separately if relative imports are to work. Worse, a
module invoked as the top-level module (e.g. ‘’) can't do imports
relative to itself, because it isn't even aware it's in a package!

Better is to structure the project explicitly under a top-level named
Python package::


and import all the project's modules relative to the ‘hurgen’ package.

More information about the Python-list mailing list