Before Python3.3, every package must contain a __init__.py
.
It's run automatically the first time a Python program imports this package.
Although after Python3.3, __init__.py
is no longer a must, using __init__.py
still provides a performance advantage.
All the code in the directory's __init__.py
is run automatically.
Can be used to initialize the state
e.g. initialize file to create required data files, open connections to databases
Declare that a directory is a Python Package
In fact, __init__.py
are often empty in practice
You can use __all__
list in __init__.py
to define what is exported when this directory is imported using from *
statement
# c.py
x = 1
y = 2
__all__ = [x]
from c import *
print(x)
print(y) # not imported since it's not in __all__
1
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-04b1c5a4d589> in <module>() 2 3 print(x) ----> 4 print(y) # not imported since it's not in __all__ NameError: name 'y' is not defined
How it works is version dependent
The directory used relative import can no longer be used as executable program.
from . import mod1
from .mod1 import name
from .. import mod2
from . import a
--------------------------------------------------------------------------- SystemError Traceback (most recent call last) <ipython-input-2-416c9517c91b> in <module>() ----> 1 from . import a SystemError: Parent module '' not loaded, cannot perform relative import
# code/pkg/spam.py
import string
print(string)
# code/pkg/string.py
print('Ni' * 8)
$ python3
>>> import pkg.spam
<module 'string' from '/usr/Python34/lib/string.py'>
$ python2
>>> import pkg.spam
NiNiNiNiNiNiNiNi
<moduel 'pkg.string' from 'pkg/string.py'>
# code/string.py
print('string' * 8)
# code/pkg/string.py
print('Ni' * 8)
# code/pkg/spam.py
import string
print(string)
$ python3
>>> import pkg.spam
stringstringstringstringstringstringstringstring
<module 'string' from './string.py'>
$ python2
>>> import pkg.spam
NiNiNiNiNiNiNiNi
<module 'pkg.string' from 'pkg/string.pyc'>
# Use absoulte import in Python2
from __future__ import absolute_import
Must choose a single mode - package(relative imports) or program(simple imports)
from . import mod # Not allowed in nonpackage in both Python2 and Python3
import mode # Does not search file's own directory in package in Python3
An extension to the import model.
When Python finds directories without finding __init__.py
, it creates a namespace package that is the virtual concatenation to all found directories having the requested module name.
# ns/mod.py (without __init__.py)
import ns.mod
This is mod.py
ns
<module 'ns' (namespace)>
ns.__path__
_NamespacePath(['C:\\Users\\smszw\\Desktop\\zw-public\\code\\Learning_Python\\Part 5 - Modules and Packages\\ns'])
__init__.py
(Since it stops the following algorithm)directory/module/__init__.py
is found, a regular package is importeddirectory/module.{py, pyc ...}
is found, a simple module is importeddirectory/module
is found and is a directory, it's record and the scan continues with the next directory in the search pathIf the search path scan completes without returning a module or package by steps 1 or 2, and at least one directory was recorded by step 3, than a namespace package is created.
Once a namespace package is created, there is no difference between it and a regular package.
Many packages require no initialization code.
Thus, they are no longer required after Python3.3
However, there is performance advantage to have one.
With namespace packages, all entries in the path must be scanned.
Compare to it, regular packages stop the algorithm at step 1.