7

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?

1
  • IIRC, That's not how it works ... pydoc (and a number of other auto-documentation tools e.g. sphinx) import the source and look for the __doc__ attributes that python adds to classes/functions, etc. The only tool that I know of which doesn't import the source is epydoc. That said, usually you can guard any statements that you don't want to execute in an if __name__ == '__main__': block. Commented Oct 17, 2015 at 19:05

1 Answer 1

8

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")
Sign up to request clarification or add additional context in comments.

3 Comments

__name__ doesn't change whether I import the module or run it with pydoc. I can't find any info on what changes when pydoc is run.
@nurettin, Let's say the file is abcd.py. Then, __name__ of the module is abcd if you import it or run it with pydoc.__name__ is __main__ if you run (not import) it with python directly. docs.python.org/library/__main__.html
that's right, and it makes no difference whether we import or run with pydoc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.