Table of Contents
A module is a component providing Python definitions of functions, variables or classes... all corresponding to a given specific thema. All these definitions are contained in a single Python file. Thanks to modules, you can reuse ready-to-use definitions in your own programs. Python also encourages you to build your own modules in a rather simple way.
In order to use a module, just use the import statement. Let us take an example. Python comes with numerous modules, and a very useful one is the sys module (sys stands for "system"): it provides information on the context of the run and the environment of the Python interpreter. For instance, consider the following code:
#!/local/bin/python import sys print "arguments: ", sys.argvSay that you stored in a modexa.py file, and that you run it like this:
./modexa.py 1 a seq.fastaThis will produces the following output:
arguments: ['./modexa.py', '1', 'a', 'seq.fasta']Explanation: By using the argv variable defined in the sys module, you can thus access to the values provided on the command line when launching the program. As shown in this example, the access to this information is made possible by:
You may also select specific components from the module:
from sys import argv
print "arguments: ", argv
In this case, you only import one definition
(the argv variable) from
the sys module. The other
definitions are not loaded.
You build your own module by creating a Python file. For instance, if the file ValSeq.py contains the following code (adapted from the Biopython ValSeq module):
Example 14.1. A module
# file Valseq.py
valid_sequence_dict = { "P1": "complete protein", \
"F1": "protein fragment", "DL": "linear DNA", "DC": "circular DNA", \
"RL": "linear RNA", "RC":"circular RNA", "N3": "transfer RNA", \
"N1": "other" }
def find_valid_key(e):
for key,value in valid_sequence_dict.items():
if value == e:
return key
you can use it by loading it:
import ValSeqwhere ValSeq is the module name. You can then access to its definitions, which may be variables, functions, classes, etc...:
>>> print ValSeq.valid_sequence_dict['RL']
linear RNA
>>> ValSeq.find_valid_key("linear RNA")
RL
Modules are mainly stored in files that are searched:
Files may be:
When importing a module, the interpreter creates a new namespace, in which the Python code of the module's file is run. The interpreter also defines a variable (such as sys, ValSeq, ...) that refers to this new namespace, by which the namespace becomes available to your program (Figure 14.2).
open
A module is loaded only once, i.e, a second import statement will neither re-execute the code inside the module (see Python reload statement in the reference guides), nor will it re-create the corresponding namespace.
When selecting specific definitions from a module:
>>> from ValSeq import find_valid_key
>>> find_valid_key("linear RNA")
RL
the other components stay hidden. As illustrated in Figure 14.2, no new namespace is created, the
imported definition is just added in the current name
space.
This can causes errors if the definition that is imported needs to access to other definitions of the module, e.g:
>>> print valid_sequence_dict['RL'] NameError: name 'valid_sequence_dict' is not defined >>> print ValSeq.valid_sequence_dict['RL'] NameError: name 'ValSeq' is not defined
You can also load "all" the components from a module, which makes them available directly into your code:
>>> from ValSeq import *
>>> find_valid_key("linear RNA")
You probably did this many times in order to use
the string module's definitions,
right? The result of:
>>> from string import *is that all the definitions of the module are copied in your current namespace.
![]() | Caution |
|---|---|
You can restrict the components being imported by an import * statement. The __all__ variable, also used for packages (Section 14.2), can explicitly list the components that should be directly accessible (see Exercise 14.4). | |
When the file of a module is run from the command line (instead for being imported):
% python ValSeq.pythe module does not behaves like a module anymore. It is, instead, run within the default __main__ module (i.e not the ValSeq module):
% python -i ValSeq.py
>>> ValSeq.find_valid_key("linear RNA")
NameError: name 'ValSeq' is not defined
>>> find_valid_key("linear RNA")
'DL'
For this reason, the code executed when the module is loaded
(e.g: either with import or from the command line)
can be made dependent of its current name by testing this
name. The current module's name is stored in a special
purpose variable __name__:
if __name__ == '__main__':
# statements that you want to be executed only when the
# module is executed from the command line
# (not when importing the code by an import statement)
print find_valid_key("linear RNA")