Sphinx Docs not importing Django project settings

ぃ、小莉子 提交于 2020-01-15 05:26:30

问题


I just recently move a Django project into a new virtualenv. The project works fine, but I am having trouble building my Sphinx Documentation.

in my conf.py I have this:

import sys, os

sys.path.append('/path/to/myproject')

from django.core.management import setup_environ
from myproject import settings

setup_environ(settings)

But when I use make html I get this error:

from myproject import settings
  ImportError: No module named myproject

Any help much appreciated.


回答1:


Turns out the conf.py needs to look like this:

import sys, os

sys.path.append('/path/to')

from myproject import settings
from django.core.management import setup_environ
setup_environ(settings)

Hope this might help someone.




回答2:


Django 1.4 deprecated setup_environ. Here's similar code for Django 1.4 and later:

import sys, os
cwd = os.getcwd()
parent = os.path.dirname(cwd)
sys.path.append(parent)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")



回答3:


Having this problem with a Django 1.7.5 project and I think it's down to some strange project layout decisions we made, but I needed one extra step to solve this using jwhitlock's answer:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.conf import settings

When I did that useless import it found my custom Django settings which were specified in DJANGO_SETTINGS_MODULE but were not found by the autodoc process. I think this is because the project lives in a folder whose parent has the same name, but inspecting sys.path only shows the "right" folder so the import should work but blows up saying it can't find my settings.



来源:https://stackoverflow.com/questions/11583075/sphinx-docs-not-importing-django-project-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!