blob: e962eea0ab888bbc2e751ae4b7214702afaa2dc2 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{ni}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{ni}
3
4\modulesynopsis{None}
5
Guido van Rossum59380111997-09-07 02:56:35 +00006
Guido van Rossum16221121997-09-07 03:02:39 +00007\strong{Warning: This module is obsolete.} As of Python 1.5a4,
8package support (with different semantics for \code{__init__} and no
Guido van Rossum138dac51997-12-09 15:03:41 +00009support for \code{__domain__} or \code{__}) is built in the
Guido van Rossum16221121997-09-07 03:02:39 +000010interpreter. The ni module is retained only for backward
Guido van Rossum138dac51997-12-09 15:03:41 +000011compatibility. As of Python 1.5b2, it has been renamed to \code{ni1};
12if you really need it, you can use \code{import ni1}, but the
13recommended approach is to rely on the built-in package support,
14converting existing packages if needed. Note that mixing \code{ni}
Fred Drake0cf785a1998-01-09 22:37:52 +000015and the built-in package support doesn't work: once you import
Guido van Rossum138dac51997-12-09 15:03:41 +000016\code{ni}, all packages use it.
Guido van Rossum16221121997-09-07 03:02:39 +000017
Guido van Rossum59380111997-09-07 02:56:35 +000018The \code{ni} module defines a new importing scheme, which supports
19packages containing several Python modules. To enable package
20support, execute \code{import ni} before importing any packages. Importing
21this module automatically installs the relevant import hooks. There
22are no publicly-usable functions or variables in the \code{ni} module.
23
24To create a package named \code{spam} containing sub-modules \code{ham}, \code{bacon} and
25\code{eggs}, create a directory \file{spam} somewhere on Python's module search
26path, as given in \code{sys.path}. Then, create files called \file{ham.py}, \file{bacon.py} and
27\file{eggs.py} inside \file{spam}.
28
29To import module \code{ham} from package \code{spam} and use function
30\code{hamneggs()} from that module, you can use any of the following
31possibilities:
32
Fred Drake19479911998-02-13 06:58:54 +000033\begin{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000034import spam.ham # *not* "import spam" !!!
35spam.ham.hamneggs()
Fred Drake19479911998-02-13 06:58:54 +000036\end{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000037%
Fred Drake19479911998-02-13 06:58:54 +000038\begin{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000039from spam import ham
40ham.hamneggs()
Fred Drake19479911998-02-13 06:58:54 +000041\end{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000042%
Fred Drake19479911998-02-13 06:58:54 +000043\begin{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000044from spam.ham import hamneggs
45hamneggs()
Fred Drake19479911998-02-13 06:58:54 +000046\end{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000047%
48\code{import spam} creates an
49empty package named \code{spam} if one does not already exist, but it does
50\emph{not} automatically import \code{spam}'s submodules.
51The only submodule that is guaranteed to be imported is
52\code{spam.__init__}, if it exists; it would be in a file named
53\file{__init__.py} in the \file{spam} directory. Note that
54\code{spam.__init__} is a submodule of package spam. It can refer to
55spam's namespace as \code{__} (two underscores):
56
Fred Drake19479911998-02-13 06:58:54 +000057\begin{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000058__.spam_inited = 1 # Set a package-level variable
Fred Drake19479911998-02-13 06:58:54 +000059\end{verbatim}
Guido van Rossum59380111997-09-07 02:56:35 +000060%
61Additional initialization code (setting up variables, importing other
62submodules) can be performed in \file{spam/__init__.py}.