简体   繁体   English

sys模块是否构建在每个python解释器中是什么意思?

[英]What does it mean that the sys module is built into every python interpreter?

I am going through the official Python tutorial, and it says 我正在阅读官方的Python教程,它说

One particular module deserves some attention: sys, which is built into every Python interpreter. 一个特定的模块值得注意:sys,它内置于每个Python解释器中。

However, if I start the python interpreter and type, for example, sys.path , I get a NameError: name sys is not defined . 但是,如果我启动python解释器并输入例如sys.path ,我会得到一个NameError: name sys is not defined

Thus, I need to import sys if I want to have access to it. 因此,如果我想访问它,我需要导入sys

So what does it mean that it is 'built into every python interpreter' ? 那么它是什么意思“它内置于每个python解释器中”?

It simply means that 它只是意味着

import sys

will succeed, regardless of which version of Python you're using. 无论您使用的是哪个版本的Python,都会成功。 It comes with every Python installation. 它随每个Python安装一起提供。 In contrast, eg, 相比之下,例如,

import mpmath

will fail unless you've installed the mpmath package yourself, or it came bundled with the specific Python installation you're using. 除非您自己安装了mpmath软件包,否则它将失败,或者它与您正在使用的特定Python安装捆绑在一起。

So what does it mean that it is 'built into every python interpreter' ? 那么它是什么意思“它内置于每个python解释器中”?

The sys module is written in C and compiled into the Python interpreter itself. sys模块用C语言编写,并编译成Python解释器本身。 Depending on the version of the interpreter, there may be more modules of this kind — sys.builtin_module_names lists them all. 根据解释器的版本,可能会有更多此类模块 - sys.builtin_module_names列出所有模块。
As you have noticed, a built-in module still needs to be import ed like any other extension. 正如您所注意到的,内置模块仍然需要像任何其他扩展一样import

>>> 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. sys模块用C语言编写,并编译成> Python解释器本身。 Depending on the >version of the interpreter, there may be more >modules of this kind — sys.builtin_module_names >lists them all. 根据解释器的>版本,可能会有更多>此类模块 - sys.builtin_module_names>将它们全部列出。

It is worthy to emphasize this, the "sys" module is built into the Python interpreter, CPython or JPython or others. 值得强调的是,“sys”模块内置于Python解释器,CPython或JPython等。

You will not find a "sys.py" like normal modules. 您将找不到像正常模块一样的“sys.py”。

Help(sys) will show below info 帮助(sys)将显示以下信息

Help on built-in module sys:
NAME
    sys
FILE
    *(built-in)*

By contrast: help(os) 相比之下:help(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*

Comparing with C, "sys" could be somewhat treated as a part of LIBC("libc.so.7"). 与C相比,“sys”可以在某种程度上被视为LIBC(“libc.so.7”)的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM