Python modules

Ben Finney ben+python at benfinney.id.au
Tue Nov 11 23:18:50 CET 2014


Will Acheson <willyach07 at gmail.com> 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
them::

    hurgen/            # Note: ‘hurgen’ is not a Python package.
        foo/
            __init__.py
            spam.py
            beans.py
        bar/
            __init__.py
            eggs.py
            ham.py

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. ‘eggs.py’) 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::

    hurgen/
        __init__.py
        foo/
            __init__.py
            spam.py
            beans.py
        bar/
            __init__.py
            eggs.py
            ham.py

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




More information about the Python-list mailing list