1

После конвертации .py файла в .exe, который использует изображения, при запуске выдаёт ошибку:

ModuleNotFoundError: No module named "images_rc"

(файл с изображениями) , хотя файл images_rc лежит в этой же директории. Как решить эту проблему?

Upd:

IdiomsTheory.py

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_IdiomsTheory(object):
    def setupUi(self, IdiomsTheory):
        IdiomsTheory.setObjectName("IdiomsTheory")
        IdiomsTheory.resize(1100, 649)
        IdiomsTheory.setStyleSheet("background-color: #d580ff")
        self.centralwidget = QtWidgets.QWidget(IdiomsTheory)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(9, 0, 1081, 58))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        self.label.setFont(font)
        self.label.setStyleSheet("color: #ffffb3")
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        self.label_2.setFont(font)
        self.label_2.setStyleSheet("color: #aa00ff")
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.verticalLayout.addWidget(self.label_2)
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(30, 90, 621, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(13)
        self.label_3.setFont(font)
        self.label_3.setAlignment(QtCore.Qt.AlignCenter)
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(30, 140, 301, 81))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(11)
        self.label_4.setFont(font)
        self.label_4.setWordWrap(True)
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(30, 230, 301, 81))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(11)
        self.label_5.setFont(font)
        self.label_5.setWordWrap(True)
        self.label_5.setObjectName("label_5")
        self.label_6 = QtWidgets.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(30, 320, 301, 81))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(11)
        self.label_6.setFont(font)
        self.label_6.setWordWrap(True)
        self.label_6.setObjectName("label_6")
        self.label_7 = QtWidgets.QLabel(self.centralwidget)
        self.label_7.setGeometry(QtCore.QRect(30, 410, 301, 81))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(11)
        self.label_7.setFont(font)
        self.label_7.setWordWrap(True)
        self.label_7.setObjectName("label_7")
        self.label_8 = QtWidgets.QLabel(self.centralwidget)
        self.label_8.setGeometry(QtCore.QRect(350, 140, 301, 101))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(11)
        self.label_8.setFont(font)
        self.label_8.setWordWrap(True)
        self.label_8.setObjectName("label_8")
        self.label_9 = QtWidgets.QLabel(self.centralwidget)
        self.label_9.setGeometry(QtCore.QRect(350, 260, 301, 81))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(11)
        self.label_9.setFont(font)
        self.label_9.setWordWrap(True)
        self.label_9.setObjectName("label_9")
        self.label_10 = QtWidgets.QLabel(self.centralwidget)
        self.label_10.setGeometry(QtCore.QRect(350, 360, 301, 111))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(11)
        self.label_10.setFont(font)
        self.label_10.setWordWrap(True)
        self.label_10.setObjectName("label_10")
        self.label_12 = QtWidgets.QLabel(self.centralwidget)
        self.label_12.setGeometry(QtCore.QRect(670, 140, 411, 341))
        self.label_12.setStyleSheet("")
        self.label_12.setText("")
        self.label_12.setTextFormat(QtCore.Qt.AutoText)
        self.label_12.setPixmap(QtGui.QPixmap(":/page1/Images/Unit 4/4a/IdiomsImages/4a1.png"))
        self.label_12.setScaledContents(True)
        self.label_12.setObjectName("label_12")
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(10, 580, 356, 23))
        self.pushButton_4.setStyleSheet("QPushButton {background-color: #f2f2f2;\n"
"    border: 1px solid black;\n"
"    border-radius: 10\n"
" }\n"
"\n"
"QPushButton:pressed  { \n"
"    background-color: #cccccc;\n"
" }\n"
"")
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_5.setGeometry(QtCore.QRect(370, 580, 355, 23))
        self.pushButton_5.setStyleSheet("QPushButton {background-color: #f2f2f2;\n"
"    border: 1px solid black;\n"
"    border-radius: 10\n"
" }\n"
"\n"
"QPushButton:pressed  { \n"
"    background-color: #cccccc;\n"
" }\n"
"")
        self.pushButton_5.setObjectName("pushButton_5")
        self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_6.setGeometry(QtCore.QRect(730, 580, 356, 23))
        self.pushButton_6.setStyleSheet("QPushButton {background-color: #f2f2f2;\n"
"    border: 1px solid black;\n"
"    border-radius: 10\n"
" }\n"
"\n"
"QPushButton:pressed  { \n"
"    background-color: #cccccc;\n"
" }\n"
"")
        self.pushButton_6.setObjectName("pushButton_6")
        IdiomsTheory.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(IdiomsTheory)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1100, 21))
        self.menubar.setObjectName("menubar")
        IdiomsTheory.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(IdiomsTheory)
        self.statusbar.setObjectName("statusbar")
        IdiomsTheory.setStatusBar(self.statusbar)

        self.retranslateUi(IdiomsTheory)
        QtCore.QMetaObject.connectSlotsByName(IdiomsTheory)

    def retranslateUi(self, IdiomsTheory):
        _translate = QtCore.QCoreApplication.translate
        IdiomsTheory.setWindowTitle(_translate("IdiomsTheory", "Idioms Theory"))
        self.label.setText(_translate("IdiomsTheory", "<html><head/><body><p><span style=\" font-size:16pt; font-weight:600;\">Unit 4a</span></p></body></html>"))
        self.label_2.setText(_translate("IdiomsTheory", "<html><head/><body><p><span style=\" font-size:16pt; font-weight:600;\">Come rain or shine!</span></p></body></html>"))
        self.label_3.setText(_translate("IdiomsTheory", "Idioms"))
        self.label_4.setText(_translate("IdiomsTheory", "<html><head/><body><p><span style=\" text-decoration: underline; color:#55007f;\">Under the weather</span><span style=\" text-decoration: underline;\"> - slightly unwell</span></p><p><span style=\" font-style:italic;\">Example: Yumi is feeling under the weather, so she won\'t be going to work today.</span></p></body></html>"))
        self.label_5.setText(_translate("IdiomsTheory", "<html><head/><body><p><span style=\" text-decoration: underline; color:#55007f;\">On cloud nine</span><span style=\" text-decoration: underline;\"> - extremely happy</span></p><p><span style=\" font-style:italic;\">Example: Mo is on cloud nine - his wife is pregnant</span></p></body></html>"))
        self.label_6.setText(_translate("IdiomsTheory", "<html><head/><body><p><span style=\" text-decoration: underline; color:#55007f;\">A breeze</span><span style=\" text-decoration: underline;\"> - very easy</span></p><p><span style=\" font-style:italic;\">Example: Studying English is a breeze. I\'m progressin so quickly</span></p></body></html>"))
        self.label_7.setText(_translate("IdiomsTheory", "<html><head/><body><p><span style=\" text-decoration: underline; color:#55007f;\">Rain cats and dogs</span><span style=\" text-decoration: underline;\"> - rain very hard </span></p><p><span style=\" font-style:italic;\">Example: I was going to go shopping, but it suddenly started raining cats and dogs, so I stayed home</span></p></body></html>"))
        self.label_8.setText(_translate("IdiomsTheory", "<html><head/><body><p><span style=\" text-decoration: underline; color:#55007f;\">Save up for a rainy day</span><span style=\" text-decoration: underline;\"> - put some money aside for whenever it may be needed</span></p><p><span style=\" font-style:italic;\">Example: Once you have the basic home comforts, it\'s importnat to save up for a rainy day</span></p></body></html>"))
        self.label_9.setText(_translate("IdiomsTheory", "<html><head/><body><p><span style=\" text-decoration: underline; color:#55007f;\">Storm in a teacup</span><span style=\" text-decoration: underline;\"> - small problem that has been blown out of proportion</span></p><p><span style=\" font-style:italic;\">Example: In my opininon, the whole thing is just a storm in a teacup </span></p></body></html>"))
        self.label_10.setText(_translate("IdiomsTheory", "<html><head/><body><p><span style=\" text-decoration: underline; color:#55007f;\">Head in the clouds</span><span style=\" text-decoration: underline;\"> - not being realistic or daydreaming</span></p><p><span style=\" font-style:italic;\">Example: He can\'t continue going around with his head in the clouds; he must be more realistic about his problems</span></p></body></html>"))
        self.pushButton_4.setText(_translate("IdiomsTheory", "Go to previous page"))
        self.pushButton_5.setText(_translate("IdiomsTheory", "Go to unit contents"))
        self.pushButton_6.setText(_translate("IdiomsTheory", "Go to next page"))
