Search code examples
pythonpyinstallerexepyvisfilenotfounderror

PyInstaller Executable FileNotFoundError (Errno 2)


I'm trying to package a Python application into a standalone .exe file.

I installed PyInstaller using

 pip install --user pyinstaller

and built my project using

 pyinstaller -F  test.py

However, I get this error when I try to perform a certain operation:

Console output when running .exe

For context, WordNetGraph.py is a local module that was bundled into the .exe when I created it. Is this error due to the mixed usage of forward slashes '/' and backslashes '/' in the path? How should I go about fixing this? Thanks!

UPDATE 1:

Here's a minimal version of my code that produces the same error:

#!/usr/bin/env python
from pyvis.network import Network
import networkx as nx
from PyQt5 import QtWidgets, QtGui
from PyQt5 import Qt, QtCore, QtWebEngineWidgets

class MyWidget(QtWidgets.QWidget):

    def __init__(self, parent=None):
        super().__init__(parent=parent)

        self.mainLayout = QtWidgets.QVBoxLayout()
        self.setLayout(self.mainLayout)

        nx_graph = nx.cycle_graph(10)
        nx_graph.nodes[1]['title'] = 'Number 1'
        nx_graph.nodes[1]['group'] = 1
        nx_graph.nodes[3]['title'] = 'I belong to a different group!'
        nx_graph.nodes[3]['group'] = 10
        nx_graph.add_node(20, size=20, title='couple', group=2)
        nx_graph.add_node(21, size=15, title='couple', group=2)
        nx_graph.add_edge(20, 21, weight=5)
        nx_graph.add_node(25, size=25, label='lonely', title='lonely node', group=3)
        nt = Network('500px', '500px')
        nt.from_nx(nx_graph)

        html_text = nt.generate_html()
        self.graphOutPut = QtWebEngineWidgets.QWebEngineView()
        self.graphOutPut.setHtml(html_text)
        self.mainLayout.addWidget(self.graphOutPut)

def main():
    app = QtWidgets.QApplication([])

    win = MyWidget()
    win.show()
    win.raise_()
    app.exec_()

if __name__ == "__main__":
    main()

This is the command that I used:

pyinstaller --onefile minimalerrortest.py 

This is the error that I get:

enter image description here

Update 2:

I realized that the directory

C:\Users<user>\AppData\Local\Temp\_MEI55522

does not exist on my system. There is however a directory

C:\Users<user>\AppData\Local\Temp\_MEI126042

In this directory, I can see some folders for Python libraries (eg. numpy, pandas, sklearn, etc.) but no folder for pyvis. I located the missing file at

C:\Python310\Lib\site-packages\pyvis\templates\template.html

I tried to include it in the PyInstaller .exe with the command below but I'm still getting the same error.

pyinstaller --add-binary="C:\Python310\Lib\site-packages\pyvis\templates\template.html" --onefile minimalerrortest.py

The code doesn't produce any errors (to my knowledge) when I run it in my IDE. I'm also curious as to why the .exe application would be looking for local files. Any clues on what I did wrong? Thanks!


Solution

  • I poked around a bit and noticed that when I ran

    pyinstaller minimalerrortest.py
    

    the contents of resulting \dist directory were virtually identical to those in the

    C:\Users\AppData\Local\Temp\_MEI<numbers>

    directory. Crucially, there was no folder for pyvis.

    I copied the

    C:\Python310\Lib\site-packages\pyvis

    folder into the \dist directory and the .exe ran without errors.

    I then found that

    pyinstaller --add-binary="C:\Python310\Lib\site-packages\pyvis;pyvis" --onefile minimalerrortest.py

    solved my issue.