构建project导入PyQt5的sphinx文档失败(build log

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

如何在readthedocs上做到这一点?
这与PyQt 4 import in read-the-docs密切相关,
不幸的是,它没有包含错误消息。PyQt5可以从pip安装。
笔记:
在高级设置中,Install your project inside a virtualenv using setup.py install被选中(但取消选中没有帮助)。
以下选项的参考geoptics快照是f33d233bf67bd7922ec864635e7589e7f4feb40f
提示语
一。带模拟模块
也许mockingPyQT5可以工作。但这似乎有点cumbersome
改编自this answer,添加
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
与simpler相同的错误(只有一行添加到conf.py
autodoc_mock_imports = ['sip', 'PyQt5', 'PyQt5.QtGui', 'PyQt5.QtCore', 'PyQt5.QtWidgets']

三。带定制模拟
使用julen's custom Mock class
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定义/检索内容从模块级移动到函数体,而不是在模块导入时执行。
四。autodoc_mock_imports没有多重继承
autodoc_mock_imports = ['sip', 'PyQt5', 'PyQt5.QtGui', 'PyQt5.QtCore', 'PyQt5.QtWidgets']

conf.py中,如第2个暂定的,但是多重继承被decorators替换。这些变化在pull request中有描述。
现在错误是
geoptics.guis.qt.handles.LineHandle.reset_move:1:term not in glossary: move restrictions

因为定义术语的geoptics类已经被sphinx模拟掉了,它的文档也丢失了。
有关问题的评论意见:
readthedocs
sphinx

最佳答案

autodoc_mock_imports已经fixed in sphinx-1.7.5
docs/conf.py中添加以下行:

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

然后,用一行代码创建docs/requirements.txt
sphinx>=1.7.5

并在readthedocs项目中声明docs/requirements.txt
admin>advanced settings>Requirements file
幸运的是,这并没有绕过setup.py,它只是添加了sphinx-1.7.5版本要求。

关于python - 如何在readthedocs上正确设置PyQt5导入?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50203452/

10-10 15:02