Feb-14-2019, 04:19 PM
(This post was last modified: Feb-14-2019, 04:20 PM by denis_beurive.)
Hello,
In the official documentation for Python 3 it is written that :
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):
Now run the script test1.py:
With the file "package/__init__.py":
Python recognises "package" as being a package event without the presence of the file "__init__.py".
Do I misunderstand something ?
Thanks,
Denis
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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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 ('') |
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