Search code examples
pythonperlpydocperldoc

How to view only doc text with pydoc


Being a perl developer I am aware of the perldoc utility. I am new to python and I was looking for same and found pydoc. But the problem with pydoc is it is also executing my script while I want to see only doc written inside triple quotes. Any help would be appreciated.


Solution

  • Probably the best way is to work in Python (instead of the command line). Let's take the module sys as an example. First import your module and then use help(), like this:

    import sys
    help(sys)
    

    If you want to see the docstrings:

    print(sys.__doc__)
    

    For modules you develop yourself, you can build in a guard so that the module is not executed, as described in the documentation:

    Note: In order to find objects and their documentation, pydoc imports the module(s) to be documented. Therefore, any code on module level will be executed on that occasion. Use an if __name__ == '__main__': guard to only execute code when a file is invoked as a script and not just imported.