The specific method of python import library

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

The specific method of python import library
The specific method of python instantiation object
End the method of running python
The essence of Python language: Itertools library
Detailed explanation of the principle of Python super() method
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Consolidate the foundation of Python (3)
The usage of wheel in python
Summary of logarithm method in Python
Python handles the 4 wheels of Chinese
Python simulation of the landlord deal
What is the use of Python
Python implements the steepest descent method
Secrets of the new features of Python 3.8
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
Basic analysis of Python turtle library implementation
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
Two days of learning the basics of Python
What is the id function of python
Analysis of glob in python standard library
Where is the pip path of python3
Method of installing django module in python
python implements the gradient method python the fastest descent method
What are the advantages of python language
Python novice learns to use the library
Example method of reading sql from python
python3 realizes the function of mask drawing
What is the function body of python
Solve the conflict of multiple versions of python
Detailed explanation of the use of pip in Python | summary of third-party library installation
Method analysis of Python calling C language program
What is the function of adb in python
The difference between the syntax of java and python
7 features of Python3.9
Detailed explanation of python standard library OS module
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
Solve the problem of python running startup error
Python implements the source code of the snake game
Analysis of common methods of Python operation Jira library
How about learning python at the age of 27?
2.1 The Python Interpreter (python interpreter)
The consequences of uninstalling python in ubuntu, very
The latest method of installing Mongodb on Ubuntu 16.04
Python writes the game implementation of fishing master
[898] python get the intersection of two lists | union | difference
Detailed explanation of the principle of Python function parameter classification