The Python Standard Library and Packages
This section introduces the importing of modules from the Python standard library and the importing of external code libraries (packages).
The Standard Library
Like many programming languages, Python has an extensive standard library with many built-in data types, constants, functions, and modules included. We have encountered some of these already, and this section gives a very short introduction to some additional aspects of the library.
Some of the standard library is available in the built-in namespace,
such as the constants True
, False
, and
None
and
the functions print()
, len()
, and
type()
.
However, much of the standard library requires the import
ing of
modules. A list of modules of particular interest to the engineer is
given in tbl. 1.
Module | Description |
---|---|
math |
Math constants and functions for integers and real numbers. |
cmath |
Math constants and functions for integers, real numbers, and complex numbers. |
random |
Functions for generating pseudorandom numbers. |
os |
Functions for interacting with the computer’s operating system and file system. |
pathlib |
Classes for representing file paths in an operating-system independent way. |
json |
Functions for importing and exporting data in the universal JSON format. |
pickle |
Functions for saving and loading objects to files in serialized (compact) form. |
Just as with our own modules, we can import
a module
from the standard library with
import math
and the related variations of import
. The
standard library modules are always in the Python search path, which is a list of directories in
which Python searches for modules. The search path begins locally, so if
you create a module math.py, the search path
will find it before the standard library version.
Packages
In addition to the standard library modules, a vast collection of
Python packages can be installed and import
ed. A
package is a collection of modules. Packages are created to organize
code into reusable units and distribute them to others.
The official source for Python packages is the Python Package Index
(PyPI) (Community 2024).
The pip
program distributed with Python
is the most popular tool for installing and managing packages. Packages
can be installed in Anaconda environments with pip
, but the use of its own package manager
called conda
is preferred. The base
Anaconda environment comes with many
preinstalled packages useful for engineering computing. The installation
process for installing a package includes adding the package to the
Python path so that it is available to all your Python programs.
Once a package is installed, it can be import
ed in a
script. Most packages import by default one or more modules; this allows
us to import
the
package in our script without individually importing each module. For
instance, if we would like to use a function do_something()
in the foo.py module of the pkg
package, we could write the
following:
import pkg # Import the entire package
# Call a function in a module loaded by default pkg.foo.do_something()
If the module is not loaded by default, or if we would only like to
load a specific module, we can manually import
the
module in the usual way:
import pkg.foo # Import the module
# Call a function in the module pkg.foo.do_something()
Often, packages will import some important functions into its
top-level namespace such that they can be called with a shorter name. In
the example above, the package could elevate do_something()
to its top-level
namespace such that it can be called via pkg.do_something()
.
Packages can contain packages, called subpackages. Simple packages do not require this nesting feature, but large and complex packages may.
You may one day create a package of your own. All that is required is
to place your modules into a directory. If you place a special file
named __init__.py
in the directory my_pkg
, it will be executed whenever
the package is loaded.1 Often, we want to load certain (or
all) modules in this file such that they are imported by default when
the package is loaded.
Your package can be distributed via PyPI or another means.
pydocs, pydocs, pypi, pypackaging
For earlier versions of Python, the
__init__.py
file was obligatory for a package. Now it is optional but advisable.↩︎
Online Resources for Section 2.3
No online resources.