Python Basics-Packages and modules
Write in front
If not specifically stated, the following are based on Python3
Summary
Modules and packages are used for reuse and better maintenance code, Python a Python file is a module, and the package is a special directory (containing files) for the organization module __init__.py .
Module search path, the Python interpreter searches for a module in a specific directory, and the run-time sys.path search path.
Use import keywords to import the module and pay attention import * to __all__ the relationship.
1. Module and Import
A module is a file containing Python definitions and statements
PythonThe module is the file containing the definition and the statement, and the filename is the name of the module plus the .py suffix.
1.1 Born for reuse
Suppose there is a function or class that accomplishes a particular function and is useful. To use this feature, you have to copy the code into every file you need to use. Repetitive code is the taboo of programming, if the functional implementation needs to be modified, will have to modify every occurrence of the place, which is anti-human.
Reuse can be a good solution to this problem, in fact, functions, classes and other structures to some extent also provides convenience for reuse.
Python, a series of related functions, classes, etc. are organized into a single file, each of which is a Python module.
1.2 Importing Modules
importImport modules using keywords (modules need to be in the search path):
Import SYS;
Import sys as system; aliases the imported names.
from sys import path; Import module-specific elements.
from sys import *; Import all import names from sys
import-only-once
Module Import only once this behavior is a substantial optimization in most cases, in the same interpreter lifetime, when multiple import statements are imported into the same module, the import occurs only once.
This can be demonstrated by adding an output statement to the module.
import *And__all__
Use namespaces import * that may contaminate the current module and import names that do not need to be referenced. Therefore, it is not recommended.
In fact, the canonical third-party module provides a common interface to the module that exposes the interfaces available to the module. The public interface is defined by __all__ a list of module names.
For example, define mtest1 a module named:
__all__ = [' test1 ', ' test12 ']def test1 ():p rint (' test1 ') def test11 ():p rint (' test11 ') def test12 ():p rint (' test12 ')
To use the full import method:
>>> form mtest1 Import *>>> dir () >>> [' __annotations__ ', ' __builtins__ ', ' __doc__ ', ' __ Loader__ ', ' __name__ ', ' __package__ ', ' __spec__ ', ' test1 ', ' test12 '
You can see that the function has test11() not been imported, which is __all__ the function.
2. Package and its construction
To better organize the modules, group the modules into packages.
2.1 Package is a special module
From the file system, the package is the directory where the module resides. In order for the Python interpreter to differentiate its generic directory as a package, the package must contain a __init__.py file (module) named directly.
A package is basically another type of module, except that the package can contain other modules and packages. As a module, the content of the package is actually __init__.py the content of the file (module).
If constants the package is named, the file is constants/__init__.py as follows:
PI = 3.14
Then you can treat the package constants as a normal module:
Import Constantsprint (constants. PI)
2.2 Building Packages
If you want to build a drawing package named, which contains shapes and colors modules, you need to create directories and files:
| Files/Directories |
Description |
| ~/python |
Directories 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 |
| ~/python/drawing/shapes.py |
Shapes module |
Assume that you are already ~/python searching for a directory. According to this setting, the following import statements are valid:
import drawing# import drawing package (i.e. __init__.py module)
import drawing.colors# Import the colors module and reference it using the Drawing.colors.attr method
from drawing import shapes# import Shapes Module
__all__Variable
Similar to the variables of the module __all__ , the variables of the package __all__ determine the sub-modules that are used from package import * for import.
drawingthe contents of the above package are as __init__.py follows:
__all__ = [' Colors ']
Then using from drawing import * only the module is imported colors .
3. Search Path
Now I have finished writing a very useful module and passed the test. So how do you make this module available? That is, how to make the module capable of importing into other modules.
3.1 Search Module
When import you import a module using a statement, the Python interpreter searches for the module in the following ways:
Search for built-in modules first
sys.pathList of paths provided by the last search variable
sys.pathInitialize from the following location when the interpreter starts:
Current Script Path
PYTHONPATHthe path collection specified by the environment variable
Install default Path
sys.pathAfter initialization is complete, it can be modified at run time.
3.2 Making the module available
So now to make the module available, one is to place it under an existing search path, and the second is to specify that the module is located on the path to the search path.
In general, if you choose the first way, we put the module Python under the installation path \lib\site-packages , this directory is dedicated to the installation of third-party modules. As the files in this directory README show:
This directory exists so, 3rd party packages can is installed here. Read the source for site.py to more details.
If you choose the second option, simply add the directory where the module resides to the environment variable PYTHONPATH .
It is important to note that you can \lib\site-packages create a new file named after the path, the user_lib.pth content is the path to search, one line, or you can add the specified path to the search directory: