Unable to import parent package modules inside child packages

I have a parent package with two child packages. Looks like this

backend
   __init__.py
   conf.py
   db.py
   connections.py
   /api
      __init__.py
      register.py
      api.py
   /scheduled
      __init__.py
      helpers.py

All the __init__.py files are empty.

The code in backend/connections.pyand is backend/conf.pyused by modules in both apiand packages scheduled.

in register.py I have code like

from backend.conf import *
from backend.connections import *

Now when i do python register.py i get this error

ImportError: No module named backend.conf

Also, when I changed from backend.conf import *to from ..conf import *or from .. import conf, I get this error

ValueError: Attempted relative import in non-package

What I understand from the error above is that python does not treat the above folders as packages. But I have __init__.pyin all the folders. What's wrong?

+5
source share
1 answer

python register.py backend/register.py __main__ , backend. , Python , backend, , , .

, , - python -m backend.register ( PYTHONPATH, ). script , .

+5

All Articles