How to document a module in Python?

What is it. If you want to document a function or class, you put the line immediately after the definition. For example:

def foo(): """This function does nothing.""" pass 

But what about the module? How can I document what a .py file does?

+65
python documentation python-module
Sep 04 '08 at 16:06
source share
6 answers

For packages, you can document it in __init__.py . For modules, you can add docstring just to the module file.

All information here: http://www.python.org/dev/peps/pep-0257/

+49
04 Sep '08 at 16:12
source share

Add the documentation line as the first statement in the module .

 """ Your module verbose yet thorough docstring. """ import foo # ... 

For packages, you can add your documentation line to __init__.py .

+64
May 03 '14 at 23:29
source share

Here is an example of a Python Google Style style string about how a module can be documented. Basically, there is information about the module, how to execute it, as well as information about the module level variables and a list of ToDo elements.

 """Example Google style docstrings. This module demonstrates documentation as specified by the 'Google Python Style Guide'_. Docstrings may extend over multiple lines. Sections are created with a section header and a colon followed by a block of indented text. Example: Examples can be given using either the ''Example'' or ''Examples'' sections. Sections support any reStructuredText formatting, including literal blocks:: $ python example_google.py Section breaks are created by resuming unindented text. Section breaks are also implicitly created anytime a new section starts. Attributes: module_level_variable1 (int): Module level variables may be documented in either the ''Attributes'' section of the module docstring, or in an inline docstring immediately following the variable. Either form is acceptable, but the two should not be mixed. Choose one convention to document module level variables and be consistent with it. Todo: * For module TODOs * You have to also use ''sphinx.ext.todo'' extension .. _Google Python Style Guide: http://google.imtqy.com/styleguide/pyguide.html """ module_level_variable1 = 12345 def my_function(): pass ... ... 
+17
Jan 28 '17 at 19:34 on
source share

You do it the exact same way. Put the line as the first statement in the module.

+8
Sep 04 '08 at 16:12
source share

Easy, you just add a document line at the top of the module.

+4
Sep 04 '08 at 16:12
source share

For PyPI packages:

If you add similar lines to the __init__.py file as shown below

 """ Please refer to the documentation provided in the README.md, which can be found at gorpyter PyPI URL: https://pypi.org/project/gorpyter/ """ # <IMPORT_DEPENDENCIES> def setup(): """Verify your Python and R dependencies.""" 

Then you will get this with the daily help function.

help(<YOUR_PACKAGE>)

 DESCRIPTION Please refer to the documentation provided in the README.md, which can be found at gorpyter PyPI URL: https://pypi.org/project/gorpyter/ FUNCTIONS setup() Verify your Python and R dependencies. 

Please note that my DESCRIPTION help is caused by the presence of this first line of documentation at the very top of the file.

+2
Aug 16 '19 at 18:54
source share



All Articles