{"id":152,"date":"2019-02-23T14:25:06","date_gmt":"2019-02-23T18:25:06","guid":{"rendered":"http:\/\/notebook.chaopricha.com\/?p=152"},"modified":"2019-02-23T14:25:06","modified_gmt":"2019-02-23T18:25:06","slug":"pyinstaller-trouble-with-pyqt5-and-matplotlib-on-python-3-7","status":"publish","type":"post","link":"https:\/\/notebook.chaopricha.com\/?p=152","title":{"rendered":"Pyinstaller Trouble with PyQt5 and Matplotlib on Python 3.7"},"content":{"rendered":"<p>Pyinstaller is a fantastic python to exe packager. When I tried making a stand alone exe with a python script (PyQt5Test.py) using a quick PyQt5 example, it creates a working exe file right off the bat using this simple command:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>pyinstaller PyQt5Test.py --onefile<\/code><\/pre>\n\n\n\n<p>Here&#8217;s PyQt5Test.py:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\nfrom PyQt5.QtWidgets import QWidget, QMessageBox, QApplication\n\nclass Example(QWidget):    \n    def __init__(self):\n        super().__init__()        \n        self.initUI()        \n        \n    def initUI(self):               \n        \n        self.setGeometry(300, 300, 250, 150)        \n        self.setWindowTitle('Message box')    \n        self.show()       \n        \n    def closeEvent(self, event):\n        \n        reply = QMessageBox.question(self, 'Message',\n            \"Are you sure to quit?\", QMessageBox.Yes | \n            QMessageBox.No, QMessageBox.No)\n\n        if reply == QMessageBox.Yes:\n            event.accept()\n        else:\n            event.ignore()        \n                \nif __name__ == '__main__':\n    \n    app = QApplication(sys.argv)\n    ex = Example()\n    sys.exit(app.exec_())<\/code><\/pre>\n\n\n\n<p>Running the executable file from the dist folder in works perfectly just as the same way as you just run the script in the interpreter<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"250\" height=\"180\" src=\"http:\/\/notebook.chaopricha.com\/wp-content\/uploads\/2019\/02\/image-1.png\" alt=\"\" class=\"wp-image-156\"\/><\/figure>\n\n\n\n<p>I left the console option in the background so that if there&#8217;s an error I could see it when the program runs.  However, you could get rid of the console by adding &#8220;&#8211;noconsole&#8221; option to pyinstaller.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pyinstaller PyQt5Test.py --onefile --noconsole<\/code><\/pre>\n\n\n\n<p>Now that works so well, I want to see whether I could make a standalone file with a script that has PyQt5 and Matplotlib.  Here&#8217;s the code I use to test this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\nimport PyQt5\nfrom PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, \\\n   QSizePolicy, QMessageBox, QWidget, QPushButton, QListWidget, QDesktopWidget\nfrom PyQt5.QtGui import QIcon\nfrom PyQt5 import QtCore\nimport PIL\nimport tkinter\nfrom tkinter import filedialog\n\n\nfrom matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas\nfrom matplotlib.figure import Figure\nimport matplotlib.pyplot as plt\n\nimport random\n\nclass App(QMainWindow):\n\n    def __init__(self):\n        super().__init__()\n        self.left = 10\n        self.top = 10\n        self.title = 'PyQt5 matplotlib example'\n        self.setFixedSize(640, 700)\n        self.initUI()\n\n    def location_on_the_screen(self):\n        ag = QDesktopWidget().availableGeometry()\n        sg = QDesktopWidget().screenGeometry()\n\n        widget = self.geometry()\n        #x = ag.width() - widget.width()\n        #y = 2 * ag.height() - sg.height() - widget.height()\n        x = sg.width() - widget.width()\n        y = 2 * ag.height() - sg.height() - widget.height()\n        self.move(x, y)\n\n    def initUI(self):\n        self.setWindowTitle(self.title)\n        #self.setGeometry(self.left, self.top, self.width, self.height)\n        self.location_on_the_screen()\n\n        m = PlotCanvas(self, width=5, height=4)\n        m.move(0,0)\n\n        button = QPushButton('PyQt5 button', self)\n        button.setToolTip('This s an example button')\n        button.move(500,0)\n        button.resize(140,100)\n\n        button2 = QPushButton('Plot', self)\n        button2.setToolTip('Plot Data')\n        button2.move(500,102)\n        button2.resize(140,100)\n\n        listBox = QListWidget(self)\n        listBox.setGeometry(QtCore.QRect(120,10,281,192))\n        listBox.move(500,400)\n\n        self.show()\n\n\nclass PlotCanvas(FigureCanvas):\n\n    def __init__(self, parent=None, width=5, height=4, dpi=100):\n        fig = Figure(figsize=(width, height), dpi=dpi)\n        self.axes = fig.add_subplot(111)\n\n        FigureCanvas.__init__(self, fig)\n        self.setParent(parent)\n\n        FigureCanvas.setSizePolicy(self,\n                QSizePolicy.Expanding,\n                QSizePolicy.Expanding)\n        FigureCanvas.updateGeometry(self)\n        self.plot()\n\n\n    def plot(self):\n        data = [random.random() for i in range(25)]\n        ax = self.figure.add_subplot(111)\n        ax.plot(data, 'r-')\n        ax.set_title('PyQt Matplotlib Example')\n        self.draw()\n\nif __name__ == '__main__':\n    app = QApplication(sys.argv)\n    ex = App()\n    sys.exit(app.exec_())<\/code><\/pre>\n\n\n\n<p>When creating an exe for this script using Pyinstaller I see a lot of warnings but no errors.  I even got an exe.  However, when I ran the exe I got an error:<br>ModuleNotFoundError: No module named &#8216;numpy.core._dtype_ctypes&#8217;<\/p>\n\n\n\n<p>I did have Numpy correctly installed using pip and I have version 1.16.0 and it&#8217;s supposedly the most up-to-date version.<\/p>\n\n\n\n<p>After Googling the error, I found that the problem was indeed Numpy itself and the issue is fixed if I have version 1.16.1.   However, doing pip install numpy doesn&#8217;t work since it insists that 1.16.0 was the up-to-date version.<\/p>\n\n\n\n<p>Therefore, I have to force the install of 1.16.1 Numpy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install numpy==1.16.1<\/code><\/pre>\n\n\n\n<p>This forces the new version of Numpy to be installed.  I tried to create the exe file again using Pyinstaller.  Again, the exe creation went just fine as before.  I wasn&#8217;t sure whether this would work or not, but to my surprise when running the exe file I have a working program!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"642\" height=\"732\" src=\"http:\/\/notebook.chaopricha.com\/wp-content\/uploads\/2019\/02\/image-2.png\" alt=\"\" class=\"wp-image-157\" srcset=\"https:\/\/notebook.chaopricha.com\/wp-content\/uploads\/2019\/02\/image-2.png 642w, https:\/\/notebook.chaopricha.com\/wp-content\/uploads\/2019\/02\/image-2-263x300.png 263w\" sizes=\"(max-width: 642px) 100vw, 642px\" \/><\/figure>\n\n\n\n<p>I was quite pleased with the result.  Now I could move forward with using Pyinstaller to make a standalone program.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pyinstaller is a fantastic python to exe packager. When I tried making a stand alone exe with a python script (PyQt5Test.py) using a quick PyQt5 example, it creates a working exe file right off the bat using this simple command: &hellip; <a href=\"https:\/\/notebook.chaopricha.com\/?p=152\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=\/wp\/v2\/posts\/152"}],"collection":[{"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=152"}],"version-history":[{"count":2,"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=\/wp\/v2\/posts\/152\/revisions"}],"predecessor-version":[{"id":158,"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=\/wp\/v2\/posts\/152\/revisions\/158"}],"wp:attachment":[{"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notebook.chaopricha.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}