首页 > 解决方案 > MATLAB 生成的 Python 包与 Ubuntu 上的 PyQt5 冲突 - 可能的库问题

问题描述

我正在使用 Ubuntu 18.04 和 PyQt 5.12.1 构建一个应用程序,它导入从 MATLAB 代码生成的Python 包(这些包依赖于 MATLAB 运行时)。Python 中的 MATLAB 包需要设置LD_LIBRARY_PATH环境变量;如果没有这个,程序会在导入 MATLAB 生成的包时引发异常。

但是,我发现 PyQt 在LD_LIBRARY_PATH设置时无法运行。只要未导入 MATLAB 包LD_LIBRARY_PATH且未设置 MATLAB,该程序在安装了 MATLAB Runtime 的情况下运行良好。

根据 MA​​TLAB 运行时安装程序的提示,我将其添加到 PyCharm 运行/调试配置中的环境变量中:

LD_LIBRARY_PATH=/usr/local/MATLAB/MATLAB_Runtime/v96/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v96/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v96/extern/bin/glnxa64.

这会导致程序的 PyQt 部分崩溃。使用QT_DEBUG_PLUGINS=1环境变量,报错信息如下:

Got keys from plugin meta data ("xcb")
QFactoryLoader::QFactoryLoader() checking directory path "<redacted>/PyMODA/venv/bin/platforms" ...
Cannot load library <redacted>/venv/lib/python3.6/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so: (/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64/libQt5XcbQpa.so.5: undefined symbol: _ZNK14QPlatformTheme14fileIconPixmapERK9QFileInfoRK6QSizeF6QFlagsINS_10IconOptionEE)
QLibraryPrivate::loadPlugin failed on "<redacted>/venv/lib/python3.6/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so" : "Cannot load library <redacted>/venv/lib/python3.6/site-packages/PyQt5/Qt/plugins/platforms/libqxcb.so: (/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64/libQt5XcbQpa.so.5: undefined symbol: _ZNK14QPlatformTheme14fileIconPixmapERK9QFileInfoRK6QSizeF6QFlagsINS_10IconOptionEE)"
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.

重要的部分:

"Cannot load library <...>/libqxcb.so: (/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64/libQt5XcbQpa.so.5: undefined symbol: _ZNK14QPlatformTheme14fileIconPixmapERK9QFileInfoRK6QSizeF6QFlagsINS_10IconOptionEE)"

MATLAB Runtime包含libQt5XcbQpa.so.5在 中/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64/,必须导出到LD_LIBRARY_PATH. 似乎这是 PyQt 在LD_LIBRARY_PATH设置时使用的,它是一个旧版本,与当前版本的 PyQt 不兼容。

另一个同名的库在 中/usr/lib/x86_64-linux-gnu/,它的 MD5 校验和与 MATLAB 版本不同。但是,将此目录添加到开头LD_LIBRARY_PATH没有帮助。设置QT_QPA_PLATFORM_PLUGIN_PATH也无济于事。

有没有办法使版本/usr/lib/x86_64-linux-gnu/的优先级高于 MATLAB 提供的库?还有其他方法可以解决此问题吗?

标签: pythonlinuxmatlabubuntupyqt5

解决方案


我发现了一种解决方法:

  • 在新进程中运行所有 MATLAB 打包的代码;这几乎不会带来不便,因为计算必须在单独的线程或进程上运行,以防止冻结 GUI。
  • 在运行 MATLAB 打包代码的每个进程中,LD_LIBRARY_PATH在导入 MATLAB 模块之前以编程方式设置环境变量。导入语句必须在函数中,而不是在文件顶部。

这是一个相对最小的示例:

class MyPlot(PlotComponent):
    """
    A class which inherits from a base class PlotComponent, which is 
    a subclass of QWidget. In this simple example, the window 
    gets the data and calls the function `plot(self, data)` on an 
    instance of this class. 
    """
    def __init__(self, parent):
        super().__init__(parent)
        self.queue = Queue()

    def plot(self, data):
        """Calculate the results from the provided data, and plot them."""
        fs = data.frequency

        self.times = data.times
        signal = data.signal.tolist()

        # Create the process, supplying all data in non-MATLAB types.
        self.proc = Process(target=generate_solutions, args=(self.queue, signal, fs))
        self.proc.start()

        # Check for a result in 1 second.
        QTimer.singleShot(1000, self.check_result)

    def check_result(self):
        """Checks for a result from the other process."""
        if self.queue.empty(): # No data yet; check again in 1 second.
            QTimer.singleShot(1000, self.check_result)
            return

        w, l = self.queue.get() # Get the data from the process.

        a = np.asarray(w)
        gh = np.asarray(l)

        # Create the plot.
        self.axes.pcolormesh(self.times, gh, np.abs(a))

def generate_solutions(queue, signal, freq):
    """
    Generates the solutions from the provided data, using the MATLAB-packaged
    code. Must be run in a new process.
    """
    import os

    # Set the LD_LIBRARY_PATH for this process. The particular value may
    # differ, depending on your installation.
    os.environ["LD_LIBRARY_PATH"] = "/usr/local/MATLAB/MATLAB_Runtime/v96/runtime/glnxa64:" \
    "/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v96/sys/os/glnxa64:" \ 
    "/usr/local/MATLAB/MATLAB_Runtime/v96/extern/bin/glnxa64"

    # Import these modules AFTER setting up the environment variables.
    import my_matlab_package
    import matlab

    package = my_matlab_package.initialize()

    # Convert the input into MATLAB data-types, to pass to the MATLAB package.
    A = matlab.double([signal])
    fs_matlab = matlab.double([freq])

    # Calculate the result.
    w, l = package.perform_my_calculation(A, fs_matlab, nargout=2)

    # Convert the results back to normal Python data-types so that the
    # main process can use them without importing matlab, and put them 
    # in the queue.
    queue.put((np.asarray(w), np.asarray(l),))

推荐阅读