Importing modules
In Python much functionality is inbuilt and immediately available, as has been
demonstrated in this chapter. However, one of the fundamentals of the Python language is
the ability to import external modules (or libraries) into the current program. Naturally, we
do this to make use of extra functionality that is available elsewhere. Such modules may
be part of the standard library (see
http://www.cambridge.org/pythonforbiology
for links
to the Python library documentation) that is automatically included with the Python
installation, they may be extra libraries which you may have to install separately (such as
NumPy or BioPython) or they may be other Python programs you have written yourself.
Whatever the source of the module, they are imported into a program via an import
command. For example, if we wish to access the mathematical constants π and e we can
use the import keyword to get the module named math and access its contents with the dot
notation:
import math
print(math.pi, math.e)
Also we can use the as keyword to give the module a different name in our code, which
can be useful for brevity and avoiding name conflicts:
import math as m
print(m.pi, m.e)
Alternatively we can import the separate components using the from … import
keyword combination:
from math import pi, e
print(pi, e)
Because the math module used above was part of the standard Python library we didn’t
have to worry about installing it separately. The standard library includes a selection of
modules that are useful in a variety of general situations. Many of these modules are listed
in the Appendices at the end of the book.
If we import a module that is not part of the standard library, then we must make sure
that it is installed on our computer system (and there are generally instructions at the
download sites). If it is not installed we will get an ImportError. For example, if the
Python Imaging Library (PIL) is not installed but we try to import its Image object we
would see something like:
>>> from PIL import Image
Traceback (most recent call last):
File "", line 1, in
from PIL import Image
ImportError: No module named PIL
Lastly, if you want to write your own modules which can be imported into other
programs then you need to be aware of how Python searches for module files. Essentially
Python has a series of import directories (a ‘search path’) that it looks inside to find a file
of the required name, starting with the inbuilt modules. If we try to import moduleAbc
then Python will look for the module file named moduleAbc.py in its import directories.
By default the import search path will include several directories, which contain the
standard libraries, external installations and the current working directory. Within Python,
sys.path gives the list of what is on the search path:
>>> import sys
>>> print(sys.path)
You will see that this list contains an empty string ” for the current directory and
various other standard ‘lib’ and ‘packages’ directories that are part of the Python
installation. If you have a Python file that you want to import that is not in one of these
directories, then you can add further directories to sys.path and then refer to modules
inside that directory:
import sys
sys.path.append('/home/user/myModules/') # Contains userModule.py
from userModule import userFunction
Alternatively you can add entries to the PYTHONPATH environment variable via your
computer’s operating system, which will automatically be added to sys.path at run time. It
is in this way that you can use all of the downloadable Python code that accompanies this
book. Hence, if you download the material in its standard directory and put the full path
(i.e. including any leading directories) to this in sys.path or the PYTHONPATH
environment variable then you will be able to import any of the example code, such as:
from MachineLearning import neuralNetTrain, neuralNetPredict
Any sub-directories of the import directories listed in sys.path can also be treated as if
they were modules, although before Python 3.3 this is provided they contain a file named
‘__init__.py’, and which is typically blank or contains only ‘pass’: a Python statement that
does nothing. For example, if I have a personal module directory located at
‘/home/user/myModules/’, which is on the module search path, I can then add a sub-folder
called ‘molecules/’ that contains the script ‘anneal.py’ as well as any required
‘__init__.py’ (for Python before version 3.3) so that I can do the following import:
from molecules import anneal
1
The term hash symbol is used outside America; in America the term pound symbol or
number sign is often used instead.
2
In Python 2.6 and 2.7 you can get the Python 3 behaviour for print using a special
statement: from __future__ import print_function.)
3
These existed in Python 2.2 but were then just synonyms for the integers 1 and 0. The
Boolean type was properly introduced in Python 2.3.
4
From Python 2.2 onwards, integers and long integers are treated uniformly, so most
people can happily ignore the difference between the two types. Before that long integers
had to have an ‘L’ at the end of the number, for example, 12345678901234L.
Do'stlaringiz bilan baham: |