Home > Article > Backend Development > Python basics-detailed explanation of packages and modules
Unless otherwise stated, the following is based on Python3
Abstract
In order to reuse and better maintain code, Python
uses modules and packages; a Python
file is a module, and a package It is a special directory for organizing modules (including the __init__.py
file).
Module search path, Python
The interpreter searches for modules in a specific directory, and sys.path
is the search path when running.
Use the import
keyword to import the module, pay attention to the relationship between import *
and __all__
.
A module is a file containing Python definitions and statements
Python
A module is a file containing definitions and statements. The file name is the name of the module plus the .py
suffix.
Suppose there is a function or class that performs a specific function and is easy to use. In order to use this feature, you have to copy this code into every file you need to use. Duplicate code is a taboo in programming. If the function implementation needs to be modified, every place where it appears will have to be modified. This is anti-human.
Reuse can solve this problem very well. In fact, structures such as functions and classes also provide convenience for reuse to a certain extent. In
Python
, a series of related functions, classes, etc. are organized in a file, and each file is a Python
module.
Use the import
keyword to import the module (the module needs to be in the search path):
import sys ;Basic import statement.
import sys as system; alias the imported name.
from sys import path; Import module specific elements.
from sys import *;Import all importable names from sys
import-only-once
The behavior of importing a module only once is a substantial optimization in most cases. In the same interpreter life cycle, the same module is imported multiple times using the import
statement. Modules, imports only happen once.
This can be proven by adding output statements to the module.
import *
and __all__
Using import *
may pollute the namespace of the current module. Import Some names do not need to be quoted. Therefore its use is not recommended.
In fact, a standardized third-party module will provide a module public interface, exposing the interfaces available to the module. Public interfaces are defined by a list of module names __all__
.
If you define a module named mtest1
:
__all__ = ['test1', 'test12']def test1():print('test1')def test11():print('test11')def test12():print('test12')
Use all import methods:
>>> form mtest1 import *>>> dir()>>> ['__annotations__', '__builtins__', '__doc__', '__loader__','__name__', '__package__', '__spec__', 'test1', 'test12']
You can see that the function test11()
has not been imported. This is the role of __all__
.
In order to better organize modules, modules are grouped into packages.
From the file system point of view, the package is the directory where the module is located. In order for the Python
interpreter to treat it as a package, the package must directly contain a file (module) named __init__.py
.
A package is basically another type of module, except that it can contain other modules and packages. As a module, the content of a package is actually the content of the file __init__.py
(module).
For example, for a package named constants
, the file constants/__init__.py
is as follows:
PI = 3.14
Then the package can be constants
is treated as a normal module:
import constantsprint(constants.PI)
If you want to build a package named drawing
, It contains the shapes
and colors
modules. You need to create the following directories and files:
File/Directory | Description |
---|---|
~/python | Directory added to the search path |
~/python /drawing | Package directory (drawing package) |
~/python/drawing/__init__.py | Package code (drawing module) |
~/python/drawing/colors.py | color module |
shapes module |
The above is the detailed content of Python basics-detailed explanation of packages and modules. For more information, please follow other related articles on the PHP Chinese website!