I am trying to structure Python project with different modules and having the test script for each module. My project directory looks something as shown below:
├── project
│ ├── main.py
├── F1
│ ├── add.py
| └── prin_add.py
main.py
from f1.add import *
if __name__ == "__main__":
add(4,5)
add.py
from print_add import *
def add(a,b):
print_add(a+b)
if __name__ == "__main__":
add(4,5)
print_add.py
def print_add(c):
print(f'sum = {c}')
If I run the main.py, I get an error ModuleNotFoundError: No module named 'print_add'
. I think this is due to the absolute and relative path of the modules. How can I have the modules imported using a relative path? If I use something like from .print_add import *
in add.py file, I get ModuleNotFoundError: No module named '__main__.print_add'; '__main__' is not a package
while running add.py file.
What is the best practice to structure Python modules? I am using Python 3.6 and I don't think I need to have init.py file.