One of the features of python is that some functions can be used by importing different modules, similar to the include in c
In python, use import or from Import to import the corresponding module.
Import the whole module in the format: import somemodule
Import a function from a module in the form of: from somemodule import somefunction
Import multiple functions from a module in the form of: from somemodule import firstfunc, secondfunc, thirdffunc
Import all functions in a module in the form of: from somemodule import*
Modu l e [m M [dju]
n. Unit (especially a part of a British university course); module; function block; program block; component; accessory
function in [ˈ f ʌ k ʃ n] us [ˈ f ʌ k ʃ n]
n. Function; function; function; function; social gathering; ceremony; banquet; function
v. To function; function normally; operate
**Import sys Modular** import sys//Import sys module print('================Python import mode==========================') print ('Command line arguments are:') for i in sys.argv: print (i) print ('\n python Path is',sys.path)
Import sys Modular argv,path member from sys import argv,path # Import specific members print('================python from import===================================') print('path:',path) # Because the path member has been imported, you do not need to add sys when referencing here.path
# Import the whole module of sys import sys # Use the sys module name as a prefix to access members in the module print(sys.argv)
The second line of code above imports the sys module in the simplest way, so when you use members within the sys module in your program, you must add the module name as a prefix.
When importing an entire module, you can also specify an alias for the module. For example, the following procedure:
# Import the whole module of sys and specify the alias as s import sys as s # Use the s module alias as a prefix to access members in the module print(s.argv)
You can also import multiple modules at a time, separated by commas. For example, the following program: insert the code piece here
# Import sys and os modules import sys,os # Use the module name as a prefix to access members in the module print(sys.argv) # The sep variable of the os module represents the path separator on the platform print(os.sep)
The code in the second line above imports two modules sys and os at a time. Therefore, to use the members in the two modules sys and os, you only need to use the names of sys and os modules as prefixes.
When importing multiple modules, you can also specify aliases for modules, such as the following programs:
# Import the two modules sys and o s, and specify the alias s for sys and the alias o for os import sys as s,os as o # Use the module alias as a prefix to access members in the module print(s.argv) print(o.sep)
When importing module members, you can also specify aliases for members, such as the following program:
# Import the argv member of the sys module and specify the alias v for it from sys import argv as v # Use the syntax of importing Members (and specifying aliases) to access directly using the aliases of members print(v)
The second line of code imports the argv member in the sys module and specifies the alias v for the member, so that the argv member can be used in the program through the alias v without any prefix
Note: it is not recommended to import all members of the module from import. It should be noted that the syntax "from module import" is generally not recommended to import all members in the specified module, because it has potential risks. For example, different modules contain functions with the same name.
What if you really want to use it?
There are two ways.
import module1 import module2 as m2 #Called with module name of module module1 as prefixfoo()function module1.foo() #Called with module alias of module2 as prefixfoo()function m2.foo()
#Import the foo member in module1 and specify its alias as foo1 from module1 import foo as fool #Import the foo member in module2 and specify its alias as foo2 from module2 import foo as foo2 foo1() #Call thefoo()function foo2() #Call thefoo()function