![](/img/trans.png)
[英]DJANGO_SETTINGS_MODULE not defined when building Sphinx documentation
[英]Failed to import module when building the sphinx documentation
我正在使用Sphinx
版本1.4.5
。
我的項目結構如下:
+ src > main.py + docs (generated with sphinx-quickstart)
即使在docs/conf.py
添加了src
文件夾的路徑:
sys.path.insert(0, os.path.abspath('../src'))
並為src/main.py
生成 rst 文件(即docs/src.rst
和docs/modules.rst
):
$ sphinx-apidoc -fo docs src
當我嘗試使用以下內容構建html
網頁時:
$ make clean
$ make html
它找不到src
模塊和src/main.py
:
WARNING: autodoc: failed to import module u'src.main'; the following exception was raised
嘗試為您的路徑插入執行此操作:
sys.path.insert(0, os.path.abspath('../'))
還要為您的目錄考慮一個比src
更好的名稱。
我喜歡在conf.py
使用以下代碼來確切知道當前目錄是什么以及目標模塊在哪里(以獲取文檔):
current_dir = os.path.dirname(__file__)
target_dir = os.path.abspath(os.path.join(current_dir, "../../src"))
sys.path.insert(0, target_dir)
print(target_dir)
在這種情況下,我希望為我的 src 創建文檔,請參閱樹以了解上下文:
main
├── docs
│ ├── build
│ ├── make.bat
│ ├── Makefile
│ └── source
│ ├── conf.py
│ └── index.rst
│
└── src
├── __init__.py
├── target_module
├── requirements.txt
└── setup.py
接下來,從您的終端:
[user@localhost docs]$ sphinx-apidoc -f -o source/ ../src/target_module
[user@localhost docs]$ make html
您當前的工作目錄應該是您的 makefile 的目錄,應該是docs
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.