简体   繁体   English

如何从 cmd 读取 python 模块的文档字符串/帮助?

[英]How can I read the docstring/help of a python module from cmd?

Consider that you are in the Windows command prompt or similar command line environment.假设您处于 Windows 命令提示符或类似的命令行环境中。 How can you get info about a Python module from its docstring printed to the console?如何从打印到控制台的文档字符串中获取有关 Python 模块的信息?

Ideally you will want to load the module without executing it, which could have side effects.理想情况下,您希望在不执行的情况下加载模块,这可能会产生副作用。 This is supported by Python's ast module, which even has a helper for getting docstrings.这是由 Python 的ast模块支持的,它甚至有一个获取文档字符串的帮助程序。 Try this:试试这个:

python3 -c"import ast, sys; a = ast.parse(open(sys.argv[1]).read()); print(ast.get_docstring(a))" "$1"

The Shortut (Hack)捷径(黑客)

Generally (in 2.7):通常(在 2.7 中):

python -c"print 'Hello world'"

(in 3.x): (在 3.x 中):

python -c"print('Hello world')"

will output: Hello world将输出: Hello world

But if you pass -c as an argument into a module, something else happens.但是,如果您将-c作为参数传递给模块,则会发生其他事情。

For an example, navigate to [your Python folder]\\Tools\\Scripts .例如,导航到[your Python folder]\\Tools\\Scripts If your script does not take parameter -c , a shortcut is simply to run:如果您的脚本不带参数-c ,则只需运行一个快捷方式:

python reindent.py -c

This will result in an error for the argument: "-c not recognized", but it will also return the docstring to the console.这将导致参数错误:“-c 无法识别”,但它也会将文档字符串返回到控制台。 (One limitation is that the output cannot be routed to the clipboard using |clip .) (一个限制是不能使用|clip将输出路由到剪贴板。)

Generally, if your script myscript.py contains a docstring and expects no argument -c :通常,如果您的脚本myscript.py包含一个文档字符串并且不需要参数-c

python myscript.py -c

returns返回

option -c not recognized
[__docstring__]

The Works作品

Once you are in the folder of reindent.py you can get an error-free docstring:进入reindent.py文件夹后,您可以获得无错误的文档字符串:

python -c"import reindent; print reindent.__doc__"

For producing a browsable help text, which prints both the docstring and lists the containing classes, functions, and global variables, use:要生成可浏览的帮助文本,打印文档字符串并列出包含的类、函数和全局变量,请使用:

python -c"import reindent; help(reindent)"

To output to the clipboard only (Warning: Contents will be replaced!):仅输出到剪贴板(警告:内容将被替换!):

python -c"import reindent; help(reindent)"|clip

Deeper更深

Now that you have figured out what classes and functions are accessible (see above), you can retrieve the methods of a class and inner docstrings:现在您已经弄清楚哪些类和函数是可访问的(见上文),您可以检索类和内部文档字符串的方法:

python -c"from reindent import Reindenter; help(Reindenter)"

If you mean print interactively, just start python without any arguments to get a REPL (read–eval–print loop).如果您的意思是交互式打印,只需启动 python 而不带任何参数即可获得 REPL(读取-评估-打印循环)。

python
import mypackage
help(mypackage)
dir(mypackage)

executed one after another and so forth.一个接一个地执行等等。

If you mean programmatically, see @noumenal's answer.如果您的意思是以编程方式,请参阅@noumenal 的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM