Objectives:
Upon completion of this lesson, you should be able to
State the purpose of modules and packages in Python
Discuss the concepts of namespace and variable scope
Import modules and libraries and use imported functions
A Module is a python file that contains a collection of related definitions.
So congratulations -- you already wrote a module or two without knowing it!
Python has hundreds of standard modules
These are organized into what is known as the Python Standard Library (
http://docs.python.org/library/)
To bring common functionality together under the common namespace.
It is very similar to how functions
were used to encapsulate commonly reused code, or how classes
encapsulate commonly reused functions and data structures.
modules
in turn encapsulate everything (functions, classes, etc) under one common "roof".
Unlike instances of classes
, modules exist in a single copy: if I import the same module twice in the same program, it will remain the same exact module.
To use functionality from a module, you first have to import the entire module or parts of it into your namespace
To import the entire module, use
import module_name```
statement, e.g.
import sys
you can also import multiple modules in a single line, although such practice is generally discouraged by PEP8:
import os, sys
and name them differently if desired:
import os as builtin_os, sys
print builtin_os
and it will be exactly the same os
module:
print os is builtin_os
print os.system
This, what looks a minor feature, provides a great opportunity to organize namespaces to avoid collisions among definitions (compare to Matlab).
Excercise #1
Remember functions lecture? We have used global
keyword to tell Python that variable is from the "global" namespace. Let me reveal you two built-in functions: locals() and globals().
Question: what structure type do they return?
Question: how they are different in the current session? (recall useful data type set
)
Excercise #2
Import few built-in modules (e.g. os, sys, glob), and inspect via completion or using command dir()
which definitions they provide.
Appreciate the fact that your "locals" or "globals" didn't become bloated.
To import specific definitions (e.g. functions, variables, etc) from the module into your local namespace, use
from module_name import name1, name2
which will make those available in your namespace
directly:
from os import system, chmod
print system
but you must be using such imports with care since then it might "overload" your previously defined or imported definitions in case of names collision:
# there is a builtin sum function, which you can access also unambigously from __builtin__ module
print __builtin__.sum
# but numpy also provides its own sum
from numpy import sum
print sum
you can overcome such a problem by importing with as again:
from os.path import join as pathjoin
print pathjoin
while choosing the name carefully so you guaranteed to not overlap with some other name.
As you can see from above example, those modules could be nested, so path
is a "sub-module" of os
module:
print os
print os.path
print os.path.join
Even more discouraged generally practice is to import everything from a module, e.g.
from os import *
It is discouraged because it
Restricts the variable names you can use: os.name
might override name
, or vise-versa.
Creates possible name clashes between modules.
Makes the code harder to read and understand: where do symbols come from?
Makes it impossible to guess the functionality by the context and the name (hint: os.name
is the name of the OS), and to profit usefully from tab completion.
Makes the code impossible to statically check for undefined symbols.
So, even if your favorite PyMVPA author/tutorial tells you to just do
from mvpa2.suite import *
better be cautious and do
import mvpa2.suite as mv
to access its functionality via mv.
prefix, e.g. mv.Dataset
Whenever internally modules are organized hierarchically, and could be accessed correspondingly separating levels with ".", above mvpa2.suite
example shows that some modules provide an interface to access all (selected) the functionality without traversing hierarchy.
Formally packages are collections of modules brought into the same namespace. Usually it is done by creating a directory containing __init__.py
and containing all the modules belonging to that package. You can access __package__
attribute of a module to see which package it originally belonged.
And each package is actually presents as its own ```init.py`` module:
import email
print email
print email.__package__
print email.parser
print email.parser.__package__
And packages can be nested within
print email.mime
print email.mime.__package__
print email.mime.base.__package__
!tree /usr/lib/python2.7/email/
Practically you can consider simply consider every package to to be a module ;-)
Standard library
is a collection of modules which comes with stock Python. It is quite reach in terms of functionality, and that is why Python is said to have "batteries included" philosophy.
Excercise
Visit and explore the Brief Tour of the Standard Library describing available modules, some of which we have already used.
Import and try to use functionality of the re and glob modules.