Fred Drake | dd17d1c | 1998-01-20 04:40:41 +0000 | [diff] [blame^] | 1 | \section{Standard Module \sectcode{ni}} |
Guido van Rossum | 5938011 | 1997-09-07 02:56:35 +0000 | [diff] [blame] | 2 | \label{module-ni} |
Fred Drake | dd17d1c | 1998-01-20 04:40:41 +0000 | [diff] [blame^] | 3 | \stmodindex{ni} |
Guido van Rossum | 5938011 | 1997-09-07 02:56:35 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 138dac5 | 1997-12-09 15:03:41 +0000 | [diff] [blame] | 7 | support for \code{__domain__} or \code{__}) is built in the |
Guido van Rossum | 1622112 | 1997-09-07 03:02:39 +0000 | [diff] [blame] | 8 | interpreter. The ni module is retained only for backward |
Guido van Rossum | 138dac5 | 1997-12-09 15:03:41 +0000 | [diff] [blame] | 9 | compatibility. As of Python 1.5b2, it has been renamed to \code{ni1}; |
| 10 | if you really need it, you can use \code{import ni1}, but the |
| 11 | recommended approach is to rely on the built-in package support, |
| 12 | converting existing packages if needed. Note that mixing \code{ni} |
Fred Drake | 0cf785a | 1998-01-09 22:37:52 +0000 | [diff] [blame] | 13 | and the built-in package support doesn't work: once you import |
Guido van Rossum | 138dac5 | 1997-12-09 15:03:41 +0000 | [diff] [blame] | 14 | \code{ni}, all packages use it. |
Guido van Rossum | 1622112 | 1997-09-07 03:02:39 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 5938011 | 1997-09-07 02:56:35 +0000 | [diff] [blame] | 16 | The \code{ni} module defines a new importing scheme, which supports |
| 17 | packages containing several Python modules. To enable package |
| 18 | support, execute \code{import ni} before importing any packages. Importing |
| 19 | this module automatically installs the relevant import hooks. There |
| 20 | are no publicly-usable functions or variables in the \code{ni} module. |
| 21 | |
| 22 | To 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 |
| 24 | path, 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 | |
| 27 | To import module \code{ham} from package \code{spam} and use function |
| 28 | \code{hamneggs()} from that module, you can use any of the following |
| 29 | possibilities: |
| 30 | |
| 31 | \bcode\begin{verbatim} |
| 32 | import spam.ham # *not* "import spam" !!! |
| 33 | spam.ham.hamneggs() |
| 34 | \end{verbatim}\ecode |
| 35 | % |
| 36 | \bcode\begin{verbatim} |
| 37 | from spam import ham |
| 38 | ham.hamneggs() |
| 39 | \end{verbatim}\ecode |
| 40 | % |
| 41 | \bcode\begin{verbatim} |
| 42 | from spam.ham import hamneggs |
| 43 | hamneggs() |
| 44 | \end{verbatim}\ecode |
| 45 | % |
| 46 | \code{import spam} creates an |
| 47 | empty package named \code{spam} if one does not already exist, but it does |
| 48 | \emph{not} automatically import \code{spam}'s submodules. |
| 49 | The 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 |
| 53 | spam's namespace as \code{__} (two underscores): |
| 54 | |
| 55 | \bcode\begin{verbatim} |
| 56 | __.spam_inited = 1 # Set a package-level variable |
| 57 | \end{verbatim}\ecode |
| 58 | % |
| 59 | Additional initialization code (setting up variables, importing other |
| 60 | submodules) can be performed in \file{spam/__init__.py}. |