27

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?

tehryan
  • 717
  • 3
  • 8
  • 13
  • 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
  • 1
    This 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 Answers3

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
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

Maybe

print open('mymodule.py').read()

?

See file.read().

gimel
  • 78,638
  • 10
  • 72
  • 104