简体   繁体   English

停止pydoc运行我的Python程序

[英]Stop pydoc from running my Python program

Following on " Windows 7 - pydoc from cmd ", I have the following problem. 继“ Windows 7 - pdoc from cmd ”之后,我遇到了以下问题。 I prepared a simple, docstring-documented hello.py "hello world" script: 我准备了一个简单的,docstring记录的hello.py“hello world”脚本:

""" This module prints Hello, world
    More documentation.
"""
print("Hello, world")

and saved it in C:\\Python34\\lib. 并将其保存在C:\\ Python34 \\ lib中。

Then using Window's command-line, I changed the directory to C:\\Python34\\lib, and ran 然后使用Window的命令行,我将目录更改为C:\\ Python34 \\ lib,然后运行

pydoc <full path to hello.py>

My output is: 我的输出是:

Hello, world
Help on module hello:

NAME
    hello

DESCRIPTION
    This module prints Hello, world
    More documentation.

FILE
    c:\python34\lib\hello.py

It's great that it printed the documentation, but first it ran the program. 它打印文档很棒,但首先运行程序。

How do I get it to NOT run the program, just print the documentation? 如何让它不运行程序,只需打印文档?

pydoc imports the module to be documented. pydoc导入要记录的模块。 So statements there are executed. 所以这些陈述已经执行。

If you can modify the code, guard the print line with if __name__ == "__main__" so the line is executed only when it is executed directly, but not when it is imported: 如果您可以修改代码,请使用if __name__ == "__main__"保护print行,以便仅在直接执行时执行该行,而不是在导入时执行:

""" This module prints Hello, world
    More documentation.
"""
if __name__ == "__main__":
    print("Hello, world")

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

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