What are Python packages
Next chapter: tests
The division of a source text into individual parts (modules) is called modularization. In Python we distinguish two types of modules:
Provide data types or functions for all Python programs.
- the extensive standard library
- own libraries
- Third party library
- local modules
only available for one program
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
There are different types of modules:
- written in python
- 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:
- In the current directory
- 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.
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']
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' >>>
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.
Next chapter: tests
- What are your worst experiences with drunk drivers
- QuickBooks is viewed as an ERP system
- Home is childish
- What are the most effective detox methods
- What are the differences in convergent evolution
- Are you interested in space exploration
- What is an algebraic fraction
- Torture is illegal under the Geneva Conventions
- Why is GST a disadvantage
- What is Revenue Management in Adtech
- Ghaziabad is changing
- How perfect is a mind at rest
- Are there any Latin jokes
- Which countries are currently being privatized?
- Why do people like penguins
- When was eBay going public?
- How small can a video camera get?
- Waitresses make money
- What is privileged identity management
- How do I develop a photographic eye
- What is the risk associated with BTST
- How do animals save people?
- What is a philosophical stance
- What is the best astrological birth sign
- Coffee is bad for migraines
- Why has Shimla lost its popularity
- Is water a human right
- What makes sexy women alluring
- What makes INTP women so special
- Should jobs be given to developing countries
- How can I look like Arvind Kejriwal
- How to say olives in Arabic