Python uses import to import modules. There are two main types:
import module name 1[as alias 1],Module name 2[as alias 2],...: Import the entire module.
from module name import member name 1[as alias 1], Member name 2[as alias 2],...: Import the specified members in the module.
The main differences between the above two import statements are as follows:
The first type of import statement imports all members in the entire module (including variables, functions, classes, etc.); the second type of import statement only imports specified members in the module (unless the form module name import * is used, but this is generally not recommended grammar).
python learning network, a large number of free python video tutorials, welcome to learn online!
When using the first type of import statement to import members in a module, you must add the module name or module alias prefix; when using the second type of import statement to import members in a module, you don’t need to use any prefix, just use the member name or member alias directly .
The following program uses the simplest syntax to import the entire module to import the specified module:
# Import the entire sys module
importsys
# Use the sys module name as a prefix to access the members in the module
print(sys.argv[0])
The second line of code above uses the simplest way to import the sys module, so when using the members of the sys module in the program, you must add the module name as a prefix.
Run the above program, you can see the following output (the argv variable under the sys module is used to obtain the command line parameters of the running Python program, and argv[0] is used to obtain the program name of the Python program).
importtest.py
You can specify aliases for modules when importing the entire module. For example, the following program:
# Import the entire sys module and specify the alias as s
importsysass
# Use the s module alias as a prefix to access the members in the module
print(s.argv[0])
The second line of code specifies the alias s when importing the sys module. Therefore, when using members in the sys module in the program, the module alias s must be added as a prefix. Run the program, you can see the following output:
importtest2.py
You can also import multiple modules at once using the syntax of importing the entire module, separated by commas. For example, the following program:
# Import sys and os modules
importsys,os
# Use the module name as a prefix to access the members in the module
print(sys.argv[0])
# The sep variable of the os module represents the path separator on the platform
print(os.sep)
Knowledge point expansion:
Automatically import any missing library instance methods:
# The following code is in python 3.6.1 Version verification passed
import sys
import os
from importlib import import_module
classAutoInstall():
_ loaded =set()
@ classmethod
def find_spec(cls, name, path, target=None):if path is None and name not in cls._loaded:
cls._loaded.add(name)print("Installing", name)try:
result = os.system('pip install {}'.format(name))if result ==0:returnimport_module(name)
except Exception as e:print("Failed", e)return None
sys.meta_path.append(AutoInstall)
So far, this article on the specific method of python import library is introduced. For more related python how to import library content, please search for the previous article of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou. !
Recommended Posts