1

I try to import a variable from another module by relative import path. Alas, is gives me an error:

C:\Python35\python.exe D:/Projects/baka_proj/pack2/mod2.py
Traceback (most recent call last):
  File "D:/Projects/baka_proj/pack2/mod2.py", line 1, in <module>
    from ..pack1.mod1 import some_vars_to_import
SystemError: Parent module '' not loaded, cannot perform relative import

The project has such a structure:

proj_root
    __init__.py (empty)
    package1
        mod1.py (here is a variable some_vars_to_import)
        __init__.py (empty)
    package2
        mod2.py (here I try to import by relative path)
        __init__.py (empty)

In mod2.py I try to import a variable from mod1.py:

from ..pack1.mod1 import some_vars_to_import

And it fails.

Can you tell me, what's wrong with my code?

P.S. https://www.sendspace.com/file/zwmx0v -- here is the project.

Felix
  • 3,351
  • 6
  • 40
  • 68
  • 2
    Does every directory have `__init__.py` in it? – vishes_shell May 08 '17 at 21:07
  • `__init__.py`? Yes, it has. Also, I uploaded it to SendSpace: https://www.sendspace.com/file/zwmx0v – Felix May 08 '17 at 21:08
  • 1
    Does `proj_root` had `__init__.py`? – Matt Williamson May 08 '17 at 21:11
  • 1
    Please show the `__init__.py`s in your tree, otherwise it is incomplete. – Mad Physicist May 08 '17 at 21:12
  • http://imgur.com/a/7BMfS — here is a screenshot of my project dir. The link to the archive with the project is attached to the post too. – Felix May 08 '17 at 21:14
  • 2
    @Felix Don't upload images, just include the relevant files (including necessary content) in your question. That way the question will be useful for future readers even if the links go stale. – MSeifert May 08 '17 at 21:16
  • See the Q&A I've linked. It is not actually about relative imports, but the cause of your problem (and the solution) is identical. – wim May 08 '17 at 21:35

0 Answers0