Skip to content Skip to sidebar Skip to footer

Unable To Import Module From Another Package

I have a directory structure like this conf __init__.py settings.py abc.conf def.conf src main.py xyz.py src I chose not to make a package but a regular fo

Solution 1:

When importing python search current directory and the sys.path. Since your main.py is in src folder it cannot see the conf package folder. Luckily you could update sys.path at runtime.

root
     conf
        __init__.py
        settings.pysrcmain.py

So you could append sys.path from main.py before importing conf module. Try following:

# main.py
import os, sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))

from conf import settings
...

The other way is to update PYTHONPATH directly and add path to your script root directory.

Post a Comment for "Unable To Import Module From Another Package"