What exactly is the use of __init__.py? Yes, I know this file makes a directory into an importable package. However, consider the following example:
project/
foo/
__init__.py
a.py
bar/
b.py
If I want to import a into b, I have to add following statement:
sys.path.append('/path_to_foo')
import foo.a
This will run successfully with or without __init__.py. However, if there is not an sys.path.append statement, a "no module" error will occur, with or without __init__.py. This makes it seem lik eonly the system path matters, and that __init__.py does not have any effect.
Why would this import work without __init__.py?
import foopython needs something to import, so it basically imports the__init__file of that folder. - Tadhg McDonald-Jensen__init__.pyto doimport foo.bar. It will act similar to__init__method on a class. - freakish__init__.pyis required to make it work if you try just deleting the file then it will use the compiled__init__.pycfile probably located in a__pycache__subfolder, if you delete that as well then you will see that it is necessary. - Tadhg McDonald-Jensen