Search code examples
pythonpyinstallerconfigparser

Error when compiling with pyinstaller with configparser


I get an error when i'm compiling with pyinstaller it works on my pc not on a different pc.

error i get

Traceback (most recent call last):
  File "app.py", line 30, in <module>
    password = parser.get('settings', 'password')
  File "configparser.py", line 781, in get
  File "configparser.py", line 1152, in _unify_values
configparser.NoSectionError: No section: 'settings'
[9464] Failed to execute script 'app' due to unhandled exception!

Here is my code

parser = ConfigParser()
parser.read('C:\\Users\\abc\Desktop\\Maker\\settings.ini')

I followed some solutions but still no luck is solving this can anyone please help?

I tried this solution as well with no luck

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

parser = ConfigParser()
settings_file = resource_path('settings.ini')

Solution

  • You have to make sure you are bundling your ini file with your application. This is a little more difficult if you are using UPX to compress all files into one executable file. I never saw the point in this because it seems like a false savings. Every time you run the EXE it will decompress all files into a temporary folder. Then your application now consumes all of the uncompressed space plus all of the compressed space. Bleah.

    I would recommend you use a Spec file to specify all of the additional files you want to include with your application.

    For debugging purposes, you can put this in your program to make sure the ini file is where you think it is (you can delete it or comment it out once you verify the file is there)

    print(os.listdir(<path>))
    

    or

    print(os.listdir(os.getcwd()))
    

    where getcwd stands for 'get current working directory' which should be the directory where your EXE resides.

    If you don't want to mess with a spec file, you can pass the --add-data parameter to pyinstaller and pass your ini file.