This is the 16th day of my participation in the August More Text Challenge. For details, see:August is more challenging
Retrospective review
We looked at modular programming last time
Modular programming idea can help us quickly reuse the developed code, the developed code has been packaged into modules, when needed to directly import reuse, to improve our work efficiency.
Module composition includes: function, class, variable, executable code
Modules come from three sources:
(1) Python built-in modules such as time, OS, SYS, etc
(2) Third-party modules such as MessagePack
(3) Custom module
Module use: In the program, through the module import to use the developed function
In this installment, we’ll learn how to import modules and some common problems with importing modules
🎵🎵 No more nonsense, Let’s learn the module import related knowledge, Let’s go~
1. import
The statement import
👉 The basic syntax of the import statement is as follows:
-
Importing a module
The import module nameCopy the code
-
Importing multiple modules
Import module 1, module 2...Copy the code
-
Import the module and use the new name
Import module name as Module aliasCopy the code
👉import Loads modules into four general categories:
- Code written in Python (.py files)
- C or C++ extensions that have been compiled into shared libraries or DLLS
- A package that wraps a group of modules
- A built-in module written in C and linked to the Python interpreter
2. form_import
The import
From… Import Imports members in a module. The basic syntax is as follows:
From module name import member 1, member 2,....Copy the code
If you want to import all members of a module, you can do this:
From module name import *Copy the code
To import module members with aliases, use the following methods:
From module name import member name as aliasCopy the code
📢 is not recommendedFrom module name import *
(1) It means that all names in the import module that do not begin with an underscore (_) are imported to the current location.
(2) Because you do not know the name of the import, you may overwrite previously defined names
(3) Poor readability, inconvenient for later maintenance
3. import
与 from ... import
The difference between
-
Import imports modules
(1) Modules are files ending in.py
(2) Import is followed by “file name”
(3) Import The module member, must add the module name or module alias prefix
-
from… Import imports a function or class from a module
(1) Only the specified member can be imported
(2) There is no need to use any prefix, direct member name or member alias when importing members in the module
4. __import__
Dynamic statement import
The import statement is essentially a call to the function __import__(). It allows you to import modules dynamically by passing different parameter values to __import__()
Steps for dynamically importing a module:
(1) Define variables and assign them to the name of the module
(2) Call __import__() and pass in variables
>>> s='math' >>> m= __import__(s) >>> m.pin (3.141592653589793) 0.0015926529164868282Copy the code
⚠️ It is generally not recommended to use __import__() imports, which behave differently in Python2 and Python3 and can cause unexpected errors. If dynamic imports are required, the importlib module can be used
>>> import importlib
>>> a = importlib.import_module("math")
>>> a.pi
3.141592653589793
Copy the code
5. Module search path
When we use the import statement to import a module, the Python interpreter finds the specified module file in the following order
⭐ Module file search steps:
- Current directory lookup. Look in the directory where the current program file is located
- PYTHONPATH environment variable in each directory
- Look in the default Python installation site-packages directory
🌟 The above directories are stored in the sys.path variable of the standard module sys, where we can see all the directories that the specified program file supports lookup.
If the imported module is not stored in the directory shown in sys.path, the Python interpreter throws a ModuleNotFoundError (module not found) exception after the imported module runs the program
6. Common module import problems
-
Modules cannot be imported twice.
(1) Design concept: Import only once
(2) When a module is imported, the code in the module is executed.
(3) Importing a module is more about defining variables, functions, objects and so on in the module, which do not need to be defined and executed repeatedly. Therefore, if the module is imported again, it will not be executed again.
Summary: No matter how many times a module is imported, there is only one instance of the module in the entire interpreter process
-
No module named ‘XXX’ cannot be found
(1) For third-party modules: Check whether the modules have been downloaded and check the PIP list through CMD. If not, PIP install the module name to download
(2) Method 1: Temporarily add the full path of the module
Temporarily adds the full path to the module file storage location to sys.path
import sys sys.path.append('path') Copy the code
(3) Method 2: Save the module to the specified location
Place the module in the module load path already included in the sys.path variable
The module file is added to the 'lib\site-packages' pathCopy the code
(4) Method 3: Set the PATH system environment variable.
conclusion
In this installment, we will learn two ways to import modules, import and from… Import:
- Import After importing a module, prefix it with the module name before calling the instance method
- from… Import Imports a specific member of a module, which can be used directly when invoking instance methods
In the actual work scenario, the above two ways to import the modules we need to achieve rapid development
See you next time ~ღ(´ · ᴗ · ‘) 🌹🌹🌹