Relative Imports, why the hell is it so hard?

Maxim Khitrov mkhitrov at gmail.com
Mon Mar 23 15:28:42 CET 2009


On Mon, Mar 23, 2009 at 10:16 AM, CinnamonDonkey
<CinnamonDonkey at googlemail.com> wrote:
> Hi All,
>
> I'm fairly new to Python so I still have a lot to learn. But I'd like
> to know how to correectly use relative imports.
>
> Please, please... please! don't go off on rants about why you think
> relative imports should not be used. I've got 15+ years in C++ and
> relative inclusion of other sections of code has never been a problem.
> As far as I am concerned what I am trying to do is perfectly
> reasonable and valid.
>
> Thank you in advance to everyone who helps solve this, because I just
> don't get it.
>
> Example:
>
> \ App
> |   main.py
> +--\subpack1
> |   |   __init__.py
> |   |   module1.py
> |
> +--\subpack2
> |   |   __init__.py
> |   |   module2.py
>
>
> Module1 needs to access functionality in Module2.
>
> #module1.py
> from ..subpack2 import module2
>
> Seems reasonable to me... but it just does not work and I was so
> liking Python. :(

Relative imports are perfectly fine, in my opinion. Do you have "from
__future__ import absolute_import" at the top of module1.py? Should
work fine once you add that line.

- Max



More information about the Python-list mailing list