• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

关闭

返回栏目

关闭

返回python栏目

70 - 自带库 - sys - 关于python解释器操作

作者:

贺及楼

成为作者

更新日期:2024-10-27 18:03:15

sys - 关于python解释器操作

sys 库简介

Python 的 sys 库提供了访问和控制Python解释器的接口。它允许程序与Python解释器交互,如获取命令行参数、管理解释器的运行环境、输出错误信息等。sys 还可以用来执行外部命令、控制标准输入输出和缓冲区大小。这个库在系统编程、脚本开发和调试工具中非常有用,提供了强大的运行时控制能力。

sys库 python解释器操作

  1. from pandas import pd
  2. import time
  3. import sys
  4. import os
  5. # 打印覆盖
  6. for i in range(5):
  7. # print (i,end="")
  8. sys.stdout.write("/r %s" % i)
  9. sys.stdout.flush()
  10. time.sleep(1)
  11. # 查看包
  12. print (sys.modules) # 打印,查看该字典具体内容。
  13. print ("当前平台")
  14. print (sys.platform)
  15. # win32
  16. print ("当前Python版本")
  17. print (sys.version)
  18. # 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)]
  19. print ("默认编码格式")
  20. print (sys.getdefaultencoding())
  21. # utf-8
  22. print ("所有系统模块")
  23. print (sys.modules)
  24. # {"builtins": <module "builtins" (built-in)>,
  25. # "sys": <module "sys" (built-in)>,
  26. # "_frozen_importlib": <module "_frozen_importlib" (frozen)>,
  27. # "_imp": <module "_imp" (built-in)>,
  28. # "_warnings": <module "_warnings" (built-in)>,
  29. # "_thread": <module "_thread" (built-in)>,
  30. # "_weakref": <module "_weakref" (built-in)>,
  31. # "_frozen_importlib_external": <module "_frozen_importlib_external" (frozen)>,
  32. # "_io": <module "io" (built-in)>,
  33. # "marshal": <module "marshal" (built-in)>,
  34. # "nt": <module "nt" (built-in)>, "winreg": <module "winreg" (built-in)>,
  35. # "zipimport": <module "zipimport" (built-in)>,
  36. # "encodings": <module "encodings" from "C:Python36libencodings\__init__.py">,
  37. # "codecs": <module "codecs" from "C:Python36libcodecs.py">,
  38. # "_codecs": <module "_codecs" (built-in)>,
  39. # "encodings.aliases": <module "encodings.aliases" from "C:Python36libencodingsaliases.py">,
  40. # "encodings.utf_8": <module "encodings.utf_8" from "C:Python36libencodingsutf_8.py">,
  41. # "_signal": <module "_signal" (built-in)>,
  42. # "__main__": <module "__main__" from "pp.py">,
  43. # "encodings.latin_1": <module "encodings.latin_1" from "C:Python36libencodingslatin_1.py">,
  44. # "io": <module "io" from "C:Python36libio.py">,
  45. # "abc": <module "abc" from "C:Python36libabc.py">,
  46. # "_weakrefset": <module "_weakrefset" from "C:Python36lib\_weakrefset.py">,
  47. # "site": <module "site" from "C:Python36libsite.py">,
  48. # "os": <module "os" from "C:Python36libos.py">,
  49. # "errno": <module "errno" (built-in)>,
  50. # "stat": <module "stat" from "C:Python36libstat.py">,
  51. # "_stat": <module "_stat" (built-in)>,
  52. # "ntpath": <module "ntpath" from "C:Python36libtpath.py">,
  53. # "genericpath": <module "genericpath" from "C:Python36libgenericpath.py">,
  54. # "os.path": <module "ntpath" from "C:Python36lib path.py">,
  55. # "_collections_abc": <module "_collections_abc" from "C:Python36lib\_collections_abc.py">,
  56. # "_sitebuiltins": <module "_sitebuiltins" from "C:Python36lib\_sitebuiltins.py">,
  57. # "sysconfig": <module "sysconfig" from "C:Python36libsysconfig.py">}
  58. print ("导入模块及包的查找路径")
  59. print (sys.path)
  60. # ["C:UsersxxxDesktoppy",
  61. # "C:Python36python36.zip",
  62. # "C:Python36DLLs",
  63. # "C:Python36lib",
  64. # "C:Python36",
  65. # "C:Python36libsite-packages"]
  66. a = os.path.abspath(__file__) # 当前文件绝对路径
  67. BASE_DIR = os.path.dirname(a) # 存放c.py所在的绝对路径
  68. sys.path.append(BASE_DIR) # 可以加路径
  69. # 脚本后参数
  70. # 用以下命令启动
  71. # python demo.py hello
  72. print ("脚本路径", sys.argv[0])
  73. if len(sys.argv) > 1:
  74. print ( "脚本后参数为", sys.argv[1:])
  75. else:
  76. print ("无参数")
  77. # 脚本后参数为 ["hello"]