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
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
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.