1

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__.py files 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?

frankfalse
  • 1,553
  • 1
  • 4
  • 17
  • 4
    Run `import folder1.b` and compare `print(folder1)` to `print(folder1.b)`. You've discovered the difference between (import) packages and [implicit namespace packages](https://peps.python.org/pep-0420/). – Brian61354270 Jul 24 '23 at 14:54
  • Does this answer your question? [How do I create a namespace package in Python?](https://stackoverflow.com/questions/1675734/how-do-i-create-a-namespace-package-in-python) Also [Namespace vs regular package](https://stackoverflow.com/q/21819649/11082165) and [Python Namespace Packages in Python3](https://stackoverflow.com/q/41621131/11082165) – Brian61354270 Jul 24 '23 at 14:57
  • @Brian61354270 So packaging is linked only with namespace and not to import instruction? – frankfalse Jul 24 '23 at 15:00
  • Until now I was convinced that packages are needed to have a correct import but the example in my question seems to tell the opposite. – frankfalse Jul 24 '23 at 15:07
  • I'm not sure I understand what you mean. Packages are needed for imports (by definition). However, Python let's you implicitly define namespace packages by placing directories _without_ `__init__.py` files on the Python path. Namespace packages behave differently from "normal" import packages in several key ways, so it's worth figuring out which you actually want – Brian61354270 Jul 24 '23 at 15:10
  • @Brian61354270 I think that until now I have used packages many times instead normal directory, but in many contexts they were not necessary. – frankfalse Jul 24 '23 at 15:17

0 Answers0