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