How to use multiple paths for python modules documentation in different directories using sphinx autodoc?

4,794 views
Skip to first unread message

nshea

unread,
Feb 15, 2017, 6:05:18 PM2/15/17
to sphinx-users

I am using sphinx autodoc to document python modules in multiple directories. I already have the rst files for each python module and can use autodoc when declaring a path to one directory with the python modules but I am trying to document python modules from two different directories. In the conf.py file, I used:


sys.path.insert(0, os.path.abspath("../python_modules_A"))


but would like to have paths to both python_modules_A and python_modules_B, since my modules are in both directories. Given the structure of my project team, I would like to avoid having to restructure the modules into one directory since they are separated by helper functions in python_module_A and classes/objects for the main module python_modules_B modules.


Below is how my directories are configured:


sphinx_doc_setup

  conf.py

  index.rst

  a1.rst

  b1.rst

  a2.rst

  b2.rst

python_modules_A

  a1.py

  b1.py

python_modules_B

  a2.py

  b2.py


Thanks for any help!

Peter Burdine

unread,
Feb 16, 2017, 8:33:26 AM2/16/17
to sphinx-users
Have you tried changing your sys.path to include both?  Eg:
sys.path.insert(0, os.path.abspath(".."))



Then for autodoc tried:
.. automodule:: python_modules_A

.. automodule:: python_modules_B



How you include documentation for it may then depend on your module structure (eg do you have an __init__.py? does it define __all__?)

nshea

unread,
Feb 17, 2017, 7:43:04 PM2/17/17
to sphinx-users
The way my modules are structured, there is an __init__.py in both the python_modules_A and the python_modules_B directories. I declared two paths in my conf.py, one for python_modules_A and python_modules_B but when I run make.html, sphinx only recognizes the first path listed and ignores the second. 

For example, in my conf.py file, I used

sys.path.insert(0, os.path.abspath("../python_modules_A")) 
sys.path.insert(0, os.path.abspath("../python_modules_B"))

but get import errors for every python module in the python_modules_B directory. 

Another error that comes up when I list sys.path.insert(0, os.path.abspath("../python_modules_B")) first instead is that sphinx cannot recognize the path for functions/modules in python_modules_A that are imported into python_module_B modules. For example, in every module in python_module_B, I import a module/function defined in python_modules A called download parameters. Here is how it is listed below:

download_parameters.py is in python_modules_A directory
Below is the header and code in a2.py (which is in the python_modules_B directory):

import numpy as np
from python_modules_A.download_parameters import download_parameters
Class module_B
...
...

This error occurs: 

"from python_modules_A.download_parameters import download_parameters

ImportError: No module named python_modules_A.download_parameters"


Is there a way to fix this problem as well? Thanks. 

Komiya Takeshi

unread,
Feb 18, 2017, 10:19:57 AM2/18/17
to sphinx...@googlegroups.com
It seems your case is simply python's problem, not sphinx's.
How about this?

sys.path.insert(0, os.path.abspath(".."))

Thanks,
Takeshi KOMIYA
> --
> You received this message because you are subscribed to the Google Groups
> "sphinx-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sphinx-users...@googlegroups.com.
> To post to this group, send email to sphinx...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sphinx-users.
> For more options, visit https://groups.google.com/d/optout.

Nicholas Shea

unread,
Feb 18, 2017, 7:11:39 PM2/18/17
to sphinx...@googlegroups.com
Ah that got it to work. I also needed to specify the path in my automodule rst files. Originally I had the automodule declaration in my rst files as only: 
.. automodule:: a1
.. automodule:: a2

but when I switched it to

.. automodule:: python_modules_A.a1
.. automodule:: python_modules_B.a2

and used sys.path.insert(0, os.path.abspath("..")) in the conf.py file it worked.

Thanks so much for the help!


> To post to this group, send email to sphinx...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sphinx-users.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "sphinx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users+unsubscribe@googlegroups.com.

durveshpi...@gmail.com

unread,
Feb 21, 2018, 6:58:38 PM2/21/18
to sphinx-users
Thanks a lot. This seems to work for me as well.

> To post to this group, send email to sphinx...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sphinx-users.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "sphinx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages