PyQT5 doesn't work on docker ImportError: libsmime3.so: cannot open shared object file: No such file or directory

PyQT5 doesn't work on docker ImportError: libsmime3.so: cannot open shared object file: No such file or directory

我有一个安装了 PyQT 的 Dockerfile,如下所示

FROM ubuntu:20.04
ENV DEBIAN_FRONTEND=noninteractive
RUN adduser --quiet --disabled-password qtuser && usermod -a -G audio qtuser
RUN apt-get update -y \
    && apt-get install alsa -y \
    && apt-get install -y python3-pyqt5 \
    && apt-get install python3-pip -y && \
    pip3 install pyqtwebengine
WORKDIR /htmltopdf

我是这样构建图像的docker build -t html-to-pdf .

然后我运行我的头像是这样的docker run --rm -v "$(pwd)":/htmltopdf -u qtuser -it html-to-pdf python3 htmlToPdfnew.py --url https://www.w3schools.com/howto/howto_css_register_form.asp

但是我遇到了以下错误

Traceback (most recent call last):
  File "htmlToPdfnew.py", line 2, in <module>
    from PyQt5 import QtWidgets, QtWebEngineWidgets
ImportError: libsmime3.so: cannot open shared object file: No such file or directory

我的 PC 中没有出现该错误。

下面是我的python代码

import sys
from PyQt5 import QtWidgets, QtWebEngineWidgets
from PyQt5.QtCore import QUrl, QTimer
from PyQt5.QtGui import QPageLayout, QPageSize
from PyQt5.QtWidgets import QApplication
import argparse


def main():
    url = ''
    parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--url", help="Type url")
    args = parser.parse_args()
    config = vars(args)
    url = config['url']


    app = QtWidgets.QApplication(sys.argv)
    loader = QtWebEngineWidgets.QWebEngineView()
    loader.setZoomFactor(1)
    layout = QPageLayout()
    layout.setPageSize(QPageSize(QPageSize.A4Extra))
    layout.setOrientation(QPageLayout.Portrait)
    loader.load(QUrl(url))
    loader.page().pdfPrintingFinished.connect(lambda *args: QApplication.exit())

    def emit_pdf(finished):
        QTimer.singleShot(2000, lambda: loader.page().printToPdf("test.pdf", pageLayout=layout))

    loader.loadFinished.connect(emit_pdf)
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

那么我该如何解决以下错误?

Traceback (most recent call last):
  File "htmlToPdfnew.py", line 2, in <module>
    from PyQt5 import QtWidgets, QtWebEngineWidgets
ImportError: libsmime3.so: cannot open shared object file: No such file or directory

您必须在 Docker 映像中安装 libnss3。

apt-get install libnss3 添加到 Dockerfile 中的安装命令应该可以解决问题。