Search code examples
pythonpython-sphinxread-the-docssys.path

Changing the sys.path for read the docs


I am documenting my pyramid project locally with sphinx. Locally autodoc works fine and the documentation looks the way I want it.

But if I am using read the docs I get the following error for each use of the autodoc feature:

/home/docs/checkouts/readthedocs.org/user_builds/buchungssystem/checkouts/latest/docs/source/02database/02models.rst:8: WARNING: autodoc: failed to import class u'Person' from module u'buchungssystem.models.person'; the following exception was raised: Traceback (most recent call last): File "/home/docs/checkouts/readthedocs.org/user_builds/buchungssystem/envs/latest/local/lib/python2.7/site-packages/sphinx/ext/autodoc.py", line 385, in import_object __import__(self.modname) ImportError: No module named buchungssystem.models.person

My project is structured like this:

project
->code
  ->models
  ->scripts
  ->...
  ->__init__.py
  ->...
->docs
  ->conf.py
  ->documentation
->setup.py

I think that my sys.path is not configured correctly. After trying out a lot I am back at sys.path.insert(0, os.path.abspath('.'))


Solution

  • in your conf.py try this

    sys.path.insert(0, os.path.join(os.path.dirname((os.path.abspath('.')), 'code', 'models')
    

    because it requires path to the Person class and that class is one step up in your hierarchy and the you can pull the path of that class.