A package is a set of modules or sub-packages. A package is actually a directory containing either .py files or sub-directories defining other packages.
The dot (.) operator is used to describe a hierarchy of packages and modules. For instance, the module Bio.WWW.ExPASy is located in the file PYTHONHOME/site-packages/Bio/WWW/ExPASy.py. This module belongs to the Bio.WWW package located into the PYTHONHOME/site-packages/Bio/WWW/ directory.
When loading a package, the __init__.py file is executed. If the __init__.py defines classes, functions, etc... they become available at once, as shown in the following example:
Example 14.2. Using the Bio.Fasta package
>>> import Bio.Fasta
>>> handle = open("data/ceru_human.fasta")
>>> it = Bio.Fasta.Iterator(pin, Bio.Fasta.SequenceParser())
>>> seq = it.next()
>>> print seq.seq
>>> it.close()
However, loading a package does not automatically load the inner modules. For instance, even though the Bio.Fasta package directory contains the following files:
% ls Bio/Fasta
FastaAlign.py FastaAlign.pyc __init__.py __init__.pyc
this does not imply that importing the
Bio.Fasta package loads the
Bio.Fasta.FastaAlign module:
>>> import Bio.Fasta
>>> Bio.Fasta.FastaAlign.parse_file("data/ceru_human.fasta")
AttributeError: 'module' object has no attribute 'FastaAlign'
Issuing:
>>> from Bio.Fasta import *will however load the Bio.Fasta.FastaAlign, because this module is mentioned in the __all__ attribute in the Bio/Fasta/__init__.py file:
__all__ = [ 'FastaAlign', ]Other attributes of interest for packages and modules: