Module sys trong Python cung cấp các hàm và các biến được sử dụng để thao tác các phần khác nhau của môi trường chạy Python. Nó cho phép chúng ta truy cập các tham số và chức năng cụ thể của hệ thống. Sau đây là các hàm phổ biến trong Module Python: Hàm này cung cấp tên của các Module python hiện có đã import. Hàm này trả về một danh sách các đối số dòng lệnh
được truyền cho tập lệnh Python. Tên của tập lệnh luôn là mục ở chỉ số 0 và phần còn lại của các đối số được lưu trữ tại các chỉ mục tiếp theo. Hàm này cung cấp một cách hiệu quả cho cùng giá trị như exec_prefix. Nếu không chạy môi trường ảo, giá trị sẽ giữ nguyên. Nó được thiết lập trong quá trình khởi động Python, trước khi site.py chạy, có cùng giá trị với tiền tố. Nó là một dấu
hiệu của byteorder gốc cung cấp một cách hiệu quả để làm một cái gì đó. Hàm này trả về số nguyên lớn nhất của một biến. Hàm sys.modules
Hàm sys.argv
Hàm sys.base_exec_prefix
Hàm sys.base_prefix
Hàm sys.byteorder
Hàm sys.maxsize
Hàm sys.path
Hàm này hiển thị bộ PYTHONPATH trong hệ thống hiện tại. Đây là một biến môi trường là một đường dẫn tìm kiếm cho tất cả các Module python.
Hàm sys.stdin
Nó là một đối tượng chứa các giá trị gốc của stdin khi bắt đầu chương trình và được sử dụng trong quá trình hoàn thiện. Nó có thể khôi phục các tập tin.
Hàm sys.getrefcount
Hàm này trả về số tham chiếu của một đối tượng.
Hàm sys.exit
Hàm này được sử dụng để thoát khỏi console trong Python hoặc dấu nhắc lệnh và cũng được sử dụng để thoát khỏi chương trình trong trường hợp ngoại lệ.
Hàm sys.executable
Giá trị của hàm này là đường dẫn tuyệt đối đến trình thông dịch Python. Nó rất hữu ích để biết nơi python được cài đặt trên máy của người khác.
Hàm sys.platform
Giá trị của hàm này được sử dụng để xác định nền tảng mà chúng ta đang làm việc.
sys là thư viện builtin của Python, nó chứa các thông tin liên quan đến chính chương trình python interpreter bạn đang chạy, và cũng cho phép thay đổi một vài thông số. Nó không phải là thư viện chứa các thông tin liên quan đến hệ điều hành như nhiều người lầm tưởng [sys - system - và nghĩ là hệ điều hành].
help[sys] có thông tin đầy đủ về từng giá trị và function, hãy thử xem hệ thống của bạn có gì.
Script
import sysSKIP = ['path', 'modules', '__doc__', 'path_importer_cache', 'meta_path']
for attr in sorted[sys.__dict__.items[]]:
if not callable[attr[1]] and attr[0] not in SKIP:
print[attr[0], ':', attr[1]]
Kết quả khá dài, nhưng một vài thông tin đáng chú ý có thể kể tới:
- version: phiên bản Python đang dùng, bản C đã build chương trình Python interpreter đang chạy
version
: 3.5.2 [default, Nov 17 2016, 17:05:23]
[GCC 5.4.0 20160609]
- builtin_module_names: tên các module builtin
- platform : linux - nền tảng hệ điều hành đang chạy
- byteorder : little endian
- float_info : các thông số liên quan đến kiểu float
Kết quả
$ python look_sys.py
__name__ : sys
__package__ :
__spec__ : ModuleSpec[name='sys', loader=]
__stderr__ :
__stdin__ :
__stdout__ :
_mercurial : ['CPython', '', '']
_xoptions : {}
abiflags : m
api_version : 1013
argv : ['look_sys.py']
base_exec_prefix : /home/hvn/py35env
base_prefix : /home/hvn/py35env
builtin_module_names : ['_ast', '_bisect', '_codecs', '_collections', '_datetime', '_elementtree', '_functools', '_heapq', '_imp', '_io', '_locale', '_md5', '_operator', '_pickle', '_posixsubprocess', '_random', '_sha1', '_sha256', '_sha512', '_signal', '_socket', '_sre', '_stat', '_string', '_struct', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', 'array', 'atexit', 'binascii', 'builtins', 'errno', 'faulthandler', 'fcntl', 'gc', 'grp', 'itertools', 'marshal', 'math', 'posix', 'pwd', 'pyexpat', 'select', 'spwd', 'sys', 'syslog', 'time', 'unicodedata', 'xxsubtype', 'zipimport', 'zlib']
byteorder : little
copyright : Copyright [c] 2001-2016 Python Software Foundation.
All Rights Reserved.Copyright [c] 2000 BeOpen.com.
All Rights Reserved.Copyright [c] 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.Copyright [c] 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
dont_write_bytecode : False
exec_prefix : /home/hvn/py35env
executable : /home/hvn/py35env/bin/python
flags : sys.flags[debug=0, inspect=0, interactive=0, optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0, ignore_environment=0, verbose=0, bytes_warning=0, quiet=0, hash_randomization=1, isolated=0]
float_info : sys.float_info[max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1]
float_repr_style : short
hash_info : sys.hash_info[width=64, modulus=2305843009213693951, inf=314159, nan=0, imag=1000003, algorithm='siphash24', hash_bits=64, seed_bits=128, cutoff=0]
hexversion : 50660080
implementation : namespace[_multiarch='x86_64-linux-gnu', cache_tag='cpython-35', hexversion=50660080, name='cpython', version=sys.version_info[major=3, minor=5, micro=2, releaselevel='final', serial=0]]
int_info : sys.int_info[bits_per_digit=30, sizeof_digit=4]
maxsize : 9223372036854775807
maxunicode : 1114111
path_hooks : [, ]
platform : linux
prefix : /home/hvn/py35env
real_prefix : /usr
stderr :
stdin :
stdout :
thread_info : sys.thread_info[name='pthread', lock='semaphore', version='NPTL 2.23']
version : 3.5.2 [default, Nov 17 2016, 17:05:23]
[GCC 5.4.0 20160609]
version_info : sys.version_info[major=3, minor=5, micro=2, releaselevel='final', serial=0]
warnoptions : []
Một số functions: [từ help[sys]]
Functions:
displayhook[] -- print an object to the screen, and save it in builtins._
excepthook[] -- print an exception and
its traceback to sys.stderr
exc_info[] -- return thread-safe information about the current exception
exit[] -- exit the interpreter by raising SystemExit
getdlopenflags[] -- returns flags to be used for dlopen[] calls
getprofile[] -- get the global profiling function
getrefcount[] -- return the reference count for an object [plus one :-]
getrecursionlimit[] -- return the max recursion depth for the interpreter
getsizeof[] -- return the size of an object in
bytes
gettrace[] -- get the global debug tracing function
setcheckinterval[] -- control how often the interpreter checks for events
setdlopenflags[] -- set the flags to be used for dlopen[] calls
setprofile[] -- set the global profiling function
setrecursionlimit[] -- set the max recursion depth for the interpreter
settrace[] -- set the global debug tracing function
Xem chi tiết tại: //docs.python.org/3/library/sys.html
Hết.
HVN at //pymi.vn