简体   繁体   中英

Import parent module from submodule

I'm having an import error I don't understand, and haven't been able to find a solution in other SO questions:

I have this django structure:

  • project_root/
    • apps/
      • __init__.py
      • my_app/
        • __init__.py
        • models.py
        • views.py
        • reports/
          • __init__.py
          • R1.py
          • R2.py

In either of R1.py or R2.py the statement

from apps.my_app.models import MyModel

throws an ImportError. I know MyModel exists... I import it exactly the same way in views.py.

In apps.my_app.reports.__init__.py I have the lines:

from R1 import R1_Report
from R2 import R2_Report

since RX_Report classes are defined in each of those files.

That's all the potentially relevant info I can think of... any ideas?

Ahh... nevermind. It was a circular import. Solved it by switching

from apps.my_app import reports
...
reports.stuff()

to

import apps.my_app
...
apps.my_app.reports.stuff()

in models.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM