Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 108943c

Browse files
committed
Added a section to the chapter on modules, describing the package system.
The text is almost completely from GvR's essay on packages; some introductory paragraphs have been removed, and everything after the 'Details' section in the HTML has been dropped (seems too technical). A paragraph has been added after the sample package layout stating that there must be a file called__init__.py in a directory to make it a package.
1 parent 0b3b43c commit 108943c

1 file changed

Lines changed: 202 additions & 0 deletions

File tree

Doc/tut/tut.tex

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,208 @@ \section{The \function{dir()} Function}
20222022
'reduce', 'reload', 'repr', 'round', 'setattr', 'str', 'type', 'xrange']
20232023
\end{verbatim}
20242024

2025+
\section{Packages}
2026+
2027+
Packages are a way of structuring Python's module namespace
2028+
by using ``dotted module names''. For example, the module name \module{A.B}
2029+
designates a submodule named \samp{B} in a package named \samp{A}. Just like the
2030+
use of modules saves the authors of different modules from having to
2031+
worry about each other's global variable names, the use of dotted
2032+
module names saves the authors of multi-module packages like NumPy or
2033+
PIL from having to worry about each other's module names.
2034+
2035+
Suppose you want to design a collection of modules (a ``package'') for
2036+
the uniform handling of sound files and sound data. There are many
2037+
different sound file formats (usually recognized by their extension,
2038+
e.g. \file{.wav}, \file{.aiff}, \file{.au}), so you may need to create
2039+
and maintain a growing collection of modules for the conversion
2040+
between the various file formats. There are also many different
2041+
operations you might want to perform on sound data (e.g. mixing,
2042+
adding echo, applying an equalizer function, creating an artificial
2043+
stereo effect), so in addition you will be writing a never-ending
2044+
stream of modules to perform these operations. Here's a possible
2045+
structure for your package (expressed in terms of a hierarchical
2046+
filesystem):
2047+
2048+
\begin{verbatim}
2049+
Sound/ Top-level package
2050+
__init__.py Initialize the sound package
2051+
Formats/ Subpackage for file format conversions
2052+
__init__.py
2053+
wavread.py
2054+
wavwrite.py
2055+
aiffread.py
2056+
aiffwrite.py
2057+
auread.py
2058+
auwrite.py
2059+
...
2060+
Effects/ Subpackage for sound effects
2061+
__init__.py
2062+
echo.py
2063+
surround.py
2064+
reverse.py
2065+
...
2066+
Filters/ Subpackage for filters
2067+
__init__.py
2068+
equalizer.py
2069+
vocoder.py
2070+
karaoke.py
2071+
...
2072+
\end{verbatim}
2073+
The \file{__init__.py} files are required to make Python treat the
2074+
directories as containing packages; this is done to prevent
2075+
directories with a common name, such as \samp{string}, from
2076+
unintentionally hiding valid modules that occur later on the module
2077+
search path. In the simplest case, \file{__init__.py} can just be an
2078+
empty file, but it can also execute initialization code for the
2079+
package or set the \code{__all__} variable, described later.
2080+
2081+
Users of the package can import individual modules from the
2082+
package, for example:
2083+
2084+
\begin{verbatim}
2085+
import Sound.Effects.echo
2086+
\end{verbatim}
2087+
This loads the submodule \module{Sound.Effects.echo}. It must be referenced
2088+
with its full name, e.g.
2089+
2090+
\begin{verbatim}
2091+
Sound.Effects.echo.echofilter(input, output, delay=0.7, atten=4)
2092+
\end{verbatim}
2093+
An alternative way of importing the submodule is:
2094+
2095+
\begin{verbatim}
2096+
from Sound.Effects import echo
2097+
\end{verbatim}
2098+
This also loads the submodule \module{echo}, and makes it available without
2099+
its package prefix, so it can be used as follows:
2100+
2101+
\begin{verbatim}
2102+
echo.echofilter(input, output, delay=0.7, atten=4)
2103+
\end{verbatim}
2104+
2105+
Yet another variation is to import the desired function or variable directly:
2106+
2107+
\begin{verbatim}
2108+
from Sound.Effects.echo import echofilter
2109+
\end{verbatim}
2110+
2111+
Again, this loads the submodule \module{echo}, but this makes its function
2112+
echofilter directly available:
2113+
2114+
\begin{verbatim}
2115+
echofilter(input, output, delay=0.7, atten=4)
2116+
\end{verbatim}
2117+
2118+
Note that when using \code{from \var{package} import \var{item}}, the
2119+
item can be either a submodule (or subpackage) of the package, or some
2120+
other name defined in the package, like a function, class or
2121+
variable. The \code{import} statement first tests whether the item is
2122+
defined in the package; if not, it assumes it is a module and attempts
2123+
to load it. If it fails to find it, \exception{ImportError} is raised.
2124+
2125+
Contrarily, when using syntax like \code{import
2126+
\var{item.subitem.subsubitem}}, each item except for the last must be
2127+
a package; the last item can be a module or a package but can't be a
2128+
class or function or variable defined in the previous item.
2129+
2130+
\subsection{Importing * From a Package}
2131+
%The \code{__all__} Attribute
2132+
2133+
Now what happens when the user writes \code{from Sound.Effects import
2134+
*}? Ideally, one would hope that this somehow goes out to the
2135+
filesystem, finds which submodules are present in the package, and
2136+
imports them all. Unfortunately, this operation does not work very
2137+
well on Mac and Windows platforms, where the filesystem does not
2138+
always have accurate information about the case of a filename! On
2139+
these platforms, there is no guaranteed way to know whether a file
2140+
\file{ECHO.PY} should be imported as a module \module{echo},
2141+
\module{Echo} or \module{ECHO}. (For example, Windows 95 has the
2142+
annoying practice of showing all file names with a capitalized first
2143+
letter.) The DOS 8+3 filename restriction adds another interesting
2144+
problem for long module names.
2145+
2146+
The only solution is for the package author to provide an explicit
2147+
index of the package. The import statement uses the following
2148+
convention: if a package's \file{__init__.py} code defines a list named
2149+
\code{__all__}, it is taken to be the list of module names that should be imported
2150+
when \code{from \var{package} import *} is
2151+
encountered. It is up to the package author to keep this list
2152+
up-to-date when a new version of the package is released. Package
2153+
authors may also decide not to support it, if they don't see a use for
2154+
importing * from their package. For example, the file
2155+
\code{Sounds/Effects/__init__.py} could contain the following code:
2156+
2157+
\begin{verbatim}
2158+
__all__ = ["echo", "surround", "reverse"]
2159+
\end{verbatim}
2160+
2161+
This would mean that \code{from Sound.Effects import *} would
2162+
import the three named submodules of the \module{Sound} package.
2163+
2164+
If \code{__all__} is not defined, the statement \code{from Sound.Effects
2165+
import *} does \emph{not} import all submodules from the package
2166+
\module{Sound.Effects} into the current namespace; it only ensures that the
2167+
package \module{Sound.Effects} has been imported (possibly running its
2168+
initialization code, \file{__init__.py}) and then imports whatever names are
2169+
defined in the package. This includes any names defined (and
2170+
submodules explicitly loaded) by \file{__init__.py}. It also includes any
2171+
submodules of the package that were explicitly loaded by previous
2172+
import statements, e.g.
2173+
2174+
\begin{verbatim}
2175+
import Sound.Effects.echo
2176+
import Sound.Effects.surround
2177+
from Sound.Effects import *
2178+
\end{verbatim}
2179+
2180+
2181+
In this example, the echo and surround modules are imported in the
2182+
current namespace because they are defined in the \module{Sound.Effects}
2183+
package when the \code{from...import} statement is executed. (This also
2184+
works when \code{__all__} is defined.)
2185+
2186+
Note that in general the practicing of importing * from a module or
2187+
package is frowned upon, since it often causes poorly readable code.
2188+
However, it is okay to use it to save typing in interactive sessions,
2189+
and certain modules are designed to export only names that follow
2190+
certain patterns.
2191+
2192+
Remember, there is nothing wrong with using \code{from Package
2193+
import specific_submodule}! In fact, this is the
2194+
recommended notation unless the importing module needs to use
2195+
submodules with the same name from different packages.
2196+
2197+
2198+
\subsection{Intra-package References}
2199+
2200+
The submodules often need to refer to each other. For example, the
2201+
\module{surround} module might use the \module{echo} module. In fact, such references
2202+
are so common that the \code{import} statement first looks in the
2203+
containing package before looking in the standard module search path.
2204+
Thus, the surround module can simply use \code{import echo} or
2205+
\code{from echo import echofilter}. If the imported module is not
2206+
found in the current package (the package of which the current module
2207+
is a submodule), the \code{import} statement looks for a top-level module
2208+
with the given name.
2209+
2210+
When packages are structured into subpackages (as with the \module{Sound}
2211+
package in the example), there's no shortcut to refer to submodules of
2212+
sibling packages - the full name of the subpackage must be used. For
2213+
example, if the module \module{Sound.Filters.vocoder} needs to use the \module{echo}
2214+
module in the \module{Sound.Effects} package, it can use \code{from
2215+
Sound.Effects import echo}.
2216+
2217+
%(One could design a notation to refer to parent packages, similar to
2218+
%the use of ".." to refer to the parent directory in Unix and Windows
2219+
%filesystems. In fact, the \module{ni} module, which was the
2220+
%ancestor of this package system, supported this using \code{__} for
2221+
%the package containing the current module,
2222+
%\code{__.__} for the parent package, and so on. This feature was dropped
2223+
%because of its awkwardness; since most packages will have a relative
2224+
%shallow substructure, this is no big loss.)
2225+
2226+
20252227

20262228
\chapter{Input and Output}
20272229
\label{io}

0 commit comments

Comments
 (0)