Python basics-import usage

Python basics-import usage

1. Import module name

#sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时环境。
import sys
print("----------------------------python Modules-------------------------")
print("命令行参数:")
#argv:实现从程序外部向程序传递参数,这里是传递文件名
for x in sys.argv:
    print(x)
#获取指定模块搜索路径的字符串集合,可以将写好的模块放在得到的某个路径下,就可以在程序中import时正确找到
print("Python的路径是:",sys.path)

Insert picture description here

2. from module name import function name

Pass in the argv and path functions in the sys module. Similarly, you can use from sys import * to pass all functions in sys.

from sys import argv,path
print("----------------------------python Modules-------------------------")
print("命令行参数:")
for x in argv:
    print(x)
print("Python的路径是:",path)

Insert picture description here

3. Impor module name as module alias

The module sys is aliased to s, and the following program can use s instead of sys.

import sys as s
print("----------------------------python Modules-------------------------")
print("命令行参数:")
for x in s.argv[0]:
    print(x)
print("Python的路径是:",s.path)

Insert picture description here

Guess you like

Origin blog.csdn.net/Doigt_/article/details/108724445