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?
-
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– tehryan2010-01-15 06:00:29 +00:00Commented Jan 15, 2010 at 6: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– Jim Hunziker2016-04-08 18:19:03 +00:00Commented Apr 8, 2016 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– Lukas2018-07-04 13:23:52 +00:00Commented Jul 4, 2018 at 13:23
Add a comment
|
3 Answers
Some of the methods of inspect module are well-suited for this purpose:
import module
import inspect
src = inspect.getsource(module)
5 Comments
recolic
sometimes doesn't work. for example: see this screenshot: recolic.net/tmp/snap-0508-203529.png
luisfelipe18
@recolic link has expired
recolic
@LuisFelipe I have lost the screenshot... Sorry for that and just ignore my comment plz.
cowlinator
Does not work for "built-in" modules.
Marco Roy
Thank you! Ended up using this to peer into
pytz.country_names and pytz.lazy 👌4 Comments
Kugel
this dumps the content of any file.
KingMak
for this to work you would have to be in the directory where the module is placed. Sorta defeats the purpose in my opinion.
lux
You can use
__file__ instead of the static string.cowlinator
__file__ does not work for "built-in" modules