7

I am going through the official Python tutorial, and it says

One particular module deserves some attention: sys, which is built into every Python interpreter.

However, if I start the python interpreter and type, for example, sys.path, I get a NameError: name sys is not defined.

Thus, I need to import sys if I want to have access to it.

So what does it mean that it is 'built into every python interpreter' ?

2
  • 1
    It means that you do not have to install it. Commented Jul 11, 2018 at 4:11
  • Hm, I don't have do import sys. Surprisingly, sys.path just works. Commented Feb 21, 2023 at 15:17

3 Answers 3

8

It simply means that

import sys

will succeed, regardless of which version of Python you're using. It comes with every Python installation. In contrast, e.g.,

import mpmath

will fail unless you've installed the mpmath package yourself, or it came bundled with the specific Python installation you're using.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Does this mean that all modules of the standard library are 'built-in'? Or are there some modules of the standard library that also depend on the specific installation? I'm asking because I looked at mpmath and it's not part of the standard library, so I'm wondering if that's the reason why it's different.
Pretty much, except you need to apply common sense too. For example, the "The Python Standard Library" document describes the nis module too, which is an interface to "Sun Microsystem's NIS (Yellow Pages)", That doesn't exist on Windows, because it makes no sense on that OS. Much the same is true of all the modules described under the "Unix Specific Services" section of the stdlib manual. The vast majority of the modules described in that doc are available in all Python installations, though.
BTW, if you're new to Python, be aware that there are literally thousands of modules available (like mpmath) that aren't even mentioned in Python's documentation. You generally need to install those yourself if you want them, and they aren't maintained in the core Python project. All the "standard library" modules are part of the core Python project.
2

So what does it mean that it is 'built into every python interpreter' ?

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.
As you have noticed, a built-in module still needs to be imported like any other extension.

>>> 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')

Comments

2

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.

It is worthy to emphasize this, the "sys" module is built into the Python interpreter, CPython or JPython or others.

You will not find a "sys.py" like normal modules.

Help(sys) will show below info

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

By contrast: 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").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.