1

I am trying to import a submodule withing a seperate submodule using python. Here is my directory structure

enter image description here

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'

Rhythem Aggarwal
  • 346
  • 2
  • 15

2 Answers2

2

You can try doing this with relative imports:

from ..database import database
JoostVn
  • 242
  • 1
  • 9
1

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:

  1. Execute the script from outside the package directory so that the package directory is discoverable from a path listed in sys.path.
  2. Add the package directory to your PYTHONPATH:
$ export PYTHONPATH=$PYTHONPATH':path/to/package'
gmdev
  • 2,725
  • 2
  • 13
  • 28