I’ve got a problem visualizing the documentation of my project in Read the Docs. Everything works fine locally by executing the command make html, but in Read the Docs the functions that should be generated by the directive automodule don’t show up.
This is how my repository is structured.
In the conf.py I set the absolute path as follows: sys.path.insert(0, os.path.abspath('../..')), and then I used the following instruction for automatically describing the functions contained in the Python module in my documentation:
.. automodule:: library.module1
:members:
However, no functions are shown by compiling the doc in Read The Docs.
I also tried to install my project in a virtualenv by specifying a requirements.txt file in the RTD advanced settings, but this doesn’t work either. Any suggestions?
Related
In the documentation about sphinx extensions are some "config values". e.g. in sphinx.ext.autodoc.
But I do not know how to set these values. Is it a parameter on the shell I have to set?
There's a page on the docs that teaches how to use extensions. The page is here: https://www.sphinx-doc.org/en/master/usage/extensions/index.html
In this page you have the following description:
A list of strings that are module names of extensions. These can be extensions coming with Sphinx (named sphinx.ext.*) or custom ones.
This tells you that sphinx.ext.* are the built-ins extensions (You can confirm that from here)
Basically when it states some kind of configuration, it refers to the conf.py file that is generated when you run sphinx-quickstart, which is a quick-setup command for sphinx in general (Might be a good read: https://www.sphinx-doc.org/en/master/usage/quickstart.html)
The root directory of a Sphinx collection of plain-text document sources is called the source directory. This directory also contains the Sphinx configuration file conf.py, where you can configure all aspects of how Sphinx reads your sources and builds your documentation.
Sphinx comes with a script called sphinx-quickstart that sets up a source directory and creates a default conf.py with the most useful configuration values from a few questions it asks you.
Note that, as it's built-in, it's already setupped with your sphinx project.
But if you want to customize it somehow, you can use conf.py file with autodoc_* configurations (As listed here)
There are also some notes that might be useful for your case
For Sphinx (actually, the Python interpreter that executes Sphinx) to find your module, it must be importable. That means that the module or the package must be in one of the directories on sys.path – adapt your sys.path in the configuration file accordingly.
Will the Sphinx documentation engine successfully generate documentation on a project that doesn't import well? In particular my project has an exotic dependency. I don't want document generation to depend on this dependency.
Does Sphinx need to import my module and use introspection or does it parse?
If you're using the autodoc extension, then yes, your project must be importable. But sometimes it's possible to mock out dependencies in your conf.py (since, presumably, at the time of import, the dependencies are needed in name only). The Read the Docs documentation has an example of how to do this.
Core Sphinx doesn't touch your code at all. The autodoc extension does, and it indeed imports it:
For Sphinx (actually, the Python interpreter that executes Sphinx) to find your module, it must be importable.
Epydoc is not working good anymore, even after applying patches, so I'm trying to move to Sphinx.
I want to auto-generate documentation from a simple python file: test.py
After installing and running sphinx-quickstart-script I copied test.py to the source folder and typed:
sphinx-build -b html .\source .\build
But it only produces the minimiun html files, and it does not parse the test.py file
I'm run out of ideas.
Thanks for your help :)
You have to include sphinx.ext.autodoc as an extension in your conf.py (https://www.sphinx-doc.org/en/master/usage/configuration.html), than you can document your module using the .. automodule:: directive (or objects from this module with .. autoclass::, .. autofunction::, ...).
See https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html
For auto-generating the necessary .rst files you can use sphinx-apidoc.
This makes life much easier. So you don' t have to document each of your modules manually.
I want to generate documentation for my package. Every file in the project contains extensive documentation. Is there a way to quickly add my entire project to the documetation index?
I'd like to automatically generate some documentation for the entire project with as little as possible work. I started by adding the following to index.rst:
.. automodule:: mymodulename
:members:
All that seems to have done is document elenments in the __init__.py file (just a docstring) - is there any way I can make it document everything else? I'm looking to add absolutely everything defined in my package and have every single class, constant, function (etc) in the package be added to the appropriate index.
Can this be done?
You can use sphinx-apidoc.
From the official documentation: sphinx-apidoc is a tool for automatic generation of Sphinx sources that, using the autodoc extension, document a whole package in the style of other automatic API documentation tools.
An usage example could be (from your project root directory):
$ sphinx-apidoc . --full -o doc -H 'MyProject' -A 'MyName' -V '1.0'
A doc directory would be created with everything ready inside.
You can also adjust the settings of your documentation editing the auto-generated conf.py file.
Other useful information can be found in similar question posted here.
I have a Python project that has the following structure:
package1
class.py
class2.py
...
package2
otherClass.py
otherClass2.py
...
config
dev_settings.ini
prod_settings.ini
I wrote a setup.py file that converts this into an egg with the same file structure. (When I examine it using a zip program the structure seems identical.) The funny thing is, when I run the Python code from my IDE it works fine and can access the config files; but when I try to run it from a different Python script using the egg, it can't seem to find the config files in the egg. If I put the config files into a directory relative to the calling Python script (external to the egg), it works - but that sort of defeats the purpose of having a self-contained egg that has all the functionality of the program and can be called from anywhere. I can use any classes/modules and run any functions from the egg as long as they don't use the config files... but if they do, the egg can't find them and so the functions don't work.
Any help would be really appreciated! We're kind of new to the egg thing here and don't really know where to start.
The problem is, the config files are not files anymore - they're packaged within the egg. It's not easy to find the answer in the docs, but it is there. From the setuptools developer's guide:
Typically, existing programs manipulate a package's __file__ attribute in order to find the location of data files. However, this manipulation isn't compatible with PEP 302-based import hooks, including importing from zip files and Python Eggs.
To access them, you need to follow the instructions for the Resource Management API.
In my own code, I had this problem with a logging configuration file. I used the API successfully like this:
from pkg_resources import resource_stream
_log_config_file = 'logging.conf'
_log_config_location = resource_stream(__name__, _log_config_file)
logging.config.fileConfig(_log_config_location)
_log = logging.getLogger('package.module')
See Setuptools' discussion of accessing pacakged data files at runtime. You have to get at your configuration file a different way if you want the script to work inside an egg. Also, for that to work, you may need to make your config directory a Python package by tossing in an empty __init__.py file.