Working with Modules
Python applications are organized through the use of modules. Modules are Python source files (or possibly compiled extensions) containing Python objects: class definitions, functions, and variables. Modules can be incorporated into other Python modules, or into the Python interactive shell directly, by importing them. This is done simply by adding an import statement:
When the above statement is executed, Python will search for a file named Foo.py (or Foo.pyc, or Foo.so) in the current import path defined in the variable sys.path. (This can be accessed in Python by typing: import sys; print(sys.path)
.) Directories listed in sys.path will be searched in order to find a matching file for import. A Python installation will construct its own internal sys.path based on where it is installed, and users can prepend additional directories to the default sys.path by specifying a colon-separated list of directories in the environment variable PYTHONPATH. Python assumes that the files in a given directory are modules if the directory contains a file called __init__.py (this file can be empty).
A second form of the import statement allows the caller to import only some of the names from a given module:
This will import only the objects named foo and bar from the Foo module.
Associated with each module is a namespace that contains all the objects contained within that module. Objects in a module's namespace are accessed via the dot (.) operator, the same way that methods attached to an object are accessed via the dot operator. Therefore, if a module Foo
contains a function foobar
, it would be accessed as Foo.foobar
within a module that imported Foo
. Namespaces help to disambiguate names in one module from identical names in other modules: Foo.foobar
is completely distinct from a function Bar.foobar
that exists in the Bar
module.
A third form of the import statement provides an evasion of the namespace rule by importing names from the module directly into the namespace of the caller. Thus, if module Foo contains an object named foobar, after issuing
a module will be able to refer directly to foobar
without specifying the Foo
namespace. This is generally dangerous and frowned upon because of the potential for name clashes, in which any object in module Foo
can overwrite an object in the current module with the same name. (For example, executing from numpy import *
, will — among other things — overwrite the Python built-in function sum
with the similarly named function in the numpy
module.) In addition, while an imported module can be reloaded (with some caveats, using the importlib.reload()
function), module contents that are splattered all over the current namespace with an from ... import * statement cannot be reloaded.
A module can also be imported with a substitute namespace, which is specified through the as
keyword; for example,
This allows subsequent code to refer to a name from the module Foo using the namespace f: f.foobar, rather than Foo.foobar.
Namespaces nest. If a module Foo
imports a module Bar
, which in turn defines an object foobar
, then a caller that issues
must specify an appropriately qualified identifier to access foobar:
Python packages provide a mechanism for automating some of this namespace nesting for a set of interrelated modules. In particular, if a directory contains a file named __init__.py
, then subdirectories can be imported via nested module names directly. For example, if the directory Foo
contains a file named __init__.py
and a subdirectory named Bar
, then one can execute a nested import as: