if I'm in the python console and I want to see how a particular module works, is there an easy way to dump the source?
Asked
Active
Viewed 5.0k times
31
-
1the question was wheteher or not there was an easy way to do it from within the console. The inspect module is exactly what I was looking for. – tehryan Jan 15 '10 at 06:00
-
Voting to re-open because the question linked as a duplicate asks about a function rather than a module. The answer is the same, but someone searching for this question would not necessarily ask the other question. – Jim Hunziker Apr 08 '16 at 18:19
-
2This question is not a duplicate. It asks about features of the Python console and not for code inspection. Therefore Michał Marczyk is answering it correctly for the IPython console. – Lukas Jul 04 '18 at 13:23
3 Answers
45
Some of the methods of inspect module are well-suited for this purpose:
import module
import inspect
src = inspect.getsource(module)

Alok Singhal
- 93,253
- 21
- 125
- 158
-
sometimes doesn't work. for example: see this screenshot: https://recolic.net/tmp/snap-0508-203529.png – recolic May 09 '19 at 03:36
-
-
@LuisFelipe I have lost the screenshot... Sorry for that and just ignore my comment plz. – recolic Nov 18 '20 at 03:46
-
2
9
Using IPython, you can do this:
In [1]: import pyparsing
In [2]: pyparsing.Word??
...and the source will be displayed, if possible. I guess this must use inspect
under the hood, but the added convenience of ? / ?? is fantastic.

Michał Marczyk
- 83,634
- 13
- 201
- 212
1
-
-
6for this to work you would have to be in the directory where the module is placed. Sorta defeats the purpose in my opinion. – KingMak Oct 12 '17 at 05:15
-
-