blob: 8a251da7f458ff5530abe2349ca3c5c6f2ad7467 [file] [log] [blame]
Fred Drakedd17d1c1998-01-20 04:40:41 +00001\section{Standard Module \sectcode{ni}}
Guido van Rossum59380111997-09-07 02:56:35 +00002\label{module-ni}
Fred Drakedd17d1c1998-01-20 04:40:41 +00003\stmodindex{ni}
Guido van Rossum59380111997-09-07 02:56:35 +00004
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
Guido van Rossum138dac51997-12-09 15:03:41 +00007support for \code{__domain__} or \code{__}) is built in the
Guido van Rossum16221121997-09-07 03:02:39 +00008interpreter. The ni module is retained only for backward
Guido van Rossum138dac51997-12-09 15:03:41 +00009compatibility. As of Python 1.5b2, it has been renamed to \code{ni1};
10if you really need it, you can use \code{import ni1}, but the
11recommended approach is to rely on the built-in package support,
12converting existing packages if needed. Note that mixing \code{ni}
Fred Drake0cf785a1998-01-09 22:37:52 +000013and the built-in package support doesn't work: once you import
Guido van Rossum138dac51997-12-09 15:03:41 +000014\code{ni}, all packages use it.
Guido van Rossum16221121997-09-07 03:02:39 +000015
Guido van Rossum59380111997-09-07 02:56:35 +000016The \code{ni} module defines a new importing scheme, which supports
17packages containing several Python modules. To enable package
18support, execute \code{import ni} before importing any packages. Importing
19this module automatically installs the relevant import hooks. There
20are no publicly-usable functions or variables in the \code{ni} module.
21
22To create a package named \code{spam} containing sub-modules \code{ham}, \code{bacon} and
23\code{eggs}, create a directory \file{spam} somewhere on Python's module search
24path, as given in \code{sys.path}. Then, create files called \file{ham.py}, \file{bacon.py} and
25\file{eggs.py} inside \file{spam}.
26
27To import module \code{ham} from package \code{spam} and use function
28\code{hamneggs()} from that module, you can use any of the following
29possibilities:
30
Fred Drake19479911998-02-13 06:58:54 +000031\begin{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000032import spam.ham # *not* "import spam" !!!
33spam.ham.hamneggs()
Fred Drake19479911998-02-13 06:58:54 +000034\end{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000035%
Fred Drake19479911998-02-13 06:58:54 +000036\begin{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000037from spam import ham
38ham.hamneggs()
Fred Drake19479911998-02-13 06:58:54 +000039\end{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000040%
Fred Drake19479911998-02-13 06:58:54 +000041\begin{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000042from spam.ham import hamneggs
43hamneggs()
Fred Drake19479911998-02-13 06:58:54 +000044\end{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000045%
46\code{import spam} creates an
47empty package named \code{spam} if one does not already exist, but it does
48\emph{not} automatically import \code{spam}'s submodules.
49The only submodule that is guaranteed to be imported is
50\code{spam.__init__}, if it exists; it would be in a file named
51\file{__init__.py} in the \file{spam} directory. Note that
52\code{spam.__init__} is a submodule of package spam. It can refer to
53spam's namespace as \code{__} (two underscores):
54
Fred Drake19479911998-02-13 06:58:54 +000055\begin{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000056__.spam_inited = 1 # Set a package-level variable
Fred Drake19479911998-02-13 06:58:54 +000057\end{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000058%
59Additional initialization code (setting up variables, importing other
60submodules) can be performed in \file{spam/__init__.py}.