1

I have the following folder structure for a package project (simplified):

projectname
├── docs
├── packagename
│   ├── somemodule
│   |   ├── __init__.py (second)
|   │   └── somescript.py
│   └── __init__.py (first)
├── setup.cfg
└── pyproject.toml

In first __init__.py I do from . import somemodule. In the second __init__.py I do from .somescript import *. And somescript.py contains some function my_sum. The problem is that sphinx doesn't see the function. It only sees module packagename.somemodule - there is no function doscstring for my_sum in generated documentation. The function works well if I install the library using pip install . from the projectname folder.
I'm sure the problem is in the folder structure or imports because there was no problem when somescript.py was placed directly in packagename folder.

Additional information (maybe useful, maybe not):
I use Read The Docs.
Part of .readthedocs.yaml:

python:
  install:
    - method: pip
      path: .

UPD:
Read The Docs generates the following warnings:

WARNING: autodoc: failed to import module 'somescript' from module 'packagename'; the following exception was raised:
No module named 'packagename.somescripts'
WARNING: html_static_path entry '_static' does not exist

UPD2:
I fixed the warning from autodoc by fixing docs/source/packagename.rst but I still have the problem

pascalamin
  • 83
  • 1
  • 7
  • Are there any error messages? Have you added a path to `sys.path`? See (for example) https://stackoverflow.com/q/41925973/407651 – mzjn Jan 07 '22 at 15:37
  • @mzjn no errors, although there are some warnings. I included `sys.path.insert(0, os.path.abspath('.')) ; sys.path.insert(0, os.path.abspath('../')) ; sys.path.insert(0, os.path.abspath('../..'))` in the `conf.py`, if it was your question – pascalamin Jan 07 '22 at 15:41
  • @mzjn it's strange because I have no errors when I install the library locally (from another environment and folder of course) – pascalamin Jan 07 '22 at 15:49
  • @mzjn yes, it was my mistake. Actually, it should be `packagename.somescript` – pascalamin Jan 07 '22 at 15:53
  • @mzjn I'm trying to find the error right now but if I fail I think I will produce the full example. I found that `docs/source/packagename.rst` contains `automodule:: scriptname` but it should (it was only correct before I made folder structure changes). How do I auto-update this file? – pascalamin Jan 07 '22 at 16:19
  • I changed `docs/source/packagename.rst` and it fixed the warning but I still have the problem – pascalamin Jan 07 '22 at 16:28
  • @mzjn there is no instruction to function (from docstrings) in the documentation I generate – pascalamin Jan 07 '22 at 16:32
  • @mzjn basically the generated documentation is empty – pascalamin Jan 07 '22 at 16:32
  • Unless you provide a [mcve], I don't think we can help. Sorry. – mzjn Jan 07 '22 at 16:33
  • @pascalamin If you're using Read the Docs, can you share the project URL? Most likely this is a configuration problem that should be easy to fix. – astrojuanlu Jan 11 '22 at 14:39

1 Answers1

0

I once had a similar problem and it could be solved by manually adding all directories of the module to the path. See also this answer.

Amos Egel
  • 937
  • 10
  • 24
  • The OP has added three paths to `sys.path` already (see the second comment of the question). Usually you need to add one `sys.path` entry: the path to the directory above the top-level package. – mzjn Jan 14 '22 at 18:47
  • @mzjn Thanks for pointing this out. When I had a siilar issue, it was solved after I manually added all directories containing source files to the path (adding the package's root directory wouldn't suffice). Not sure if I also tried to add the directory above the package's root, though. Anyways, maybe my workaround is worth a try for the OP ... – Amos Egel Jan 15 '22 at 20:02