Python的sys包

sys.path 系统环境变量,导入包和文件时,从其路径中加载包和文件
# coding:utf-8

import sys

if __name__ == '__main__':
    print(sys.path)  # ['G:\\workSpace\\py_d', 'G:\\workSpace\\py_d', 'H:\\PyCharm 2019.3.5\\plugins\\python\\helpers\\pycharm_display', 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip', 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs', 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib', 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32', 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages', 'H:\\PyCharm 2019.3.5\\plugins\\python\\helpers\\pycharm_matplotlib_backend']

sys.getdefaultencoding() 获取当前默认编码
# coding:utf-8

import sys

if __name__ == '__main__':
    print(sys.getdefaultencoding())  # utf-8
sys.modules 查看当前已加载的模块信息
# coding:utf-8

import sys

if __name__ == '__main__':
    print(sys.modules)  # {'sys': <module 'sys' (built-in)>, 'builtins': <module 'builtins' (built-in)>, '_frozen_importlib': <module '_frozen_importlib' (frozen)>, '_imp': <module '_imp' (built-in)>, '_thread': <module '_thread' (built-in)>, '_warnings': <module '_warnings' (built-in)>, '_weakref': <module '_weakref' (built-in)>, 'zipimport': <module 'zipimport' (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)>, 'encodings': <module 'encodings' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\encodings\\__init__.py'>, 'codecs': <module 'codecs' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\codecs.py'>, '_codecs': <module '_codecs' (built-in)>, 'encodings.aliases': <module 'encodings.aliases' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\encodings\\aliases.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\encodings\\utf_8.py'>, '_signal': <module '_signal' (built-in)>, '__main__': <module '__main__' from 'G:/workSpace/py_d/sys_test.py'>, 'encodings.latin_1': <module 'encodings.latin_1' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\encodings\\latin_1.py'>, 'io': <module 'io' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\io.py'>, 'abc': <module 'abc' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\abc.py'>, '_abc': <module '_abc' (built-in)>, 'site': <module 'site' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site.py'>, 'os': <module 'os' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\os.py'>, 'stat': <module 'stat' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\stat.py'>, '_stat': <module '_stat' (built-in)>, '_collections_abc': <module '_collections_abc' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\_collections_abc.py'>, 'ntpath': <module 'ntpath' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\ntpath.py'>, 'genericpath': <module 'genericpath' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\genericpath.py'>, 'os.path': <module 'ntpath' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\ntpath.py'>, '_sitebuiltins': <module '_sitebuiltins' from 'C:\\Users\\xieruixiang\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\_sitebuiltins.py'>, 'sitecustomize': <module 'sitecustomize' from 'H:\\PyCharm 2019.3.5\\plugins\\python\\helpers\\pycharm_matplotlib_backend\\sitecustomize.py'>}

sys.planform 获取当前操作系统
# coding:utf-8

import sys

if __name__ == '__main__':
    print(sys.platform)  # win32
sys.exit(status) 退出解释器
# coding:utf-8

import sys

if __name__ == '__main__':
    '''
    sys.exit(status=None)
    如果 status是None 或者status是空字符串 或者status是仅仅由空格组成的字符串时(string.isspace() == True),退出解释器不会抛出异常
    其它会 raise SystemExit(status) 抛出异常
    '''
    try:
        sys.exit('')
    except SystemExit as e:
        print(e)  # 1

sys.argv 获取外部传入参数
# coding:utf-8

import sys

if __name__ == '__main__':
    '''
    sys.argv 返回list
    list[0] 是当前脚本路径
    0索引以后的是从外部传入的参数
    '''
    print(sys.argv)
xieruixiang@DESKTOP-Q6LSD0B MINGW64 /g/workSpace/py_d
$ python sys_test.py a b c
['sys_test.py', 'a', 'b', 'c']

sys.version 获取当前python版本
# coding:utf-8

import sys

if __name__ == '__main__':
    print(sys.version)  # 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 09:44:33) [MSC v.1900 32 bit (Intel)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值