Hellbea
Hellbea

Reputation: 299

Real path to config.ini in python binary *app in MacOS

I have this code:

def _read_config(self):
    config = configparser.ConfigParser()
    config.sections()
   # I tried 
    path_main = os.path.dirname(os.path.realpath(__file__))
   # and this after create exec file with pyinstaller nothing changed
    path_main = os.getcwd() 
    print(path_main)
    file = os.path.join(path_main, "config.ini")
    print(file)
    config.read(file)
    return config

When I run the code in MacOS using the terminal with python gui.py, it prints this:

/Users/telos/Desktop/Telos-Monitor-Tool/client
/Users/telos/Desktop/Telos-Monitor-Tool/client/config.ini

But when I do pyinstaller --onefile --windowed gui.py, I receive 1 app file, when I run it I get this:

/Users/telos
/Users/telos/config.ini

But the one file app and ``gui.py` is in the same directory.

So I have an error because the Python parser can't find config.ini.

As in comennt discasion advise me to use print(QtCore.QCoreApplication.applicationDirPath()) after recreating app, i have 2 file 1 gui.app, 2-nd gui.exec. gui.exec find config.ini fine and all work fine, but gui.app can't and send the error.

Any idea what is the problem?

Upvotes: 2

Views: 897

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

Since you are using PyQt5 if you want to get the executable folder you can use:

QtCore.QCoreApplication.applicationDirPath()

Upvotes: 1

Related Questions