I am new to python. Although the language is clean and easy to learn, I am finding a hard time understanding the mechanics of imports. I have searched a lot about it on the web, most of the articles are not comprehensive and are rather confusing for a beginner like me. So, once for all, I want to ask the experts here to help me understand this concept.
Python3 --version = Python 3.10.1
System - Mac M1 (2020)
Problem 1 - I have the following folder structure -
Package1/
/__init__.py
/module1.py
/module2.py
/SubPackage1/
/__init__.py
/subpack1_module1.py
/SubPackage2/
/__init__.py
/subpack2_module1.py
Now I want to import the entire SubPackage2 in SubPackage1 to use its modules. This is the code I am writing in the '__init__.py
' file of SubPackage1
from Package1 import SubPackage2
This is showing me the following error -
ModuleNotFoundError: No module named 'Package1'
What is the right way to import one sub-package into another sub-package? Although I have found a cheat to this as follows -
import sys
sys.path.append('root/Package1/')
import SubPackage2
But I seriously feel that this method is a noob method and is not scalable.
Problem 2 - Now let's say I have completed the above package and want to use it in another Folder/Package/Program, in the same way, we use Pandas by just writing import Pandas as pd. What is the best way? To be more clear let me define the folder structure -
Package 1/
/__init__.py
/all other subpackages and modules
Folder 2/
/test.py
Now in test.py I want to use Package1. So I want to write on test.py -
import Package1
But again it gives me the same ModuleNotFoundError. sys.path.append('path') trick works but it is not scalable. Another way is to create wheels (using setup.py), but in that case, how to use wheels in development mode so that I don't have to create wheels every time there is a change in the version.