Guido van Rossum | 5938011 | 1997-09-07 02:56:35 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{ni}} |
| 2 | \label{module-ni} |
| 3 | \bimodindex{ni} |
| 4 | |
Guido van Rossum | 1622112 | 1997-09-07 03:02:39 +0000 | [diff] [blame] | 5 | \strong{Warning: This module is obsolete.} As of Python 1.5a4, |
| 6 | package support (with different semantics for \code{__init__} and no |
| 7 | support for \code{__domain__} or\code{f__}) is built in the |
| 8 | interpreter. The ni module is retained only for backward |
| 9 | compatibility. |
| 10 | |
Guido van Rossum | 5938011 | 1997-09-07 02:56:35 +0000 | [diff] [blame] | 11 | The \code{ni} module defines a new importing scheme, which supports |
| 12 | packages containing several Python modules. To enable package |
| 13 | support, execute \code{import ni} before importing any packages. Importing |
| 14 | this module automatically installs the relevant import hooks. There |
| 15 | are no publicly-usable functions or variables in the \code{ni} module. |
| 16 | |
| 17 | To 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 |
| 19 | path, 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 | |
| 22 | To import module \code{ham} from package \code{spam} and use function |
| 23 | \code{hamneggs()} from that module, you can use any of the following |
| 24 | possibilities: |
| 25 | |
| 26 | \bcode\begin{verbatim} |
| 27 | import spam.ham # *not* "import spam" !!! |
| 28 | spam.ham.hamneggs() |
| 29 | \end{verbatim}\ecode |
| 30 | % |
| 31 | \bcode\begin{verbatim} |
| 32 | from spam import ham |
| 33 | ham.hamneggs() |
| 34 | \end{verbatim}\ecode |
| 35 | % |
| 36 | \bcode\begin{verbatim} |
| 37 | from spam.ham import hamneggs |
| 38 | hamneggs() |
| 39 | \end{verbatim}\ecode |
| 40 | % |
| 41 | \code{import spam} creates an |
| 42 | empty package named \code{spam} if one does not already exist, but it does |
| 43 | \emph{not} automatically import \code{spam}'s submodules. |
| 44 | The 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 |
| 48 | spam'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 | % |
| 54 | Additional initialization code (setting up variables, importing other |
| 55 | submodules) can be performed in \file{spam/__init__.py}. |