import 与 from...import

在 python 用 import 或者 from...import 来导入相应的模块。

将整个模块(somemodule)导入,格式为: import somemodule

从某个模块中导入某个函数,格式为: from somemodule import somefunction

从某个模块中导入多个函数,格式为: from somemodule import firstfunc, secondfunc, thirdfunc

将某个模块中的全部函数导入,格式为: from somemodule import *

导入 sys 模块

import sysprint('================Python import mode==========================')print ('命令行参数为:')for i in sys.argv:    print (i)print ('\n python 路径为',sys.path)

导入 sys 模块的 argv,path 成员

from sys import argv,path  #  导入特定的成员 print('================python from import===================================')print('path:',path) # 因为已经导入path成员,所以此处引用时不需要加sys.path

命令行参数

很多程序可以执行一些操作来查看一些基本信息,Python可以使用-h参数查看各参数帮助信息:

$ python -h
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...Options and arguments (and corresponding environment variables):-c cmd : program passed in as string (terminates option list)-d     : debug output from parser (also PYTHONDEBUG=x)-E     : ignore environment variables (such as PYTHONPATH)-h     : print this help message and exit[ etc. ]

我们在使用脚本形式执行 Python 时,可以接收命令行输入的参数,具体使用可以参照 Python 3 命令行参数。