问题
I am currently in the process of getting the documentation of my code online via read-the-docs, however, getting read-the-docs to process my PyQt4 dependent modules seems problematic.
My project has the following structure:
pkg
pkg/__init__.py
pkg/modules/
pkg/modules/__init__.py
pkg/modules/somemodules.py
pkg/gui/__init__.py
pkg/gui/someGUImodules.py
I am using sphinx-autodoc to build a html representation of the docstring of the different modules. On my local machine everything works fine, however, since I need to mock
PyQt4 on read-the-docs, I ran into the following problem: In one of my GUI classes, I subclass QtGui.QDialog
via
class listSelectorDialog(QtGui.QDialog):
def __init__(self,parent,List):
super(listSelectorDialog,self).__init__(parent)
and listSelectorDialog
via
class advancedListSelectorDialog(listSelectorDialog):
def __init__(self,parent,List):
super(advancedListSelectorDialog,self).__init__(parent,List)
Mocking QtGui
will result in read-the-docs telling me:
class advancedListSelectorDialog(listSelectorDialog):
TypeError: Error when calling the metaclass bases
str() takes at most 1 argument (3 given)
and hence crashing. I've tried to build my package into a virtual environment by selecting
Install your project inside a virtualenv using setup.py install
however, it turns out even though PyQt4 is listed in pip
, you can not install it, see https://superuser.com/questions/679298/how-to-install-pyqt4-and-what-are-the-practical-differences-between-pyqt4-and-py.
The only workaround I've found so far is to not load the GUI modules if the environment is RTD and leave out the documentation of the GUI modules, however this should not be the final solution. Thanks.
回答1:
I had a similar problem with PyQt5/py3 (a metaclass conflict with MagickMock). My workaround is to manually mock the module in conf.py instead of using unittest.mock:
class PyQt5:
@staticmethod
def qVersion():
return '5.0.0'
class QtCore:
class QObject:
pass
# etc...
sys.modules['PyQt5'] = PyQt5
This makes the import / metaclass conflict problem disappear. Unfortunately autodoc still doesn't work (no output), though the builds pass...
Tedious of course.
来源:https://stackoverflow.com/questions/36283829/pyqt-4-import-in-read-the-docs