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

时间:2018-05-06 19:00:04

标签: python pyqt read-the-docs

构建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

的试作

1。使用模拟模块

也许mocking PyQT5可行。但这似乎有点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。使用sphinx内置autodoc_mock_imports

更简单的错误(只有一行添加到conf.py

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

3。使用自定义模拟

使用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定义/检索内容从模块级别移动到函数体,而不是在模块导入时执行。

4。没有多重继承的autodoc_mock_imports

autodoc_mock_imports = ['sip', 'PyQt5', 'PyQt5.QtGui', 'PyQt5.QtCore', 'PyQt5.QtWidgets']
<{1>}中的

,与第2次尝试中一样,但是多个继承被装饰器替换。这些更改在此pull request中进行了描述。

现在错误是

conf.py

因为定义术语的地理位置类geoptics.guis.qt.handles.LineHandle.reset_move:1:term not in glossary: move restrictions 已被sphinx嘲笑,其文档丢失。

以下相关问题留下的评论:

1 个答案:

答案 0 :(得分:0)

autodoc_mock_importsfixed 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版本的要求。