meta
meta

Reputation: 11

Running PyQt4 script more than once inside REAPER results in SIP error

Some Context: REAPER is a Digital Audio Workstation that has an extensive scripting API with Lua, Python, C/C++ and EEL bindings. I haven't found much information regarding how it handles Python internally. On the surface the configuration involves pointing to a Python DLL somewhere on the system. Screenshot

The Problem: I tried running a modified version of the PyQt4 script found here inside REAPER. At first it worked perfectly

On subsequent runs of the same script however an error popped up:

 Traceback (most recent call last):
  File "pyqt_test.py", line 2, in <module>
    from PyQt4 import QtGui
RuntimeError: the sip module has already registered a module called PyQt4.QtCore

Restarting REAPER solves the issue for another single run of the script before showing the same error again.

Googling the issue hasn't been much help. There's one message on the PyQt mailing list that mentions the error and that the solution involved a package's __init__ file but no other elaboration.

Here's the modified script I'm running:

import sys
from PyQt4 import QtGui

def main():
    sys.argv = [] # necessary because for some reason argv doesnt exist when the script is run inside REAPER

    app = QtGui.QApplication(sys.argv)
    widget = QtGui.QWidget()
    layout = QtGui.QGridLayout()

    buttons = {}

    for i in range(16):
        for j in range(16):
            # keep a reference to the buttons
            buttons[i, j] = QtGui.QPushButton('row %d, col %d' % (i, j))
            # add to the layout
            layout.addWidget(buttons[i, j], i, j)

    widget.setLayout(layout)
    widget.show()
    app.exit(app.exec_())

if __name__ == '__main__':
    RPR_defer('main()') # RPR_defer runs supplied code in REAPER's GUI thread.

Any ideas as to what's going on and how to workaround/solve it?

Upvotes: 1

Views: 303

Answers (1)

Rom&#233;o Despr&#233;s
Rom&#233;o Despr&#233;s

Reputation: 2183

There is a known bug with importing external libraries more than once with the Python ReaScript API. It also happens with numpy (and thus all libraries that depend on it).

I've been working on a way to solve it by writing the reapy library. It allows to call functions of the ReaScript API from outside REAPER (e.g. in a usual terminal), and thus to import safely any third-party libraries (including PyQt4). You can check the documentation or the repository if you're interested!

Upvotes: 1

Related Questions