3

I have got a problem when using python imports. I wrote a finished module, that itself uses several submodules (those are imported).

e.g.

module:
 main_class.py
 submodule1.py
 ....

Now I want to use this finished module by another supermodule, so the folder structure would change like this

supermodule:
 main_class_super.py -- this class imports module.main_class
 module:
 main_class.py
 submodule1.py
 ....

However now all imports that are used in the code of main_class.py inside the module fail (I guess because the import now works in the namespace of main_class_super.py)

Any idea how to solve this problem without restructuring the entire sources?


The concrete error:

In my main_class.py I use the line:

import submodule1

In my supermodule.py I use the line:

import module.main_class

When executing the superclass that imports module.main_class of course the import submodule1 line is executed as well, but fails as it can not find the module in the namespace of supermodule.py.

nalzok
16.4k24 gold badges85 silver badges159 bronze badges
asked Aug 10, 2016 at 12:35
0

1 Answer 1

5

If you are on python 2 you should add from __future__ import absolute_import to your files (not needed on 3) so you can do the imports like Guido states in PEP 328

According to this you should

  • Make sure all your package folders have a __init__.py in it to mark them as importable

  • In main_class.py: replace import submodule1 or import module.submodule1 with from . import submodule1

  • In main_class_super.py: replace import module.main_class with from .module import main_class

This way you don't have to care about any outer package structure.

The option to use absolute and explicit relative imports was added in Python 2.5.

answered Aug 10, 2016 at 12:59
Sign up to request clarification or add additional context in comments.

4 Comments

Er, i think i do not quite get how this can help me. Given my folder structure, how would i do the import of the submodule1.py inside the supermodule.py?
Sorry to bother you again, by i encountered another problem. Inside those submodules several modules import the same other modules (e.g. submodule1 imports submodule2 and 3 and submodule2 also imports submodule3. Then this import inside submodule2 wont work. (even though i changed it to from . import submodule3). All other imports just work fine
Sorry, without directory layout (what files are in what folders?) i cannot help on that
Structure is the same, only that there is not only submodule1.py on the lowest level but also submodule2.py and submodule3.py.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.