Search code examples
pythonpython-sphinxrestructuredtext

How do I access a variable in sphinx conf.py from my .rst file?


I'm brand new to Sphinx and reStructuredText.

Inside my Sphinx conf.py I defined:

version = '0.0.2'

Inside of tutorial.rst I want access the version variable and display 0.0.2 in my html file. I tried:

version

|version|

:version:

.. |version|

.. :version:

I succeeded in testing the contents of the version.

.. ifconfig:: version == '0.0.2'

    This prints!

This was encouraging, but not what I want.


Solution

  • If you want this to be available everywhere, you can do something like this:

    conf.py

    rst_epilog = """
    .. |ProjectVersion| replace:: Foo Project, version {versionnum}
    """.format(
    versionnum = version,
    )
    

    file.rst

    As one may see, we have made significant strides in |ProjectVersion| 
    and hope that you enjoy the product!
    

    The rst_epilog list makes items within it globally-available to compiled .rst files. See http://www.sphinx-doc.org/en/master/usage/configuration.html#confval-rst_epilog