【轉載】Python常用模塊之sys

原文鏈接

sys模塊提供了一系列有關Python運行環境的變量和函數。

常見用法
sys.argv
可以用sys.argv獲取當前正在執行的命令行參數的參數列表(list)。
變量 解釋
sys.argv[0] 當前程序名
sys.argv[1] 第一個參數
sys.argv[0] 第二個參數


參考代碼:

# encoding: utf-8
# filename: argv_test.py
import sys

# 獲取腳本名字
print 'The name of this program is: %s' %(sys.argv[0])
# 獲取參數列表
print 'The command line arguments are:'
for i in sys.argv:
    print i
# 統計參數個數
print 'There are %s arguments.'%(len(sys.argv)-1)

運行結果:

E:\p>python argv_test.py arg1 arg2 arg3
The name of this program is: argv_test.py
The command line arguments are:
argv_test.py
arg1
arg2
arg3
There are 3 arguments.

sys.platform
獲取當前執行環境的平臺,如win32表示是Windows 32bit操作系統,linux2表示是linux平臺;

# linux 
>>> import sys
>>> sys.platform
'linux2'

# windows
>>> import sys
>>> sys.platform
'win32'

sys.path
path是一個目錄列表,供Python從中查找第三方擴展模塊。在python啓動時,sys.path根據內建規則、PYTHONPATH變量進行初始化。

>>> sys.path

['', 'E:\\Python27\\Lib\\idlelib', 'C:\\Windows\\system32\\python27.zip', 'E:\\Python27\\DLLs', 'E:\\Python27\\lib', 'E:\\Python27\\lib\\plat-win', 'E:\\Python27\\lib\\lib-tk', 'E:\\Python27', 'E:\\Python27\\lib\\site-packages']

**有時候爲了讓python能夠找到我們自己定義的模塊,需要修改sys.path的內容,比如:**
# 在path的開始位置 插入test
>>> sys.path.insert(0,'test')
>>> sys.path
['test', '', 'E:\\Python27\\Lib\\idlelib', 'C:\\Windows\\system32\\python27.zip', 'E:\\Python27\\DLLs', 'E:\\Python27\\lib', 'E:\\Python27\\lib\\plat-win', 'E:\\Python27\\lib\\lib-tk', 'E:\\Python27', 'E:\\Python27\\lib\\site-packages']
# 可以成功import test
>>> import test
# 找不到 other 這個模塊
>>> import other
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    import other
ImportError: No module named other
# 需要添加path
>>> sys.path.insert(0,'other')
>>> import other


也可以用sys.path.append(“mine module path”)來添加自定義的module。

sys.builtin_module_names
sys.builtin_module_names返回一個列表,包含內建模塊的名字。如:

>>> import sys
>>> print sys.builtin_module_names
('__builtin__', '__main__', '_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections', '_csv', '_functools', '_heapq', '_hotshot', '_io', '_json', '_locale', '_lsprof', '_md5', '_multibytecodec', '_random', '_sha', '_sha256', '_sha512', '_sre', '_struct', '_subprocess', '_symtable', '_warnings', '_weakref', '_winreg', 'array', 'audioop', 'binascii', 'cPickle', 'cStringIO', 'cmath', 'datetime', 'errno', 'exceptions', 'future_builtins', 'gc', 'imageop', 'imp', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', 'nt', 'operator', 'parser', 'signal', 'strop', 'sys', 'thread', 'time', 'xxsubtype', 'zipimport', 'zlib')
代碼示例:

# encoding: utf-8
# find_module.py

import sys

# print sys.builtin_module_names

def find_module(module):
    if module in sys.builtin_module_names:
        print module," => ","__builtin__"
    else:
        print module,"=> ",__import__(module).__file__

find_module('os')
find_module('sys')
find_module('strop')
find_module('zlib')
find_module('string')

# 運行結果:
>>> 
======================== RESTART: E:/p/find_module.py ========================
os =>  E:\Python27\lib\os.pyc
sys  =>  __builtin__
strop  =>  __builtin__
zlib  =>  __builtin__
string =>  E:\Python27\lib\string.pyc
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章