Relative Imports, why the hell is it so hard?

skip at pobox.com skip at pobox.com
Mon Mar 23 15:44:03 CET 2009


    >> 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.

However, C++ != Python.  Regardless whether or not you can "make it work",
translating idioms from one language to another is often suboptimal.  That
may not be the case here, but it bears keeping in mind.

    >> 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. :(

You might try removing a dot from your import statement.  Python is also not
Unix.  Popping up one level in the package hierarchy is done with ".", not
"..".

-- 
Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/



More information about the Python-list mailing list