1.. _tut-modules:
2
3*******
4Modules
5*******
6
7If you quit from the Python interpreter and enter it again, the definitions you
8have made (functions and variables) are lost. Therefore, if you want to write a
9somewhat longer program, you are better off using a text editor to prepare the
10input for the interpreter and running it with that file as input instead.  This
11is known as creating a *script*.  As your program gets longer, you may want to
12split it into several files for easier maintenance.  You may also want to use a
13handy function that you've written in several programs without copying its
14definition into each program.
15
16To support this, Python has a way to put definitions in a file and use them in a
17script or in an interactive instance of the interpreter. Such a file is called a
18*module*; definitions from a module can be *imported* into other modules or into
19the *main* module (the collection of variables that you have access to in a
20script executed at the top level and in calculator mode).
21
22A module is a file containing Python definitions and statements.  The file name
23is the module name with the suffix :file:`.py` appended.  Within a module, the
24module's name (as a string) is available as the value of the global variable
25``__name__``.  For instance, use your favorite text editor to create a file
26called :file:`fibo.py` in the current directory with the following contents::
27
28   # Fibonacci numbers module
29
30   def fib(n):    # write Fibonacci series up to n
31       a, b = 0, 1
32       while a < n:
33           print(a, end=' ')
34           a, b = b, a+b
35       print()
36
37   def fib2(n):   # return Fibonacci series up to n
38       result = []
39       a, b = 0, 1
40       while a < n:
41           result.append(a)
42           a, b = b, a+b
43       return result
44
45Now enter the Python interpreter and import this module with the following
46command::
47
48   >>> import fibo
49
50This does not add the names of the functions defined in ``fibo``  directly to
51the current :term:`namespace` (see :ref:`tut-scopes` for more details);
52it only adds the module name ``fibo`` there. Using
53the module name you can access the functions::
54
55   >>> fibo.fib(1000)
56   0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
57   >>> fibo.fib2(100)
58   [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
59   >>> fibo.__name__
60   'fibo'
61
62If you intend to use a function often you can assign it to a local name::
63
64   >>> fib = fibo.fib
65   >>> fib(500)
66   0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
67
68
69.. _tut-moremodules:
70
71More on Modules
72===============
73
74A module can contain executable statements as well as function definitions.
75These statements are intended to initialize the module. They are executed only
76the *first* time the module name is encountered in an import statement. [#]_
77(They are also run if the file is executed as a script.)
78
79Each module has its own private namespace, which is used as the global namespace
80by all functions defined in the module. Thus, the author of a module can
81use global variables in the module without worrying about accidental clashes
82with a user's global variables. On the other hand, if you know what you are
83doing you can touch a module's global variables with the same notation used to
84refer to its functions, ``modname.itemname``.
85
86Modules can import other modules.  It is customary but not required to place all
87:keyword:`import` statements at the beginning of a module (or script, for that
88matter).  The imported module names, if placed at the top level of a module
89(outside any functions or classes), are added to the module's global namespace.
90
91There is a variant of the :keyword:`import` statement that imports names from a
92module directly into the importing module's namespace.  For example::
93
94   >>> from fibo import fib, fib2
95   >>> fib(500)
96   0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
97
98This does not introduce the module name from which the imports are taken in the
99local namespace (so in the example, ``fibo`` is not defined).
100
101There is even a variant to import all names that a module defines::
102
103   >>> from fibo import *
104   >>> fib(500)
105   0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
106
107This imports all names except those beginning with an underscore (``_``).
108In most cases Python programmers do not use this facility since it introduces
109an unknown set of names into the interpreter, possibly hiding some things
110you have already defined.
111
112Note that in general the practice of importing ``*`` from a module or package is
113frowned upon, since it often causes poorly readable code. However, it is okay to
114use it to save typing in interactive sessions.
115
116If the module name is followed by :keyword:`!as`, then the name
117following :keyword:`!as` is bound directly to the imported module.
118
119::
120
121   >>> import fibo as fib
122   >>> fib.fib(500)
123   0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
124
125This is effectively importing the module in the same way that ``import fibo``
126will do, with the only difference of it being available as ``fib``.
127
128It can also be used when utilising :keyword:`from` with similar effects::
129
130   >>> from fibo import fib as fibonacci
131   >>> fibonacci(500)
132   0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
133
134
135.. note::
136
137   For efficiency reasons, each module is only imported once per interpreter
138   session.  Therefore, if you change your modules, you must restart the
139   interpreter -- or, if it's just one module you want to test interactively,
140   use :func:`importlib.reload`, e.g. ``import importlib;
141   importlib.reload(modulename)``.
142
143
144.. _tut-modulesasscripts:
145
146Executing modules as scripts
147----------------------------
148
149When you run a Python module with ::
150
151   python fibo.py <arguments>
152
153the code in the module will be executed, just as if you imported it, but with
154the ``__name__`` set to ``"__main__"``.  That means that by adding this code at
155the end of your module::
156
157   if __name__ == "__main__":
158       import sys
159       fib(int(sys.argv[1]))
160
161you can make the file usable as a script as well as an importable module,
162because the code that parses the command line only runs if the module is
163executed as the "main" file:
164
165.. code-block:: shell-session
166
167   $ python fibo.py 50
168   0 1 1 2 3 5 8 13 21 34
169
170If the module is imported, the code is not run::
171
172   >>> import fibo
173   >>>
174
175This is often used either to provide a convenient user interface to a module, or
176for testing purposes (running the module as a script executes a test suite).
177
178
179.. _tut-searchpath:
180
181The Module Search Path
182----------------------
183
184.. index:: triple: module; search; path
185
186When a module named :mod:`spam` is imported, the interpreter first searches for
187a built-in module with that name. These module names are listed in
188:data:`sys.builtin_module_names`. If not found, it then searches for a file
189named :file:`spam.py` in a list of directories given by the variable
190:data:`sys.path`.  :data:`sys.path` is initialized from these locations:
191
192* The directory containing the input script (or the current directory when no
193  file is specified).
194* :envvar:`PYTHONPATH` (a list of directory names, with the same syntax as the
195  shell variable :envvar:`PATH`).
196* The installation-dependent default (by convention including a
197  ``site-packages`` directory, handled by the :mod:`site` module).
198
199More details are at :ref:`sys-path-init`.
200
201.. note::
202   On file systems which support symlinks, the directory containing the input
203   script is calculated after the symlink is followed. In other words the
204   directory containing the symlink is **not** added to the module search path.
205
206After initialization, Python programs can modify :data:`sys.path`.  The
207directory containing the script being run is placed at the beginning of the
208search path, ahead of the standard library path. This means that scripts in that
209directory will be loaded instead of modules of the same name in the library
210directory. This is an error unless the replacement is intended.  See section
211:ref:`tut-standardmodules` for more information.
212
213.. %
214    Do we need stuff on zip files etc. ? DUBOIS
215
216.. _tut-pycache:
217
218"Compiled" Python files
219-----------------------
220
221To speed up loading modules, Python caches the compiled version of each module
222in the ``__pycache__`` directory under the name :file:`module.{version}.pyc`,
223where the version encodes the format of the compiled file; it generally contains
224the Python version number.  For example, in CPython release 3.3 the compiled
225version of spam.py would be cached as ``__pycache__/spam.cpython-33.pyc``.  This
226naming convention allows compiled modules from different releases and different
227versions of Python to coexist.
228
229Python checks the modification date of the source against the compiled version
230to see if it's out of date and needs to be recompiled.  This is a completely
231automatic process.  Also, the compiled modules are platform-independent, so the
232same library can be shared among systems with different architectures.
233
234Python does not check the cache in two circumstances.  First, it always
235recompiles and does not store the result for the module that's loaded directly
236from the command line.  Second, it does not check the cache if there is no
237source module.  To support a non-source (compiled only) distribution, the
238compiled module must be in the source directory, and there must not be a source
239module.
240
241Some tips for experts:
242
243* You can use the :option:`-O` or :option:`-OO` switches on the Python command
244  to reduce the size of a compiled module.  The ``-O`` switch removes assert
245  statements, the ``-OO`` switch removes both assert statements and __doc__
246  strings.  Since some programs may rely on having these available, you should
247  only use this option if you know what you're doing.  "Optimized" modules have
248  an ``opt-`` tag and are usually smaller.  Future releases may
249  change the effects of optimization.
250
251* A program doesn't run any faster when it is read from a ``.pyc``
252  file than when it is read from a ``.py`` file; the only thing that's faster
253  about ``.pyc`` files is the speed with which they are loaded.
254
255* The module :mod:`compileall` can create .pyc files for all modules in a
256  directory.
257
258* There is more detail on this process, including a flow chart of the
259  decisions, in :pep:`3147`.
260
261
262.. _tut-standardmodules:
263
264Standard Modules
265================
266
267.. index:: pair: module; sys
268
269Python comes with a library of standard modules, described in a separate
270document, the Python Library Reference ("Library Reference" hereafter).  Some
271modules are built into the interpreter; these provide access to operations that
272are not part of the core of the language but are nevertheless built in, either
273for efficiency or to provide access to operating system primitives such as
274system calls.  The set of such modules is a configuration option which also
275depends on the underlying platform.  For example, the :mod:`winreg` module is only
276provided on Windows systems. One particular module deserves some attention:
277:mod:`sys`, which is built into every Python interpreter.  The variables
278``sys.ps1`` and ``sys.ps2`` define the strings used as primary and secondary
279prompts::
280
281   >>> import sys
282   >>> sys.ps1
283   '>>> '
284   >>> sys.ps2
285   '... '
286   >>> sys.ps1 = 'C> '
287   C> print('Yuck!')
288   Yuck!
289   C>
290
291
292These two variables are only defined if the interpreter is in interactive mode.
293
294The variable ``sys.path`` is a list of strings that determines the interpreter's
295search path for modules. It is initialized to a default path taken from the
296environment variable :envvar:`PYTHONPATH`, or from a built-in default if
297:envvar:`PYTHONPATH` is not set.  You can modify it using standard list
298operations::
299
300   >>> import sys
301   >>> sys.path.append('/ufs/guido/lib/python')
302
303
304.. _tut-dir:
305
306The :func:`dir` Function
307========================
308
309The built-in function :func:`dir` is used to find out which names a module
310defines.  It returns a sorted list of strings::
311
312   >>> import fibo, sys
313   >>> dir(fibo)
314   ['__name__', 'fib', 'fib2']
315   >>> dir(sys)  # doctest: +NORMALIZE_WHITESPACE
316   ['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__',
317    '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__',
318    '__stderr__', '__stdin__', '__stdout__', '__unraisablehook__',
319    '_clear_type_cache', '_current_frames', '_debugmallocstats', '_framework',
320    '_getframe', '_git', '_home', '_xoptions', 'abiflags', 'addaudithook',
321    'api_version', 'argv', 'audit', 'base_exec_prefix', 'base_prefix',
322    'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing',
323    'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info',
324    'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info',
325    'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth',
326    'getallocatedblocks', 'getdefaultencoding', 'getdlopenflags',
327    'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile',
328    'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval',
329    'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info',
330    'intern', 'is_finalizing', 'last_traceback', 'last_type', 'last_value',
331    'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks',
332    'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'pycache_prefix',
333    'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'setdlopenflags',
334    'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr',
335    'stdin', 'stdout', 'thread_info', 'unraisablehook', 'version', 'version_info',
336    'warnoptions']
337
338Without arguments, :func:`dir` lists the names you have defined currently::
339
340   >>> a = [1, 2, 3, 4, 5]
341   >>> import fibo
342   >>> fib = fibo.fib
343   >>> dir()
344   ['__builtins__', '__name__', 'a', 'fib', 'fibo', 'sys']
345
346Note that it lists all types of names: variables, modules, functions, etc.
347
348.. index:: pair: module; builtins
349
350:func:`dir` does not list the names of built-in functions and variables.  If you
351want a list of those, they are defined in the standard module
352:mod:`builtins`::
353
354   >>> import builtins
355   >>> dir(builtins)  # doctest: +NORMALIZE_WHITESPACE
356   ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException',
357    'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning',
358    'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError',
359    'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning',
360    'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False',
361    'FileExistsError', 'FileNotFoundError', 'FloatingPointError',
362    'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
363    'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError',
364    'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError',
365    'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented',
366    'NotImplementedError', 'OSError', 'OverflowError',
367    'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError',
368    'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning',
369    'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError',
370    'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError',
371    'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError',
372    'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
373    'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__',
374    '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs',
375    'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable',
376    'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits',
377    'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit',
378    'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr',
379    'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass',
380    'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview',
381    'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property',
382    'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice',
383    'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars',
384    'zip']
385
386.. _tut-packages:
387
388Packages
389========
390
391Packages are a way of structuring Python's module namespace by using "dotted
392module names".  For example, the module name :mod:`A.B` designates a submodule
393named ``B`` in a package named ``A``.  Just like the use of modules saves the
394authors of different modules from having to worry about each other's global
395variable names, the use of dotted module names saves the authors of multi-module
396packages like NumPy or Pillow from having to worry about
397each other's module names.
398
399Suppose you want to design a collection of modules (a "package") for the uniform
400handling of sound files and sound data.  There are many different sound file
401formats (usually recognized by their extension, for example: :file:`.wav`,
402:file:`.aiff`, :file:`.au`), so you may need to create and maintain a growing
403collection of modules for the conversion between the various file formats.
404There are also many different operations you might want to perform on sound data
405(such as mixing, adding echo, applying an equalizer function, creating an
406artificial stereo effect), so in addition you will be writing a never-ending
407stream of modules to perform these operations.  Here's a possible structure for
408your package (expressed in terms of a hierarchical filesystem):
409
410.. code-block:: text
411
412   sound/                          Top-level package
413         __init__.py               Initialize the sound package
414         formats/                  Subpackage for file format conversions
415                 __init__.py
416                 wavread.py
417                 wavwrite.py
418                 aiffread.py
419                 aiffwrite.py
420                 auread.py
421                 auwrite.py
422                 ...
423         effects/                  Subpackage for sound effects
424                 __init__.py
425                 echo.py
426                 surround.py
427                 reverse.py
428                 ...
429         filters/                  Subpackage for filters
430                 __init__.py
431                 equalizer.py
432                 vocoder.py
433                 karaoke.py
434                 ...
435
436When importing the package, Python searches through the directories on
437``sys.path`` looking for the package subdirectory.
438
439The :file:`__init__.py` files are required to make Python treat directories
440containing the file as packages.  This prevents directories with a common name,
441such as ``string``, from unintentionally hiding valid modules that occur later
442on the module search path. In the simplest case, :file:`__init__.py` can just be
443an empty file, but it can also execute initialization code for the package or
444set the ``__all__`` variable, described later.
445
446Users of the package can import individual modules from the package, for
447example::
448
449   import sound.effects.echo
450
451This loads the submodule :mod:`sound.effects.echo`.  It must be referenced with
452its full name. ::
453
454   sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)
455
456An alternative way of importing the submodule is::
457
458   from sound.effects import echo
459
460This also loads the submodule :mod:`echo`, and makes it available without its
461package prefix, so it can be used as follows::
462
463   echo.echofilter(input, output, delay=0.7, atten=4)
464
465Yet another variation is to import the desired function or variable directly::
466
467   from sound.effects.echo import echofilter
468
469Again, this loads the submodule :mod:`echo`, but this makes its function
470:func:`echofilter` directly available::
471
472   echofilter(input, output, delay=0.7, atten=4)
473
474Note that when using ``from package import item``, the item can be either a
475submodule (or subpackage) of the package, or some  other name defined in the
476package, like a function, class or variable.  The ``import`` statement first
477tests whether the item is defined in the package; if not, it assumes it is a
478module and attempts to load it.  If it fails to find it, an :exc:`ImportError`
479exception is raised.
480
481Contrarily, when using syntax like ``import item.subitem.subsubitem``, each item
482except for the last must be a package; the last item can be a module or a
483package but can't be a class or function or variable defined in the previous
484item.
485
486
487.. _tut-pkg-import-star:
488
489Importing \* From a Package
490---------------------------
491
492.. index:: single: __all__
493
494Now what happens when the user writes ``from sound.effects import *``?  Ideally,
495one would hope that this somehow goes out to the filesystem, finds which
496submodules are present in the package, and imports them all.  This could take a
497long time and importing sub-modules might have unwanted side-effects that should
498only happen when the sub-module is explicitly imported.
499
500The only solution is for the package author to provide an explicit index of the
501package.  The :keyword:`import` statement uses the following convention: if a package's
502:file:`__init__.py` code defines a list named ``__all__``, it is taken to be the
503list of module names that should be imported when ``from package import *`` is
504encountered.  It is up to the package author to keep this list up-to-date when a
505new version of the package is released.  Package authors may also decide not to
506support it, if they don't see a use for importing \* from their package.  For
507example, the file :file:`sound/effects/__init__.py` could contain the following
508code::
509
510   __all__ = ["echo", "surround", "reverse"]
511
512This would mean that ``from sound.effects import *`` would import the three
513named submodules of the :mod:`sound.effects` package.
514
515If ``__all__`` is not defined, the statement ``from sound.effects import *``
516does *not* import all submodules from the package :mod:`sound.effects` into the
517current namespace; it only ensures that the package :mod:`sound.effects` has
518been imported (possibly running any initialization code in :file:`__init__.py`)
519and then imports whatever names are defined in the package.  This includes any
520names defined (and submodules explicitly loaded) by :file:`__init__.py`.  It
521also includes any submodules of the package that were explicitly loaded by
522previous :keyword:`import` statements.  Consider this code::
523
524   import sound.effects.echo
525   import sound.effects.surround
526   from sound.effects import *
527
528In this example, the :mod:`echo` and :mod:`surround` modules are imported in the
529current namespace because they are defined in the :mod:`sound.effects` package
530when the ``from...import`` statement is executed.  (This also works when
531``__all__`` is defined.)
532
533Although certain modules are designed to export only names that follow certain
534patterns when you use ``import *``, it is still considered bad practice in
535production code.
536
537Remember, there is nothing wrong with using ``from package import
538specific_submodule``!  In fact, this is the recommended notation unless the
539importing module needs to use submodules with the same name from different
540packages.
541
542
543.. _intra-package-references:
544
545Intra-package References
546------------------------
547
548When packages are structured into subpackages (as with the :mod:`sound` package
549in the example), you can use absolute imports to refer to submodules of siblings
550packages.  For example, if the module :mod:`sound.filters.vocoder` needs to use
551the :mod:`echo` module in the :mod:`sound.effects` package, it can use ``from
552sound.effects import echo``.
553
554You can also write relative imports, with the ``from module import name`` form
555of import statement.  These imports use leading dots to indicate the current and
556parent packages involved in the relative import.  From the :mod:`surround`
557module for example, you might use::
558
559   from . import echo
560   from .. import formats
561   from ..filters import equalizer
562
563Note that relative imports are based on the name of the current module.  Since
564the name of the main module is always ``"__main__"``, modules intended for use
565as the main module of a Python application must always use absolute imports.
566
567
568Packages in Multiple Directories
569--------------------------------
570
571Packages support one more special attribute, :attr:`__path__`.  This is
572initialized to be a list containing the name of the directory holding the
573package's :file:`__init__.py` before the code in that file is executed.  This
574variable can be modified; doing so affects future searches for modules and
575subpackages contained in the package.
576
577While this feature is not often needed, it can be used to extend the set of
578modules found in a package.
579
580
581.. rubric:: Footnotes
582
583.. [#] In fact function definitions are also 'statements' that are 'executed'; the
584   execution of a module-level function definition adds the function name to
585   the module's global namespace.
586