如何在readthedocs上正确设置PyQt5导入?



构建导入 PyQt5 的项目的狮身人面像文档失败(构建日志)

QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-docs'
qt.qpa.screen: QXcbConnection: Could not connect to display 
Could not connect to any X display.

tox.ini需要以下方面:

[testenv:docs]
# avoid QStandardPaths: XDG_RUNTIME_DIR not set
passenv = XDG_RUNTIME_DIR
# xvfb-run prevents Could not connect to any X display
commands = /usr/bin/xvfb-run sphinx-build --color -W -b html -d {envtmpdir}/doctrees . {envtmpdir}/html

如何在阅读文档上做到这一点?

这与阅读文档中的 PyQt 4 导入密切相关, 不幸的是,它不包含错误消息。PyQt5可以从pip安装。

笔记:

  • 在高级设置中,选中Install your project inside a virtualenv using setup.py install(但取消选中没有帮助)。
  • 以下暂定的参考地质快照f33d233bf67bd7922ec864635e7589e7f4feb40f

暂定

1. 带模拟模块

也许嘲笑 PyQT5 可以奏效。但这似乎有点麻烦。

根据这个答案改编,添加

import mock 
MOCK_MODULES = ['sip', 'PyQt5', 'PyQt5.QtGui', 'PyQt5.QtCore', 'PyQt5.QtWidgets']
sys.modules.update((mod_name, mock.MagicMock()) for mod_name in MOCK_MODULES)

conf.py产量

class _GRay(GCounterPart, QGraphicsPathItem):
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

2.内置狮身人面像autodoc_mock_imports

与更简单的错误相同(只需向conf.py添加一行)

autodoc_mock_imports = ['sip', 'PyQt5', 'PyQt5.QtGui', 'PyQt5.QtCore', 'PyQt5.QtWidgets']

3.使用自定义模拟

使用 julen 的自定义模拟类

class Mock(object):
def __init__(self, *args, **kwargs):
pass
def __call__(self, *args, **kwargs):
return Mock()
@classmethod
def __getattr__(cls, name):
if name in ('__file__', '__path__'):
return '/dev/null'
elif name[0] == name[0].upper():
mockType = type(name, (), {})
mockType.__module__ = __name__
return mockType
else:
return Mock()
MOCK_MODULES = ['sip', 'PyQt5', 'PyQt5.QtGui', 'PyQt5.QtCore', 'PyQt5.QtWidgets']
for mod_name in MOCK_MODULES:
sys.modules[mod_name] = Mock()

收益 率

File ".../geoptics/guis/qt/main.py", line 59, in <module>
app = QCoreApplication.instance()
AttributeError: type object 'QCoreApplication' has no attribute 'instance'

应该可以将app定义/检索内容从模块级别移动到函数体,而不是在模块导入时执行。

4. 无多重继承autodoc_mock_imports

autodoc_mock_imports = ['sip', 'PyQt5', 'PyQt5.QtGui', 'PyQt5.QtCore', 'PyQt5.QtWidgets']

conf.py中,与第二次暂定一样,但多重继承被装饰器取代。此拉取请求中描述了这些更改。

现在错误是

geoptics.guis.qt.handles.LineHandle.reset_move:1:term not in glossary: move restrictions

因为定义该术语的Geoptics类_GScene(QGraphicsScene)已被狮身人面像嘲笑,并且其文档丢失。


在以下相关问题中留下的意见:

  • 阅读文档
  • 狮身人面像

autodoc_mock_imports已在狮身人面像-1.7.5中修复。

docs/conf.py中添加以下行:

autodoc_mock_imports = ['sip', 'PyQt5', 'PyQt5.QtGui', 'PyQt5.QtCore', 'PyQt5.QtWidgets']

然后,创建包含一行的文档/要求.txt

sphinx>=1.7.5

并在 ReadtheDocs 项目中声明docs/requirements.txtadmin>advanced settings>Requirements file.

令人高兴的是,这并没有绕过 setup.py,它只是增加了狮身人面像-1.7.5版本要求。

最新更新