1

My question might have discussed in earlier posts too, but couldn't get proper answer applicable for my scenario. Hence posting it as a new query. I have multiple sub-folders where module tries to import modules present across multiple subfolders.

Folder Structure:

main
|-- lib
|   |-- lib1.py
|   |-- lib2.py
|   `-- lib3.py
|-- common
|   |-- lib4.py
|-- tests
|   |--folder1
|       |-- script1.py
|       |-- folder2
|           |-- script2.py
|   |--scriptA.py
|   `--scriptB.py

Use case/Requirements:

  1. script1 & script2 import functions from module lib1.py.
  2. lib1 wants to import functions from lib2.py & lib3.py
  3. lib4.py import funtions from lib1 & lib2

I tried adding blank __init__.py in root folder (main) and the all other subfolders. But couldn't get this working. Ending up with 'ModuleNotFound' error.

DanBrezeanu
  • 523
  • 3
  • 13
Anil
  • 41
  • 4
  • Its not `init.py` , it is `__init__.py` file. – Ajay A Sep 10 '20 at 11:48
  • Put the scripts you run in `main` and add a `__init__.py` to all folders except `main`. Then you can import like `from lib.lib1 import ...`. – Klaus D. Sep 10 '20 at 11:49
  • Refer to the following [answer](https://stackoverflow.com/a/50194143/10907391) for full details of how to structure a python package – DanBrezeanu Sep 10 '20 at 11:49
  • @KlausD. The script to be run are available in folder tests. Say Script1.py – Anil Sep 10 '20 at 12:48
  • The trick is to move them. – Klaus D. Sep 10 '20 at 13:23
  • @DanBrezeanu, i did follow the link, but it doesn't resolve my issue. Lib2.py is trying to import lib3 but when i execute it fails with ModuleNotFound:lib3 – Anil Sep 10 '20 at 15:51

1 Answers1

1

You need blank __init__.py files, not init.py. See the python documentation for more info.

Kraay89
  • 919
  • 1
  • 7
  • 19