Python Forum
Do packages really require "__init__.py" ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do packages really require "__init__.py" ?
#1
Hello,

In the official documentation for Python 3 it is written that :

Quote:The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

However, when a create a package without the presence of the file "__init__.py", it "works". I means that Python knows that it is a package even if the file "__init__.py" does not exist.

Demonstration:

Let's consider the following file tree:

├── package
│   ├── __init__.py
│   ├── module1.py
│   ├── module2.py
│   └── module3.py
└── test1.py


And let's consider the following code (the content of the file test1.py):

import os
import sys
from pprint import pformat

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

import package
print('What is a package ? %s' %pformat(package))
print('Do packages have a property called "__path__" ? %s' %('yes' if hasattr(package, '__path__') else 'no'))
print('package.__path__ = %s' %package.__path__)
print('')

from package import module1
print('What is a module ? %s' %pformat(module1))
print('Do modules have a property called "__path__" ? %s' %('yes' if hasattr(module1, '__path__') else 'no'))
print('')
Now run the script test1.py:

With the file "package/__init__.py":

Output:
$ ls package/__init__.py && python3 test1.py package/__init__.py What is a package ? <module 'package' from '/home/denis/projects/imap/package_vs_module/package/__init__.py'> Do packages have a property called "__path__" ? yes package.__path__ = ['/home/denis/projects/imap/package_vs_module/package'] What is a module ? <module 'package.module1' from '/home/denis/projects/imap/package_vs_module/package/module1.py'> Do modules have a property called "__path__" ? no
And without the file "package/__init__.py":

Output:
$ rm -f package/__init__.py && rm -rf package/__pycache__ && python3 test1.py What is a package ? <module 'package' (namespace)> Do packages have a property called "__path__" ? yes package.__path__ = _NamespacePath(['/home/denis/projects/imap/package_vs_module/package', '/home/denis/projects/imap/package_vs_module/package']) What is a module ? <module 'package.module1' from '/home/denis/projects/imap/package_vs_module/package/module1.py'> Do modules have a property called "__path__" ? no
Conclusion:

Python recognises "package" as being a package event without the presence of the file "__init__.py".

Do I misunderstand something ?

Thanks,

Denis
Reply


Messages In This Thread
Do packages really require "__init__.py" ? - by denis_beurive - Feb-14-2019, 04:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't list require global keyword? johnywhy 9 841 Jan-15-2024, 11:47 PM
Last Post: sgrey
  Pattern Require Last Line Print() why? Harshil 4 2,446 Aug-08-2020, 04:54 PM
Last Post: Harshil
  Require Some Suggestions gouravlal 2 1,895 Jul-27-2020, 06:14 AM
Last Post: gouravlal
  Do you require imports used in other files? Panda 1 2,518 Jul-14-2018, 02:30 PM
Last Post: gontajones
  Why does function print require bracket for text? richardm55 2 2,993 May-23-2018, 07:30 PM
Last Post: buran
  I Require some help for project idea...FaceRecognizer Door actived JavierGTZ 0 2,492 Aug-18-2017, 04:45 PM
Last Post: JavierGTZ
  Why is there an __init__ statement within the __init__ definition iFunKtion 7 6,046 Feb-06-2017, 07:31 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020