[Tutor] sys module - what does "It is always available" mean in the docs?

Peter Otten __peter__ at web.de
Thu Aug 21 15:14:52 CEST 2014


Flynn, Stephen (L & P - IT) wrote:

> The documentation (https://docs.python.org/3/library/sys.html) for
> Python 3.4.1 says that "This module provides access to some variables
> used or maintained by the interpreter and to functions that interact
> strongly with the interpreter. It is always available."
> 
> I interpreted that last sentence as "It's built in", so I made the
> assumption I didn't need to import it in order to use it.

"built-in" means that it is part of the interpreter executable. You still 
have to import it, but for built-in (and already loaded) modules that is 
only a cache lookup. That cache (called sys.modules) is part of the sys 
module, so if the sys module were not available even import would fail...

You can find out about other built-in modules with

>>> import sys
>>> sys.builtin_module_names
('_ast', '_bisect', '_codecs', '_collections', '_datetime', '_elementtree', 
[...]
>>> len(_)
54

> This is not the case - it still needs to be imported before I can use
> anything defined within it.
> 
> Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64
> bit (AMD64)] on win32
> Type "copyright", "credits" or "license()" for more information.
>>>> print(sys.maxsize)
> Traceback (most recent call last):
>   File "<pyshell#0>", line 1, in <module>
>     print(sys.maxsize)
> NameError: name 'sys' is not defined
>>>> import sys
>>>> print(sys.maxsize)
> 9223372036854775807
>>>>  
> 
> So, what does the documentation mean by explicitly pointing out that sys
> is always available? To me, it's misleading but I'm obviously missing a
> subtlety.
> 
> I've asked this question on /learnpython on reddit recently and the best
> explanation is that it's built by the interpreter at runtime, so it
> can't fail to import due to a broken installation. Any other offers?
> 
> 
> As an addendum, I fired up my local docs via "Module Docs" and "This is
> always available" *isn't* in the docs page for sys. I'd assumed they
> were built from the same sources but apparently not.




More information about the Tutor mailing list