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__.pycthis 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:
Exercise 14.2. Bio.SwissProt package
Which import statements are necessary to make the following code work?
expasy = ExPASy.get_sprot_raw('CERU_HUMAN') sp = SProt.Iterator(expasy, SProt.RecordParser()) record = sp.next() print record.keywords
Exercise 14.3. Using a class from a module
Why does the following code issue an error?
from Bio.SubsMat import FreqTable dict = ... # whatever f = FreqTable(dict, 'COUNT') TypeError: 'module' object is not callable