Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop pydoc from running my Python program

Following on "Windows 7 - pydoc from cmd", I have the following problem. I prepared a simple, docstring-documented hello.py "hello world" script:

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

and saved it in C:\Python34\lib.

Then using Window's command-line, I changed the directory to C:\Python34\lib, and ran

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?

like image 777
talkaboutquality Avatar asked Oct 17 '15 19:10

talkaboutquality


People also ask

How do I quit pydoc?

Get out of pydoc by typing q to quit.

What does pydoc mean in Python?

Pydoc is the documentation generation system for Python. Say you can document your functions using the Pydoc standard and then it can be used to generate documentation in your code. Follow this answer to receive notifications.

How do I run pydoc in Python?

You can access the interactive shell in pydoc using it's help function. In order to do this, launch your terminal, and enter the python interactive shell. Now, import pydoc and then use the pydoc. help() command to launch the interactive shell.

How do I make Python wait before closing?

You can add raw_input('Press Enter to exit') right before your program would exit. It tells Python to wait for input before exiting.


1 Answers

pydoc imports the module to be documented. 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:

""" This module prints Hello, world
    More documentation.
"""
if __name__ == "__main__":
    print("Hello, world")
like image 87
falsetru Avatar answered Oct 17 '22 00:10

falsetru