[Tutor] Where is sys.py?
Steven D'Aprano
steve at pearwood.info
Mon Aug 22 01:13:37 CEST 2011
Lisi wrote:
> If sys.py is a file, it must be somewhere; but I can't find it. Where is it?
> I would like to look at it.
Others have already answered this, but consider how you might explore
the answer at the interactive interpreter:
>>> import os
>>> os.__file__
'/usr/lib/python2.5/os.pyc'
>>> import sys
>>> sys.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
So the sys module has no __file__ attribute, and therefore there is no
sys.py. Looking deeper:
>>> import inspect
>>> inspect.getfile(sys)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/inspect.py", line 347, in getfile
raise TypeError('arg is a built-in module')
TypeError: arg is a built-in module
Hmmm, not the most user-friendly of messages ("arg" is a built-in
module? how about *naming* it, all modules have a name!) but the answer
is clear: sys is part of the Python virtual machine itself, and does not
live in a file.
(Except, of course, that Python itself is compiled from files, and the
code creating sys will be in those.)
Furthermore:
>>> import math
>>> math.__file__
'/usr/lib/python2.5/lib-dynload/mathmodule.so'
So here we have a module that does live in a file, but it is a C
library, not a .py python file. (On Windows, it may be a .dll.)
> The modules within it must surely be somewhere too. But since I can't find
> sys, I obviously can't find the modules. Again, I'd like to look at them.
I don't understand what you mean by "modules within it".
Do you mean sys.modules?
That's easy:
>>> sys.modules['inspect']
<module 'inspect' from '/usr/lib/python2.5/inspect.pyc'>
--
Steven
More information about the Tutor
mailing list