使用 Sphinx 记录 Django 项目
Documenting Django project with Sphinx
我正在关注 this tutorial 以便为我的 Django 项目生成文档,但是当我键入 make html
时,我收到此错误:
Configuration error:
There is a programmable error in your configuration file:
Traceback (most recent call last):
File "/Users/hugovillalobos/.local/share/virtualenvs/sacbeh-backend-lZZWPwwy/lib/python3.8/site-packages/sphinx/config.py", line 327, in eval_config_file
execfile_(filename, namespace)
File "/Users/hugovillalobos/.local/share/virtualenvs/sacbeh-backend-lZZWPwwy/lib/python3.8/site-packages/sphinx/util/pycompat.py", line 88, in execfile_
exec(code, _globals)
File "/Users/hugovillalobos/Documents/Code/sacbeh-project/sacbeh-backend/docs/conf.py", line 18, in <module>
django.setup()
File "/Users/hugovillalobos/.local/share/virtualenvs/sacbeh-backend-lZZWPwwy/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/Users/hugovillalobos/.local/share/virtualenvs/sacbeh-backend-lZZWPwwy/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/Users/hugovillalobos/.local/share/virtualenvs/sacbeh-backend-lZZWPwwy/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/Users/hugovillalobos/.local/share/virtualenvs/sacbeh-backend-lZZWPwwy/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'sacbeh_backend.settings'
make: *** [html] Error 2
这是我的 conf.py 文件:
import os
import sys
import django
sys.path.insert(0, os.path.abspath('..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'sacbeh_backend.settings'
django.setup()
# -- Project information -----------------------------------------------------
project = 'sacbeh_backend'
copyright = '2021, Attractora'
author = 'Attractora'
# The full version, including alpha/beta/rc tags
release = '0.1'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
这是我项目的文件结构:
我相信我已经很好地按照教程说明进行操作,但我不知道是否遗漏了什么。
您已将项目根路径插入到您的 sys.path 中,这很好:
sys.path.insert(0, os.path.abspath('..'))
但是该路径上不存在该模块:
os.environ['DJANGO_SETTINGS_MODULE'] = 'sacbeh_backend.settings'
你应该改为:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sacbeh_backend.sacbeh_backend.settings')
应该没问题。在这里 https://github.com/kuter/events-web-app/tree/master/docs/ 你可以找到工作示例。
我正在关注 this tutorial 以便为我的 Django 项目生成文档,但是当我键入 make html
时,我收到此错误:
Configuration error:
There is a programmable error in your configuration file:
Traceback (most recent call last):
File "/Users/hugovillalobos/.local/share/virtualenvs/sacbeh-backend-lZZWPwwy/lib/python3.8/site-packages/sphinx/config.py", line 327, in eval_config_file
execfile_(filename, namespace)
File "/Users/hugovillalobos/.local/share/virtualenvs/sacbeh-backend-lZZWPwwy/lib/python3.8/site-packages/sphinx/util/pycompat.py", line 88, in execfile_
exec(code, _globals)
File "/Users/hugovillalobos/Documents/Code/sacbeh-project/sacbeh-backend/docs/conf.py", line 18, in <module>
django.setup()
File "/Users/hugovillalobos/.local/share/virtualenvs/sacbeh-backend-lZZWPwwy/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/Users/hugovillalobos/.local/share/virtualenvs/sacbeh-backend-lZZWPwwy/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/Users/hugovillalobos/.local/share/virtualenvs/sacbeh-backend-lZZWPwwy/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/Users/hugovillalobos/.local/share/virtualenvs/sacbeh-backend-lZZWPwwy/lib/python3.8/site-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'sacbeh_backend.settings'
make: *** [html] Error 2
这是我的 conf.py 文件:
import os
import sys
import django
sys.path.insert(0, os.path.abspath('..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'sacbeh_backend.settings'
django.setup()
# -- Project information -----------------------------------------------------
project = 'sacbeh_backend'
copyright = '2021, Attractora'
author = 'Attractora'
# The full version, including alpha/beta/rc tags
release = '0.1'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
这是我项目的文件结构:
我相信我已经很好地按照教程说明进行操作,但我不知道是否遗漏了什么。
您已将项目根路径插入到您的 sys.path 中,这很好:
sys.path.insert(0, os.path.abspath('..'))
但是该路径上不存在该模块:
os.environ['DJANGO_SETTINGS_MODULE'] = 'sacbeh_backend.settings'
你应该改为:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sacbeh_backend.sacbeh_backend.settings')
应该没问题。在这里 https://github.com/kuter/events-web-app/tree/master/docs/ 你可以找到工作示例。