In writing the script, found to import some modules, often error prompts to import the module failed, here to bad to fill the Python import module knowledge points.
1. File lookup order at module import
In the script, the steps to import the XXX module are as follows:
(1) Create a new module
(2) Insert module into Sys.module
(3) Find the path to the module, looking in order to find the file currently using the Import module in the same level directory, followed by the Python search module path set Sys.path, and finally Python added the path of the default environment variable python path
(4) Module lookup to then load module
Therefore, if the module that needs to be imported is not in the same level directory, name uses Sys.path to add the path to the module
Import Sys
Sys.path ("Folder path of module, package path of module")
2. Module, Package
The module is a file with a. py suffix, and the package is the directory where the module resides, in order to identify the directory where the module is a reference package, you can create a new __init__.py file under the package, either empty or not empty.
When not empty, you can write a module import statement under a package in a file, because the __init__.py file is imported when the module is actually imported.
3. Import Module Statement format
Import Module Name
From Package name Import module name (only one module in the package is imported)
From package name. module Name Import function name (when a specific function in the module needs to be imported)
4..pycache file
The module is executed when it is imported, and the Python interpreter automatically generates a Pycache file in the same folder of the module to speed up the startup of the program.
Selenium + Python automation Test unittest Framework Learning (iv) Python import module and package knowledge points