What are Python packages

Python course

Previous chapter: files
Next chapter: tests

Modularization

Modules

The division of a source text into individual parts (modules) is called modularization. In Python we distinguish two types of modules:

  • Libraries
    Provide data types or functions for all Python programs.
    There are:
    • the extensive standard library
    • own libraries
    • Third party library
  • local modules
    only available for one program
A library, whether from the standard library or your own, is created with the import statement involved.
Example:
import math The math module from the standard library provides mathematical constants and functions.
The keyword import can be followed by several module names separated by commas: import math, random import instructions can appear at any point in the source code, but they should be placed at the beginning for the sake of clarity.

Namespaces of the libraries

If a library is imported, e.g.

import math then the names of the library are available in their own namespace.
The sin () function of math can initially only be accessed via its full name ("fully qualified"), ie math.sin (x) You can also selectively import only individual methods / functions: from math import sin, pi Die other methods of the library are then not available. However, sin and pi can be accessed directly (without math.).
You can also include a library completely in the global namespace. Any existing names of the same name are then overwritten, as is done in the following example: >>> pi = 3.142 >>> print pi 3.142 >>> from math import * >>> print pi 3.14159265359 >>>

Renaming of the namespace during import

When importing a library, you can also choose a new name for the namespace:

>>> import math as mathematik >>> print mathematik.cos (mathematik.pi) -1.0 A namespace math does not exist in the above example, only a namespace mathematik.
In the following example, individual methods of the math module are renamed, so pow () are renamed to power () and sin () to sinus (): >>> from math import pi, pow as power, sin as sinus >>> power (2,3) 8.0 >>> sinus (pi) 1.2246467991473532e-16

Module types

There are different types of modules:

  • written in python
    Extension: .py
  • Dynamically loaded C modules
    Ending: .dll, .pyd, .so, .sl, etc.
  • C modules that are linked with the interpreter:
    To get a list of these modules: import sys print sys.builtin_module_names If it is a built-in module, you will get an error message.

Search path for modules

If you import a module e.g. abc, the interpreter searches for abc.py in the following order:

  1. In the current directory
  2. PYTHONPATH
  3. If PYTHONPATH is not set, a search is made in the default path depending on the installation, i.e. under Linux / Unix e.g. in /usr/lib/python2.5.
In the following we see how to find out where a module is: >>> import math >>> math .__ file__ '/usr/lib/python2.5/lib-dynload/math.so' >>> import random >>> random .__ file__ '/usr/lib/python2.5/random.pyc'

Content of a module

The build-in function dir () can be used to display the names defined in a module.

>>> dir (math) ['__doc__', '__file__', '__name__', 'acos',' asin ',' atan ',' atan2 ',' ceil ',' cos', 'cosh', 'degrees ',' e ',' exp ',' fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] >>> Without arguments, dir () returns the defined names >>> import math >>> col = ["red", "green", "blue"] >>> dir () ['__builtins__', '__doc__', '__name__', 'col', 'math']

Own modules

The two following functions fib (), which returns the nth Fibonacci value, and the function fiblist () are saved in a file fibonacci.py.

def fib (n): a, b = 0, 1 for i in range (n): a, b = b, a + b return a def fiblist (n): fib = [0,1] for i in range ( 1, n): fib + = [fib [-1] + fib [-2]] return fib From another program or from the interactive shell, if fibonacci.py can be found within the search path, the file can be found with call the two Fibonacci functions as a module. >>> import fibonacci >>> fibonacci.fib (10) 55 >>> fibonacci.fiblist (10) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] >>> fibonacci .__ name__ 'fibonacci' >>>

Packages

Python makes it possible to encapsulate several modules in one package. A package can contain any number of other packages.

To create a package, all you have to do is create a subfolder containing a file named __init__.py.

The file can be empty or contain initialization code in Python that is executed once when the package is imported.

Previous chapter: files
Next chapter: tests