I am trying to import a submodule withing a seperate submodule using python. Here is my directory structure
I am trying to do this in process_qc.py
from package.database import database
d = database.Database('spark')
print(d.sparkSelect('SHOW DATABASES'))
It gives me error: ModuleNotFoundError: No module named 'package'
2 Answers 2
You can try doing this with relative imports:
from ..database import database
2 Comments
Python does not know where package exists when you use an absolute import. This is because Python first looks in the built-in modules and then at directories listed in sys.path for the requested import. If the import is not found, the current working directory is prepended to sys.path.
To use absolute imports, you should either:
- Execute the script from outside the
packagedirectory so that thepackagedirectory is discoverable from a path listed insys.path. - Add the package directory to your
PYTHONPATH:
$ export PYTHONPATH=$PYTHONPATH':path/to/package'
__init__.pyin thepackagedirectory.process_qc.pyfrom inside that script?