
Python 的 sys 库提供了访问和控制Python解释器的接口。它允许程序与Python解释器交互,如获取命令行参数、管理解释器的运行环境、输出错误信息等。sys 还可以用来执行外部命令、控制标准输入输出和缓冲区大小。这个库在系统编程、脚本开发和调试工具中非常有用,提供了强大的运行时控制能力。
from pandas import pdimport timeimport sysimport os# 打印覆盖for i in range(5):# print (i,end="")sys.stdout.write("/r %s" % i)sys.stdout.flush()time.sleep(1)# 查看包print (sys.modules) # 打印,查看该字典具体内容。print ("当前平台")print (sys.platform)# win32print ("当前Python版本")print (sys.version)# 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)]print ("默认编码格式")print (sys.getdefaultencoding())# utf-8print ("所有系统模块")print (sys.modules)# {"builtins": <module "builtins" (built-in)>,# "sys": <module "sys" (built-in)>,# "_frozen_importlib": <module "_frozen_importlib" (frozen)>,# "_imp": <module "_imp" (built-in)>,# "_warnings": <module "_warnings" (built-in)>,# "_thread": <module "_thread" (built-in)>,# "_weakref": <module "_weakref" (built-in)>,# "_frozen_importlib_external": <module "_frozen_importlib_external" (frozen)>,# "_io": <module "io" (built-in)>,# "marshal": <module "marshal" (built-in)>,# "nt": <module "nt" (built-in)>, "winreg": <module "winreg" (built-in)>,# "zipimport": <module "zipimport" (built-in)>,# "encodings": <module "encodings" from "C:Python36libencodings\__init__.py">,# "codecs": <module "codecs" from "C:Python36libcodecs.py">,# "_codecs": <module "_codecs" (built-in)>,# "encodings.aliases": <module "encodings.aliases" from "C:Python36libencodingsaliases.py">,# "encodings.utf_8": <module "encodings.utf_8" from "C:Python36libencodingsutf_8.py">,# "_signal": <module "_signal" (built-in)>,# "__main__": <module "__main__" from "pp.py">,# "encodings.latin_1": <module "encodings.latin_1" from "C:Python36libencodingslatin_1.py">,# "io": <module "io" from "C:Python36libio.py">,# "abc": <module "abc" from "C:Python36libabc.py">,# "_weakrefset": <module "_weakrefset" from "C:Python36lib\_weakrefset.py">,# "site": <module "site" from "C:Python36libsite.py">,# "os": <module "os" from "C:Python36libos.py">,# "errno": <module "errno" (built-in)>,# "stat": <module "stat" from "C:Python36libstat.py">,# "_stat": <module "_stat" (built-in)>,# "ntpath": <module "ntpath" from "C:Python36libtpath.py">,# "genericpath": <module "genericpath" from "C:Python36libgenericpath.py">,# "os.path": <module "ntpath" from "C:Python36lib path.py">,# "_collections_abc": <module "_collections_abc" from "C:Python36lib\_collections_abc.py">,# "_sitebuiltins": <module "_sitebuiltins" from "C:Python36lib\_sitebuiltins.py">,# "sysconfig": <module "sysconfig" from "C:Python36libsysconfig.py">}print ("导入模块及包的查找路径")print (sys.path)# ["C:UsersxxxDesktoppy",# "C:Python36python36.zip",# "C:Python36DLLs",# "C:Python36lib",# "C:Python36",# "C:Python36libsite-packages"]a = os.path.abspath(__file__) # 当前文件绝对路径BASE_DIR = os.path.dirname(a) # 存放c.py所在的绝对路径sys.path.append(BASE_DIR) # 可以加路径# 脚本后参数# 用以下命令启动# python demo.py helloprint ("脚本路径", sys.argv[0])if len(sys.argv) > 1:print ( "脚本后参数为", sys.argv[1:])else:print ("无参数")# 脚本后参数为 ["hello"]