0

I have seen several questions related to this error but I think they are different cases. My Django project has 2 applications and a directory structure like this and I'm facing an issue with relative imports I don't understand the logic.

Python allows me to import market from file my_app2/signals.py but it returns ValueError: attempted relative import beyond top-level package if I import portfolio from file my_app1/signals.py. What is the reason and how can I found a wordaround?

/my_project
  /my_project
    __init__.py
  /my_app1
    market.py
    signals.py  #  Here I can't import portfolio.py
  /my_app2
    portfolio.py 
    signals.py  #  Here I can import market.py

my_app1/signals.py

from ..my_app2 import portfolio #  doesn't work
ValueError: attempted relative import beyond top-level package

from my_project.my_app2 import portfolio #  doesn't work
ModuleNotFoundError: No module named 'my_project.my_app2'

my_app2/signals.py

from ..my_app1 import market #  works
Florent
  • 1,791
  • 22
  • 40
  • https://stackoverflow.com/questions/30669474/beyond-top-level-package-error-in-relative-import – sleepyhead Apr 30 '20 at 17:00
  • I'm afraid this is not applicable to my case, `sys.path.append(".")` or `sys.path.append("..")` doesn't solve the issue. – Florent Apr 30 '20 at 17:43

1 Answers1

1

I finally solved the issue without the use of from, it's not clear for me what was causing the error.

import my_app2.portfolio as portfolio
Florent
  • 1,791
  • 22
  • 40