有没有办法从 python 控制台中查看模块的源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2055110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:40:06  来源:igfitidea点击:

is there a way to view the source of a module from within the python console?

python

提问by tehryan

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?

如果我在 python 控制台中并且想查看特定模块的工作方式,是否有一种简单的方法可以转储源代码?

回答by Alok Singhal

Some of the methods of inspectmodule are well-suited for this purpose:

检查模块的一些方法非常适合此目的:

import module
import inspect
src = inspect.getsource(module)

回答by Micha? Marczyk

Using IPython, you can do this:

使用 IPython,你可以这样做:

In [1]: import pyparsing
In [2]: pyparsing.Word??

...and the source will be displayed, if possible. I guess this must use inspectunder the hood, but the added convenience of ? / ?? is fantastic.

...如果可能,将显示源。我想这必须inspect在引擎盖下使用,但是 ? / ?? 太棒了。

回答by gimel

Maybe

或许

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

?

?

See file.read().

file.read()