Python import usage and differences with from... import
This article mainly introduces the usage of Python import and the difference with from... import. This article is concise and clear and easy to understand. For more information, see
Use import or from... import in python to import the corresponding module. The module is actually a collection file of some functions and classes. It can implement some corresponding functions. When we need to use these functions, we can directly import the corresponding modules into our programs, we can use it. This is similar to the include header file in C language. In Python, we use import to import the required modules.
Eg:
The Code is as follows:
Import sys
Print ('= ========= ');
Print ('the command line arguments are :')
For I in sys. argv:
Print (I)
Print ('\ n The python path', sys. path)
From sys import argv, path # import a specific Member
Print ('= ==================== ')
Print ('path: ', path) # because the path member has been imported, sys. path is not required for reference.
If you want to use the names of all sys modules, you can:
The Code is as follows:
From sys import *
Print ('path: ', path)
We can see from the above:
############################
# Import modules. The difference between import and from... import is that:
# If you want to use argv in the program to represent sys. argv,
# Use: from sys import argv
# Generally, use the import statement instead of from .. import,
# This will make your program easier to read and avoid name conflicts
###########################