Docker、PyQt5、ModuleNotFoundError:没有名为“PyQt5”的模块

问题描述 投票:0回答:1

我想dockerize这个应用程序:

# app.py

import sys

# 1. Import QApplication and all the required widgets
from PyQt5.QtWidgets import QApplication, QLabel, QWidget

# 2. Create an instance of QApplication
app = QApplication([])

# 3. Create your application's GUI
window = QWidget()
window.setWindowTitle("PyQt App")
window.setGeometry(100, 100, 280, 80)
helloMsg = QLabel("<h1>Hello, World!</h1>", parent=window)
helloMsg.move(60, 15)

# 4. Show your application's GUI
window.show()

# 5. Run your application's event loop
sys.exit(app.exec())

dockerfile 是:

# Use an official Python runtime as a base image
FROM python:3.8

# Set the working directory in the container
WORKDIR /app

# Update package repository and install dependencies
RUN apt-get update && \
    apt-get install -y python3-pyqt5 && \
    rm -rf /var/lib/apt/lists/*

# Copy the current directory contents into the container at /app
COPY . /app

# Run the PyQt5 app when the container launches
CMD ["python", "app.py"]

使用

docker build -t myapp .
构建后并使用
docker run --rm -it myapp
开始:

docker run --rm -it myapp
Traceback (most recent call last):
  File "app.py", line 6, in <module>
    from PyQt5.QtWidgets import QApplication, QLabel, QWidget
ModuleNotFoundError: No module named 'PyQt5'

编辑: 我使用

apt-get
而不是
pip
,因为我收到
pip
的错误,建议的解决方案是使用
apt-get
: importerror: libgl.so.1: 无法打开共享对象文件:没有这样的文件或目录(PyQt5)

使用

RUN pip install --no-cache-dir PyQt5
会导致此错误:

docker run --rm -it myapp
Traceback (most recent call last):
  File "app.py", line 6, in <module>
    from PyQt5.QtWidgets import QApplication, QLabel, QWidget
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
python linux docker pyqt pyqt5
1个回答
0
投票

创建一个

requirements.txt
文件,其中包含所需包的列表。

PyQt5==5.15.10

更新

Dockerfile

FROM python:3.8

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

RUN apt-get update && \
    apt-get install -y libgl1 && \
    rm -rf /var/lib/apt/lists/*

COPY . /app

CMD ["python", "app.py"]

Dockerfile
使用

  • pip
    安装 Python 包并
  • apt-get
    安装 Python 包所需的共享库 (
    libGL.so.1
    )。
© www.soinside.com 2019 - 2024. All rights reserved.