In this part of the Python programming tutorial, we will talk about packages.
A package is a collection of modules which have a common purpose. Technically a package is a directory which must have one special file called
There are several ways to manage Python code:
The constants directory has two files.
We can also create subpackages. To access subpackages, we use the dot operator.
In this chapter, we have covered packages in Python.
A package is a collection of modules which have a common purpose. Technically a package is a directory which must have one special file called
__init__.py
. There are several ways to manage Python code:
- functions
- classes
- modules
- packages
In our current working directory we have a constants directory and a read.py script.
read.py
constants/
__init__.py
names.py
The constants directory has two files.
__init__.py
file makes constants a Python package. The names.py is an ordinary module. from names import namesThe
print "initializing constants package"
__init__.py
file is initialized when the package is imported. Here we can control, what object will be available to the module, which imports the package. #!/usr/bin/pythonThe names.py module has a tuple of 5 names.
# names.py
names = ('Jack', 'Jessica', 'Robert', 'Lucy', 'Tom')
#!/usr/bin/pythonFinally, the read.py script imports the constants package. We print the names tuple from the package.
# read.py
import constants
print constants.names
$ ./read.py
initializing constants package
('Jack', 'Jessica', 'Robert', 'Lucy', 'Tom')
We can also create subpackages. To access subpackages, we use the dot operator.
read.pyThis is the new hierarchy. We have a subpackage called numbers.
constants/
__init__.py
names.py
numbers/
__init__.py
integers.py
from integers import integersThe __init__.py file in the numbers package has this one line.
#!/usr/bin/pythonThe integers module defines a tuple of 7 integers. This tuple will be accessed from the read.py script.
# integers.py
integers = (2, 3, 45, 6, 7, 8, 9)
#!/usr/bin/pythonThe read.py script is modified a bit.
# read.py
import constants
import constants.numbers as int
print constants.names
print int.integers
$ ./read.py
initializing constants package
('Jack', 'Jessica', 'Robert', 'Lucy', 'Tom')
(2, 3, 45, 6, 7, 8, 9)
from package import *
construct in a __init__.py file could cause problems on some older operating systems. Therefore a special variable __all__
has been introduced. This variable controls what objects will be imported from a package. __all__ = ["names"]Here we modify the __init__.py file in the constants directory.
print "initializing constants package"
#!/usr/bin/pythonWe also change the read.py script accordingly.
# read.py
from constants import names
import constants.numbers as int
print names.names
print int.integers
$ ./read.pyThe output is the same.
initializing constants package
('Jack', 'Jessica', 'Robert', 'Lucy', 'Tom')
(2, 3, 45, 6, 7, 8, 9)
In this chapter, we have covered packages in Python.
No comments:
Post a Comment