Python's import is a very frequently used operation, which is an operation of using the code of another module in one module (almost all languages have similar statements). The import statement is the most commonly used method, but it is not the only method. There are also importlib.import_module()
and __import__()
, etc.
import does two things:
● Search for the module with the name (in fact, the built-in __import__() method is used) and then initialize an object
● Bind the result to a local variable name (the default is the name of this module).
means that these two sentences are equivalent: import package as pk and pg = __import__('package')
It is worth noting that only calling __import__() will only execute Search actions, initialize the module object, and finally discard the object (because there is no assignment operation). It is easy to confuse the built-in __imoprt__() method with the import statement.
Related recommendations: "Python Tutorial"
The above is the detailed content of What is python import. For more information, please follow other related articles on the PHP Chinese website!