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 3.9k times
27
-
THis question doesn't make sense. All Python modules are shipped as source. You can trivially read them. They're in the file system as source. Are you having trouble find the Python libraries? Can you not open a second window? – S.Lott Jan 13 '10 at 11:35
-
the 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
-
1This 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
40
Some of the methods of inspect module are well-suited for this purpose:
import module
import inspect
src = inspect.getsource(module)
Alok Singhal
- 88,099
- 18
- 124
- 155
-
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
8
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
- 82,286
- 11
- 198
- 212
0
gimel
- 78,638
- 10
- 72
- 104
-
-
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
-
-