2

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?

Hellbea
  • 289
  • 6
  • 14
  • I don't see a call to `os.getcwd()` in your code. – 9769953 Sep 20 '18 at 13:30
  • changes with `os.getcwd()` but try and `getcwd()` and that way, same – Hellbea Sep 20 '18 at 13:37
  • 2
    The working directory isn’t the directory the script/executable is in, it’s the directory you were in when you run it. If you’re in /Users/telos when you run it (either version), that’s what its working directory will be. – Gordon Davisson Sep 20 '18 at 15:21
  • How i can check where the app situated? from Python? – Hellbea Sep 20 '18 at 16:10
  • have you read the question ,`path_main =os.path.dirname(os.path.realpath(__file__))` i tried, same as` getcwd` after i created exec file for mac the application can't find config And return `/Users/telos/config.ini` but config `/Users/telos/Desktop/Telos-Monitor-Tool/client/config.ini` – Hellbea Sep 20 '18 at 18:59
  • In what folder is the binary? – eyllanesc Sep 20 '18 at 19:05
  • `/Users/telos/Desktop/Telos-Monitor-Tool/client` – Hellbea Sep 20 '18 at 19:10
  • But this command in binary `path_main = os.path.dirname(os.path.realpath(__file__))` print `/Users/telos/config.ini` as result error cause in that folder no that file – Hellbea Sep 20 '18 at 19:12
  • @Hellbea `print(QtCore.QCoreApplication.applicationDirPath())` – eyllanesc Sep 20 '18 at 19:14
  • @eyllanesc print(QtCore.QCoreApplication.applicationDirPath()) you are my Hero, work. post the nswer i will confirm – Hellbea Sep 20 '18 at 19:24
  • @Hellbea I can not since your question is closed, delete this question and publish a new one stating that your application is done in PyQt5 and I can publish an answer. – eyllanesc Sep 20 '18 at 19:27
  • @Hellbea I reopened this question. Please edit it to provide all of the relevant information. You can then prompt the people who helped you to provide an answer for posterity, or answer it yourself if you feel confident you can provide an explanation. – Patrick Haugh Sep 20 '18 at 20:45
  • @eyllanesc Edited question how you can post answer and i edit have new problem. Thx for help – Hellbea Sep 20 '18 at 21:14
  • @Hellbea You say you have an error, what is the error? – eyllanesc Sep 20 '18 at 21:22

1 Answers1

1

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

QtCore.QCoreApplication.applicationDirPath()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • do you know why exec run and find file normally and app no, have two file in pyinstaller dist directory? – Hellbea Sep 20 '18 at 21:22
  • @Hellbea I do not have mac, I have tried it in linux and windows, in the case of linux it generates a .elf and in the case of windows it generates an .exe that are executable, in the case of mac I suppose that it should generate you only an executable , I do not understand why you have 2 outputs, on the other hand if you are going to manage setting I recommend using QSettings, with that class you only have to establish some minimum configurations and he creates the .ini in the generic place. – eyllanesc Sep 20 '18 at 21:25
  • @Hellbea the path used by QSettings does not depend on the executable. – eyllanesc Sep 20 '18 at 21:26
  • me too windows and linux oerfect decision, but mac os dont want to work corectly – Hellbea Sep 20 '18 at 21:31
  • @Hellbea The applications that I did work for mac but I do not use ConfigParser but QSettings, to use it you only have to set `QCoreApplication.setOrganizationXXX()`, as it indicates it will create the config file in the correct place that depends on the OS, and not on the app. For more information read http://doc.qt.io/qt-5/qsettings.html – eyllanesc Sep 20 '18 at 21:35
  • I guees it is only sane desicion for mac os. – Hellbea Sep 20 '18 at 21:38