[Python] Determining application path in a Python EXE generated by pyInstaller

Discussão em 'Python' iniciado por Stack, Junho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have an application that resides in a single .py file. I've been able to get pyInstaller to bundle it successfully into an EXE for Windows. The problem is, the application requires a .cfg file that always sits directly beside the application in the same directory.

    Normally, I build the path using the following code:

    import os
    config_name = 'myapp.cfg'
    config_path = os.path.join(sys.path[0], config_name)


    However, it seems the sys.path is blank when its called from an EXE generated by pyInstaller. This same behaviour occurs when you run the python interactive command line and try to fetch sys.path[0].

    Is there a more concrete way of getting the path of the currently running application so that I can find files that are relative to it?

    Continue reading...

Compartilhe esta Página