Organising packages/modules - importing functions from a common.py in a separate directory?

Ben Finney ben+python at benfinney.id.au
Mon Oct 28 21:01:03 EDT 2013


Victor Hooi <victorhooi at gmail.com> writes:

> Ok, so I should be using absolute imports, not relative imports.

I'd say it is fine to use relative imports, so long as they are
explicit. (In Python 3, the default for an import is to be absolute, and
the *only* way to do a relative import is to make it explicitly
relative. So you may as well start doing so now.)

> Hmm, I just tried to use absolute imports, and it can't seem to locate
> the modules:
>
> In the file "foo_loading/em_load/sync_em.py", I have:
>
>     from common.common_bex import setup_foo_logging

So I'd recommend this be done with an explicit relative import:

    from .common.common_bex import setup_foo_logging

or, better, import a module:

    from .common import common_bex

or a whole package:

    from . import common

-- 
 \         “I went over to the neighbor's and asked to borrow a cup of |
  `\       salt. ‘What are you making?’ ‘A salt lick.’” —Steven Wright |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list