Sphinx: Documentation Generator Tool
First install
Sphinx
&sphinx-rtd-theme
withpip install -r requirements.txt
-
Create directory
docs
(adjacent to yourapp
), open it & run:sphinx-quickstart
-
Go one directory up from
docs
(back to parent directory) & run:sphinx-apidoc -o docs app
-
Changes in file
docs/conf.py
- [line 6]
import os, sys sys.path.insert(0, os.path.abspath('..'))
- [line 20]
extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.duration', 'sphinx.ext.todo', 'sphinx.ext.viewcode' ]
- [line 33]
html_theme = 'sphinx_rtd_theme'
-
Changes in file
docs/index.rst
- [line 10] Change the
:maxdepth:
number to0
(it won't crawl sub-modules, we'll add them manually) - [line 13] Add only
modules
text (it callsdocs/modules.rst
on build)
- [line 10] Change the
-
Changes in file
docs/modules.rst
- [line 7+] Add your module names (example:
app.module_name
) to include them in the docs generation
- [line 7+] Add your module names (example:
-
Open
docs
directory again and run:make.bat html
-
Open
docs/_build/index.html
in a web-browser.
- Run
run.py
which runsapp
package and doesn't callapp.__main__
python -B run.py
- Run
app.__main__
file
python -m app
- Run
app.__main__
file without caching the bytecode
python -B -m app
- Argument
-B
skips writing compiled bytecode (no__pycache__
folders are created) - Argument
-m
runs as a module
-
In the context of
from . import file
, the.
ispackage_name
, so importing.
importspackage_name
, which runspackage_name/__init__.py
. -
__init__.py
is run when you import a package into a running python program, which does not do anything as its only purpose is to mark the directory as a package. It contains most of the code and defines all the classes. -
__main__.py
is run as'__main__'
when you run a package as the main program. For instance,python -m package_name
at a command line runspackage_name/__main__.py
, which starts the package.