importing modules

Richard Lamboj richard.lamboj at bilcom.at
Fri May 7 08:25:40 EDT 2010


Am Friday 07 May 2010 13:50:15 schrieb Jean-Michel Pichavant:
> Richard Lamboj wrote:
> > Hello,
> >
> > I have a question about importing python modules.
> >
> > I have modul package, with submodules. So how can a  submodul access a
> > modul that is on level upper?
> >
> > Is there something like "import ../../blah"? I don't mean something like
> > this: "import bla.blub.moep"
> >
> > Kind Regards,
> >
> > Richi
>
> I would advise to use absolute imports whenever possible (always ?).
> If 'pkg' is your package, then module sub1 should import sub2 that way:
>
> file pkg/sub1.py:
>
> import pkg.sub2
>
>
> see http://www.python.org/dev/peps/pep-0328/#rationale-for-relative-imports
>
> " the python-dev community chose absolute imports as the default because
> they're the more common use case and because absolute imports can
> provide all the functionality of relative (intra-package) imports --
> albeit at the cost of difficulty when renaming package pieces higher up
> in the hierarchy or when moving one package inside another."
>
> You can still use relative import if you want, they've been implemented
> for a purpose, but I woudl highly discourage the ambiguous relative
> imports of python 2.4.
>
> JM

Hello,

I always use absolute imports, but sometimes i does not work.

I got: ImportError: No module named mail. When i import it from top of the 
module it works. If i import it from a level down, it works not always - 
strange?

Kind Regards,

Richi



More information about the Python-list mailing list