How do you read the source code of a Python module?
Although cxw's answer is accurate and correct, I would suggest installing IPython, if you find yourself looking through the code of Python modules often. It's a better version of the python console:
$ pip install IPython...$ ipython
Then, to see the code of a class / function / module, type its name in and add ??
:
from Tkinter import *Tk??
class Tk
is in:
- Py 2.7:
Tkinter.py
- Py 3.6:
__init__.py
.
In general, when you import Foo
, if Foo
is a module implemented using Python code, and there is a Foo/__init__.py
in Python's search path, that file runs. Some more information here, plus official docs on modules for Python 2 or Python 3. That may not be the case for modules built in to the Python interpreter, such as 2.7's Tkinter
.