libni.tex 2.38 KB
Newer Older
Fred Drake's avatar
Fred Drake committed
1 2
\section{\module{ni} ---
         None}
3 4 5 6
\declaremodule{standard}{ni}

\modulesynopsis{None}

Guido van Rossum's avatar
Guido van Rossum committed
7

8 9
\strong{Warning: This module is obsolete.}  As of Python 1.5a4,
package support (with different semantics for \code{__init__} and no
10
support for \code{__domain__} or \code{__}) is built in the
11
interpreter.  The ni module is retained only for backward
12 13 14 15
compatibility.  As of Python 1.5b2, it has been renamed to \code{ni1}; 
if you really need it, you can use \code{import ni1}, but the
recommended approach is to rely on the built-in package support,
converting existing packages if needed.  Note that mixing \code{ni}
Fred Drake's avatar
Fred Drake committed
16
and the built-in package support doesn't work: once you import
17
\code{ni}, all packages use it.
18

Guido van Rossum's avatar
Guido van Rossum committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
The \code{ni} module defines a new importing scheme, which supports
packages containing several Python modules.  To enable package
support, execute \code{import ni} before importing any packages.  Importing
this module automatically installs the relevant import hooks.  There
are no publicly-usable functions or variables in the \code{ni} module.

To create a package named \code{spam} containing sub-modules \code{ham}, \code{bacon} and
\code{eggs}, create a directory \file{spam} somewhere on Python's module search
path, as given in \code{sys.path}.  Then, create files called \file{ham.py}, \file{bacon.py} and
\file{eggs.py} inside \file{spam}.

To import module \code{ham} from package \code{spam} and use function
\code{hamneggs()} from that module, you can use any of the following
possibilities:

34
\begin{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
35 36
import spam.ham		# *not* "import spam" !!!
spam.ham.hamneggs()
37
\end{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
38
%
39
\begin{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
40 41
from spam import ham
ham.hamneggs()
42
\end{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
43
%
44
\begin{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
45 46
from spam.ham import hamneggs
hamneggs()
47
\end{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
48 49 50 51 52 53 54 55 56 57
%
\code{import spam} creates an
empty package named \code{spam} if one does not already exist, but it does
\emph{not} automatically import \code{spam}'s submodules.  
The only submodule that is guaranteed to be imported is
\code{spam.__init__}, if it exists; it would be in a file named
\file{__init__.py} in the \file{spam} directory.  Note that
\code{spam.__init__} is a submodule of package spam.  It can refer to
spam's namespace as \code{__} (two underscores):

58
\begin{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
59
__.spam_inited = 1		# Set a package-level variable
60
\end{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
61 62 63
%
Additional initialization code (setting up variables, importing other
submodules) can be performed in \file{spam/__init__.py}.