blob: b0f07a871f141daff33f8b0cc3e423e10ae77b68 [file] [log] [blame]
Guido van Rossum59380111997-09-07 02:56:35 +00001\section{Built-in Module \sectcode{ni}}
2\label{module-ni}
3\bimodindex{ni}
4
5The \code{ni} module defines a new importing scheme, which supports
6packages containing several Python modules. To enable package
7support, execute \code{import ni} before importing any packages. Importing
8this module automatically installs the relevant import hooks. There
9are no publicly-usable functions or variables in the \code{ni} module.
10
11To create a package named \code{spam} containing sub-modules \code{ham}, \code{bacon} and
12\code{eggs}, create a directory \file{spam} somewhere on Python's module search
13path, as given in \code{sys.path}. Then, create files called \file{ham.py}, \file{bacon.py} and
14\file{eggs.py} inside \file{spam}.
15
16To import module \code{ham} from package \code{spam} and use function
17\code{hamneggs()} from that module, you can use any of the following
18possibilities:
19
20\bcode\begin{verbatim}
21import spam.ham # *not* "import spam" !!!
22spam.ham.hamneggs()
23\end{verbatim}\ecode
24%
25\bcode\begin{verbatim}
26from spam import ham
27ham.hamneggs()
28\end{verbatim}\ecode
29%
30\bcode\begin{verbatim}
31from spam.ham import hamneggs
32hamneggs()
33\end{verbatim}\ecode
34%
35\code{import spam} creates an
36empty package named \code{spam} if one does not already exist, but it does
37\emph{not} automatically import \code{spam}'s submodules.
38The only submodule that is guaranteed to be imported is
39\code{spam.__init__}, if it exists; it would be in a file named
40\file{__init__.py} in the \file{spam} directory. Note that
41\code{spam.__init__} is a submodule of package spam. It can refer to
42spam's namespace as \code{__} (two underscores):
43
44\bcode\begin{verbatim}
45__.spam_inited = 1 # Set a package-level variable
46\end{verbatim}\ecode
47%
48Additional initialization code (setting up variables, importing other
49submodules) can be performed in \file{spam/__init__.py}.