MATLAB 生成的 Python 包与 Ubuntu 上的 PyQt5 冲突 - 可能的库问题

Posted

技术标签:

【中文标题】MATLAB 生成的 Python 包与 Ubuntu 上的 PyQt5 冲突 - 可能的库问题【英文标题】:MATLAB-generated Python packages conflict with PyQt5 on Ubuntu - possible library issue 【发布时间】:2019-06-25 17:09:30 【问题描述】:

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

但是,我发现当设置LD_LIBRARY_PATH 时,PyQt 无法运行。只要未导入 MATLAB 包且未设置 LD_LIBRARY_PATH,程序在安装了 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 运行时在/usr/local/MATLAB/MATLAB_Runtime/v96/bin/glnxa64/ 中提供libQt5XcbQpa.so.5,必须将其导出到LD_LIBRARY_PATH。设置LD_LIBRARY_PATH时似乎PyQt正在使用它,并且它是与当前版本的PyQt不兼容的旧版本。

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

有没有办法让/usr/lib/x86_64-linux-gnu/ 中的版本比 MATLAB 提供的库具有更高的优先级?有没有其他方法可以解决这个问题?

【问题讨论】:

【参考方案1】:

我发现了一种解决方法:

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

这是一个相对简单的例子:

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),))

【讨论】:

以上是关于MATLAB 生成的 Python 包与 Ubuntu 上的 PyQt5 冲突 - 可能的库问题的主要内容,如果未能解决你的问题,请参考以下文章

python 3 包与模块

python 包与模块

TensorRT python包与python 3.6不兼容

python 抓包与解包

Python基础之包与模块

如何用Python生成多个随机矩阵