DEV Community

tigerfanxiao
tigerfanxiao

Posted on • Updated on

Pyinstaller or Python eel generated .exe refer external files errors

When we are trying to generate .exe file for our python project, normally we use pyinstaller. If you are writing python eel project, basically in the background, eel encapsulate pyinstaller.

But sometimes we want to refer the external file in our project, for example for config or setting files. In development environment, we didn't found any issue, but when we run generated .exe file, we may encounterd error saying that cannot find the specific external files. The cause of this error is when you run .exe file, the app is referring the local python interpreter not the one packaged inside of .exe file.

In this blog, I wrote a function to refer the differrent project path for dev env and production.

Commands we use to generate .exe file

# eel command to generate .exe file
# where web is the dir for static files
python -m eel --onefile main.py web
Enter fullscreen mode Exit fullscreen mode
# pyinstaller command to generate .exe file
pyinstaller --onefile -w main.py
Enter fullscreen mode Exit fullscreen mode

function refer different path for dev env and production.

def get_app_running_path():
    # for .exe file
    if getattr(sys, 'frozen', False):
        app_path = os.path.dirname(sys.executable)
    else:
        # for dev environment
        app_path = os.path.dirname(__file__)
    return app_path
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
artydev profile image
artydev • Edited

Nice, thank you