Sphinx Cannot Find My Python Files. Says 'no Module Named ...'
Solution 1:
This is the usual "canonical approach" to "getting started" applied to the case when your source code resides in a src
directory like Project/src
instead of simply being inside the Project
base directory.
Follows these steps:
Create a
docs
directory in yourProject
directory (it's from thisdocs
directory the commands in the following steps are executed).sphinx-quickstart
(choose separatesource
frombuild
. Places.html
and.rst
files in different folders).sphinx-apidoc -o ./source ../src
make html
This would yield the following structure (provided you .py
source files reside in Project/src
):
Project
|
├───docs
│ │ make.bat
│ │ Makefile
│ │
│ ├───build
│ └───source
│ │ conf.py
│ │ index.rst
│ │ modules.rst
│ │ stack.rst
│ │
│ ├───_static
│ └───_templates
└───src
stack.py
In your conf.py
you'd add (after step 2):
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join('..', '..', 'src')))
Also include in conf.py
:
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.napoleon']
And in index.rst
you'd link modules.rst
:
Welcome to Project's documentation!
================================
.. toctree::
:maxdepth: 2
:caption: Contents:
modules
Indices and tables
==================
* :ref:`genindex`* :ref:`modindex`* :ref:`search`
Your stack.rst
and modules.rst
were auto-generated by sphinx-apidoc
, no need to change them (at this point). But just so you know this is what they look like:
stack.rst
:
stack module
============
.. automodule:: stack
:members:
:undoc-members:
:show-inheritance:
modules.rst
:
src
===
.. toctree::
:maxdepth: 4
stack
After `make html` open `Project/docs/build/index.html` in your browser, the results:
and:
Solution 2:
sys.path.insert(0, os.path.abspath('../..'))
That's not correct. Steve Piercy's comment is not entirely on point (you don't need to add a __init__.py
since you're using a simple module) but they're right that autodoc will try to import the module and then inspect the content.
Hoever assuming your tree is
doc/conf.py
src/stack.py
then you're just adding the folder which contains your repository to the sys.path which is completely useless. What you need to do is add the src
folder to sys.path, such that when sphinx tries to import stack
it finds your module. So your line should be:
sys.path.insert(0, os.path.abspath('../src')
(the path should be relative to conf.py).
Of note: since you have something which is completely synthetic and should contain no secrets, an accessible repository or a zip file of the entire thing makes it much easier to diagnose issues and provide relevant help: the less has to be inferred, the less can be wrong in the answer.
Solution 3:
For me installing the package via setup.py
file and re-running corresponding commands fixed the problem:
$ python setup.py install
Post a Comment for "Sphinx Cannot Find My Python Files. Says 'no Module Named ...'"