python - Sphinx 在包含导入的行上引发错误
问题描述
我遇到了 Sphinx 的问题,它似乎在 python 文件中导入失败。
我尝试在其中添加一个autodoc_mock_imports
以conf.py
避免此错误但没有成功:
autodoc_mock_imports = ['test.lib.labjackue9']
有关信息,当我在 中注释该行时import test.lib.labjackue9 as labjack
,test.src.led.py
生成的文档没有错误!
你有什么想法?
这是错误消息:
<WORKING FOLDER>/test/doc/source/test.src.rst:34: WARNING: autodoc: failed to import module 'test.src.led'; the following exception was raised:
Traceback (most recent call last):
File "<WORKING FOLDER>/test/lib/labjackue9.py", line 9, in <module>
labjack_id = ue9.UE9()
File "<WORKING FOLDER>/test/lib/labjack/ue9.py", line 92, in __init__
self.open(**kargs)
File "<WORKING FOLDER>/test/lib/labjack/ue9.py", line 111, in open
Device.open(self, 9, Ethernet = ethernet, firstFound = firstFound, serial = serial, localId = localId, devNumber = devNumber, ipAddress = ipAddress, handleOnly = handleOnly, LJSocket = LJSocket)
File "<WORKING FOLDER>/test/lib/labjack/LabJackPython.py", line 616, in open
d = openLabJack(devType, ct, firstFound = True, handleOnly = handleOnly, LJSocket = LJSocket)
File "<WORKING FOLDER>/test/lib/labjack/LabJackPython.py", line 1388, in openLabJack
handle = _openLabJackUsingExodriver(deviceType, firstFound, pAddress, devNumber)
File "<WORKING FOLDER>/test/lib/labjack/LabJackPython.py", line 1276, in _openLabJackUsingExodriver
raise NullHandleException(info)
LabJackPython.NullHandleException: Couldn't open device. Please check that the device you are trying to open is connected.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/sphinx/ext/autodoc.py", line 658, in import_object
__import__(self.modname)
File "<WORKING FOLDER>/test/src/led.py", line 7, in <module>
import test.lib.labjackue9 as labjack
File "<WORKING FOLDER>/test/lib/labjackue9.py", line 11, in <module>
raise ConnectionError("LabJack UE9 did not answered")
ConnectionError: LabJack UE9 did not answered
这是我的项目文件夹:
+- test
|
+---- src
+------- __init__.py
+------- led.py
+------- battery.py
+------- const.py
+------- ...
|
+---- lib
+------- __init__.py
+------- labjack
+------------ __init__.py
+------------ LabJackPython.py
+------------ ue9.py
+------- generic.py
+------- labjackue9.py
+------- labjackue9_config.py
+------- ...
|
+---- doc
+------- Makefile
+------- build (folder with HTML files)
+------- source
+---------- conf.py
+---------- index.rst
+---------- ...
编辑:
这里是开头test.src.led.py
:
import os
import time
from threading import Thread
import test.lib.generic as generic
import test.lib.labjackue9 as labjack
from test.src.const import *
from test.lib.labjackue9_config import *
class led(Thread):
def __init__(self):
...
这是test.src.rst
test.src package
================
Submodules
----------
test.src.led module
-------------------
.. automodule:: test.src.led
:members:
:undoc-members:
:show-inheritance:
test.src.battery module
-----------------------
.. automodule:: test.src.battery
:members:
:undoc-members:
:show-inheritance:
test.src.const module
---------------------
.. automodule:: test.src.const
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: test.src
:members:
:undoc-members:
:show-inheritance:
编辑2:
这里是test.lib.labjackue9.py
:
import sys
sys.path.append("<WORKING FOLDER>/test/lib/labjack")
import ue9
try:
labjack_id = ue9.UE9()
except Exception:
raise ConnectionError("LabJack UE9 not reachable")
def labjackid():
return labjack_id
解决方案
将 LabJack 模块连接到 PC 后,问题消失,文档生成成功。我真的不明白为什么连接 LabJack 模块会改变 Sphinx 读取源代码的方式,但它似乎工作得很好。
推荐阅读
- python - 有没有办法查看 2 个数据列是否代表相同类型的信息?
- asp.net-mvc - ASP.NET MVC 5 Actionlink 忽略 actionname
- javascript - 无论如何在提交之前和之后检测表单值的变化?
- c++ - 为什么我们找不到动态分配的数组大小?
- azure-devops - Devops 管道将输出复制到 Repo
- apache - 在服务器上部署时删除 url 上的 hashbang (#)
- firebase - 隐藏查询以显示在列表磁贴小部件上
- spring-boot - HTTP 标头不允许使用字符?
- firebase - 是否可以使用 Cloudflare 工作者,例如具有 Flutter 和 Firebase 数据库的 Firebase 云功能
- javascript - 搜索功能的更多参数