[Tutor] relative imports within a package?

James Hartley jjhartley at gmail.com
Sat Nov 7 17:09:19 EST 2015


The Python 3 tutorial discusses relative imports at:

https://docs.python.org/3/tutorial/modules.html#intra-package-references

I have the following directory structure for a package in development:

+ outer_package/
     + __init__.py
     + inner_package
     |     + __init__.py
     |     + myclass.py
     + collateral_directory
           + arbitrary_tool.py

arbitrary_tool.py needs to instantiate the class MyClass found in
myclass.py.  I have made MyClass accessible by adding the following to
inner_package/__init__.py:

from myclass import MyClass

Within arbitrary_tool.py, I have attempted the following:

from ..inner_package import MyClass

...but executing with Python 3.4 yields the following stack trace:

Traceback (most recent call last):
  File "./arbitrary_tool.py", line 5, in <module>
    from ..inner_package import MyClass
SystemError: Parent module '' not loaded, cannot perform relative import

Suggestions on how to solve this would be most certainly welcomed!


More information about the Tutor mailing list