簡體   English   中英

構建 sphinx 文檔時無法導入模塊

[英]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.rstdocs/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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM