Open chamaoskurumi opened 4 years ago
Hey, that's really weird. Not sure why it's happening.
Could you show the logs for sphinx-action for when it generates those empty documentation files? Alternatively, you can also just push your project to Github so I can try recreating it myself (I tried looking through you repos but couldn't find it).
I initially ran into the same issue, but was able to resolve (and responded on SO). If you want to compare notes, here's the link to my repo .. it's very much work in progress, but the GitHub actions work.
PS: There are some differences here as I use setuptools
to install my package, but the outcome was the same. In my case, I actually don't think that it's an issue with the action itself, as I have faint recollections of running into this earlier for sphinx documentation generated for a GitLab repo.
@chamaoskurumi Is the workflow running, passing, and not generating docs, or is the workflow failing? Can you share a link to your GitHub Actions run?
Sorry @adam-grant-hendry , I cannot reproduce the error now as I don't have access anymore the code I used back then. Unfortunately I also can't remember if the workflow passed or not, but I'm pretty sure it didn't.
@chamaoskurumi No worries. Thanks for replying back! I agree with @ischoegl that your package was most likely not importable, which can cause this to happen. The sphinx.ext.autodoc
docs state:
For Sphinx (actually, the Python interpreter that executes Sphinx) to find your module, it must be importable.
This can be done in one of two ways:
Make an editable install, i.e. pip install -e .
(More on that here in the pip.pypa
docs). This was the answer given by @ischoegl in your SO post
Manually add your package to sys.path
in your conf.py
files, which has been the traditional/"old-school" recommendation. The problem with this approach is if the interpreter can't find your module (i.e. a bad path is used) or it encounters an error when importing your package, then it still won't generate your docs. (That is, even if sys.path
has a path, it might not be able to import that path.)
I generally don't recommend 2, but if you choose it, you can safeguard against this from happening again by using importlib.import_module
in a try...except
clause:
from importlib import import_module
package_name = 'my_package' # Replace with your package name
try:
import_module(package_name)
except ImportError as err:
raise ImportError(f'Package {package_name} could not be imported.')
It's also generally considered best practice nowadays to use the pathlib
module instead of os.path
, especially when mucking with sys.path
. In fact, pathlib.Path
behaves similarly to sphinx.path
: both treat paths as objects instead of strings (more on why that makes sense in this realpython.com article).
The second recommendation in your SO post to use __file__
to get an absolute path to your package is also accurate, but there are some caveats. Barry Warsaw gave a great PyCon talk on this: I highly recommend watching it to everyone I talk to.
Ultimately, the issue comes down to your package being importable, so I recommend 1 like @ischoegl did. It's much simpler and cleaner all around. Just do import mypackage
and Python will automatically error if it can't import it.
@ammaraskar @chamaoskurumi Can this issue be closed?
Tl;dr
When I create a sphinx doc locally it builds as expected, but sphinx-action creates an empty doc. It must have to do with the sphinx-action not finding the target python modules as specified in
conf.py
.Any ideas to configure the sphinx-action or
conf.py
correctly?Expected Sphinx Doc
When I build the sphinx doc locally on my machine via
cd docs/ && make html
the resulting html looks as expectedEmpty Sphinx Doc generated by sphinx-action
My
.github/workflows/sphinx_action.yml
includesand generates an empty skeleton of a Sphinx Doc
Project Setup
Project Structure
conf.py
ConfigurationMy
docs/conf.py
looks as follows. (Mind that I added three entries to thesys.path
manually in order to make Sphinx find all python modules.)Pipfile
Note: I asked the same question on SO.