每个 python 解释器都内置了 sys 模块是什么意思?
What does it mean that the sys module is built into every python interpreter?
我正在阅读官方 Python 教程,它说
One particular module deserves some attention: sys, which is built into every Python interpreter.
但是,如果我启动 python 解释器并输入,例如 sys.path
,我会得到一个 NameError: name sys is not defined
。
因此,如果我想访问它,我需要导入 sys
。
那么'built into every python interpreter'是什么意思呢?
简单来说就是
import sys
将成功,无论您使用哪个版本的 Python。它随每个 Python 安装一起提供。相反,例如,
import mpmath
除非您自己安装了 mpmath
软件包,否则它将失败,或者它与您正在使用的特定 Python 安装捆绑在一起。
So what does it mean that it is 'built into every python interpreter'
?
sys
模块 written in C and compiled into the Python interpreter itself. Depending on the version of the interpreter, there may be more modules of this kind — sys.builtin_module_names
列出了所有这些。
正如您所注意到的,内置模块仍然需要像任何其他扩展一样 import
ed。
>>> import sys
>>> sys.builtin_module_names
('_ast', '_codecs', '_collections', '_functools', '_imp', '_io', '_locale', '_operator', '_signal', '_sre', '_stat', '_string', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', 'atexit', 'builtins', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'posix', 'pwd', 'sys', 'time', 'xxsubtype', 'zipimport')
The sys module is written in C and compiled into >the Python interpreter itself. Depending on the >version of the interpreter, there may be more >modules of this kind — sys.builtin_module_names >lists them all.
值得强调的是,"sys"模块内置于Python解释器,CPython或JPython或其他。
您不会找到像普通模块那样的"sys.py"。
Help(sys) 将显示以下信息
Help on built-in module sys:
NAME
sys
FILE
*(built-in)*
相比之下:
帮助(os)
Help on module os:
NAME
os - OS routines for Mac, NT, or Posix depending on what system we're on.
FILE
*/usr/lib64/python2.7/os.py*
与C相比,"sys"在某种程度上可以看作是LIBC("libc.so.7")的一部分。
我正在阅读官方 Python 教程,它说
One particular module deserves some attention: sys, which is built into every Python interpreter.
但是,如果我启动 python 解释器并输入,例如 sys.path
,我会得到一个 NameError: name sys is not defined
。
因此,如果我想访问它,我需要导入 sys
。
那么'built into every python interpreter'是什么意思呢?
简单来说就是
import sys
将成功,无论您使用哪个版本的 Python。它随每个 Python 安装一起提供。相反,例如,
import mpmath
除非您自己安装了 mpmath
软件包,否则它将失败,或者它与您正在使用的特定 Python 安装捆绑在一起。
So what does it mean that it is 'built into every python interpreter' ?
sys
模块 written in C and compiled into the Python interpreter itself. Depending on the version of the interpreter, there may be more modules of this kind — sys.builtin_module_names
列出了所有这些。
正如您所注意到的,内置模块仍然需要像任何其他扩展一样 import
ed。
>>> import sys
>>> sys.builtin_module_names
('_ast', '_codecs', '_collections', '_functools', '_imp', '_io', '_locale', '_operator', '_signal', '_sre', '_stat', '_string', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', 'atexit', 'builtins', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'posix', 'pwd', 'sys', 'time', 'xxsubtype', 'zipimport')
The sys module is written in C and compiled into >the Python interpreter itself. Depending on the >version of the interpreter, there may be more >modules of this kind — sys.builtin_module_names >lists them all.
值得强调的是,"sys"模块内置于Python解释器,CPython或JPython或其他。
您不会找到像普通模块那样的"sys.py"。
Help(sys) 将显示以下信息
Help on built-in module sys:
NAME
sys
FILE
*(built-in)*
相比之下: 帮助(os)
Help on module os:
NAME
os - OS routines for Mac, NT, or Posix depending on what system we're on.
FILE
*/usr/lib64/python2.7/os.py*
与C相比,"sys"在某种程度上可以看作是LIBC("libc.so.7")的一部分。