blob: eecbe19039e8a77cc3a96a4b3082568d3c7b8509 [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
Guido van Rossum16221121997-09-07 03:02:39 +00005\strong{Warning: This module is obsolete.} As of Python 1.5a4,
6package support (with different semantics for \code{__init__} and no
7support for \code{__domain__} or\code{f__}) is built in the
8interpreter. The ni module is retained only for backward
9compatibility.
10
Guido van Rossum59380111997-09-07 02:56:35 +000011The \code{ni} module defines a new importing scheme, which supports
12packages containing several Python modules. To enable package
13support, execute \code{import ni} before importing any packages. Importing
14this module automatically installs the relevant import hooks. There
15are no publicly-usable functions or variables in the \code{ni} module.
16
17To create a package named \code{spam} containing sub-modules \code{ham}, \code{bacon} and
18\code{eggs}, create a directory \file{spam} somewhere on Python's module search
19path, as given in \code{sys.path}. Then, create files called \file{ham.py}, \file{bacon.py} and
20\file{eggs.py} inside \file{spam}.
21
22To import module \code{ham} from package \code{spam} and use function
23\code{hamneggs()} from that module, you can use any of the following
24possibilities:
25
26\bcode\begin{verbatim}
27import spam.ham # *not* "import spam" !!!
28spam.ham.hamneggs()
29\end{verbatim}\ecode
30%
31\bcode\begin{verbatim}
32from spam import ham
33ham.hamneggs()
34\end{verbatim}\ecode
35%
36\bcode\begin{verbatim}
37from spam.ham import hamneggs
38hamneggs()
39\end{verbatim}\ecode
40%
41\code{import spam} creates an
42empty package named \code{spam} if one does not already exist, but it does
43\emph{not} automatically import \code{spam}'s submodules.
44The only submodule that is guaranteed to be imported is
45\code{spam.__init__}, if it exists; it would be in a file named
46\file{__init__.py} in the \file{spam} directory. Note that
47\code{spam.__init__} is a submodule of package spam. It can refer to
48spam's namespace as \code{__} (two underscores):
49
50\bcode\begin{verbatim}
51__.spam_inited = 1 # Set a package-level variable
52\end{verbatim}\ecode
53%
54Additional initialization code (setting up variables, importing other
55submodules) can be performed in \file{spam/__init__.py}.