import images_rc


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    IdiomsTheory = QtWidgets.QMainWindow()
    ui = Ui_IdiomsTheory()
    ui.setupUi(IdiomsTheory)
    IdiomsTheory.show()
    sys.exit(app.exec_())

Строка конвертации:

pyinstaller -F -w IdiomsTheory.py

images.qrc

<RCC>
  <qresource prefix="page1">
    <file>Images/Unit 4/4a/IdiomsImages/1_1.png</file>
    <file>Images/Unit 4/4a/IdiomsImages/1_2.png</file>
    <file>Images/Unit 4/4a/IdiomsImages/1_3.png</file>
    <file>Images/Unit 4/4a/IdiomsImages/4a1.png</file>
    <file>Images/Unit 4/4a/IdiomsImages/4a2.png</file>
    <file>Images/Unit 4/4a/IdiomsImages/weather1_0.png</file>
  </qresource>
  <qresource prefix="page2">
    <file>Images/Unit 4/4a/VocabularyImages/1.png</file>
    <file>Images/Unit 4/4a/VocabularyImages/2.png</file>
  </qresource>
  <qresource prefix="page3"/>
</RCC>
6
  • Чем собираете? Скорее всего, можно в сборщике указать тот модуль, чтобы он подхватился
    – gil9red
    11 сен 2020 в 20:33
  • Собираю через pyinstaller. Что ещё раз нужно сделать, не совсем понял?
    – Mswork6
    11 сен 2020 в 20:34
  • покажите свой исходный пример и строку, которой собираете exe.
    – S. Nick
    11 сен 2020 в 20:36
  • Добавил к посту
    – Mswork6
    11 сен 2020 в 20:39
  • добавьте images.qrc
    – S. Nick
    11 сен 2020 в 20:45

1 ответ 1

1

все сделал как вы, только в модуле IdiomsTheory.py заменил имена IdiomsTheory на w, т.к. они мне не понравились (с вашими не пробовал).

IdiomsTheory.py

...

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QMainWindow()                       # w
    ui = Ui_IdiomsTheory()
    ui.setupUi(w)                                     # w
    w.show()                                          # w 
    sys.exit(app.exec_())

images.qrc

<RCC>
  <qresource prefix="page1">
    <file>Images/Unit 4/4a/IdiomsImages/4a1.png</file>
  </qresource>
</RCC>
  • далее

    pyrcc5 images.qrc -o images_rc.py

  • далее

    pyinstaller -F -w IdiomsTheory.py

введите сюда описание изображения

введите сюда описание изображения

Все работает !

1
  • Сделал всё как вы написали и всё заработало, спасибо!
    – Mswork6
    12 сен 2020 в 6:00

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.