I need to better understand the difference between a normal directory and a Python package. I know this sentence present in the documentation:
The
__init__.pyfiles are required to make Python treat directories containing the file as packages.
To explain my doubt I'll show an example of directory structure:
study_import
  |-folder1
  |   |-b.py
  |-a.py
The content of the file study_import/a.py is:
import folder1.b
print(folder1.b.VAR2)
The content of the file study_import/folder1/b.py is:
VAR2="20"
The script a.py imports the module b.py from the directory folder1 and can be executed correctly. Its output is the printout of the number 20.
With the previous folder structure study_import and folder1 are not package because they don't contain the file __init__.py.
In this moment I don't understand the need to have a package because for example the instruction import folder1.b can be executed even the __init__.py there isn't present.
Until now I thought that packages were needed to correctly import modules.
Could someone help me to understand what is the difference between import a module from a folder and import a module from a package?
EDIT: I have followed the hint of Brian61354270 so I add 2 print() instructions in study_import/a.py:
import folder1.b
print(folder1.b.VAR2)
print(folder1)
print(folder1.b)
If I execute the script a.py the output is:
20
<module 'folder1' (namespace)>
<module 'folder1.b' from '/path/to/study_import/folder1/b.py'>
folder1 becomes a package
I add the file __init__.py in folder1 ===> folder1 becomes a package:
study_import
  |-folder1
  |   |-__init__.py
  |   |-b.py
  |-a.py
Now the execution of the script a.py produces the followed output:
20
<module 'folder1' from '/path/to/study_import/folder1/__init__.py'>
<module 'folder1.b' from '/path/to/study_import/folder1/b.py'>
Can someone explain the difference between 2 outputs?
