Skip to content Skip to sidebar Skip to footer

Pyinstaller Seems Not To Find A Data File

Edit 3: I replaced __file__ with sys.argv[0], when I need to know the location of my script/executable. This is not exactly the same, but in my case it seems to run fine (at least

Solution 1:

Firstly, it might be wise to do a print config_file / os.path.exists(config_file) before reading it, so you can be sure where the file is and if python can find it.

As to actually accessing it, os.path.split(__file__) looks almost correct, but I'm not sure it works properly under pyinstaller - the proper way of packing files is to add them to the .spec file, pyinstaller will then load them at compile time and unpack them to $_MEIPASS2/ at run time. To get the _MEIPASS2 dir in packed-mode and use the local directory in unpacked (development) mode, I use this:

def resource_path(relative):
    return os.path.join(
        os.environ.get(
            "_MEIPASS2",
            os.path.abspath(".")
        ),
        relative
    )


# in development
>>> resource_path("logging.conf")
"/home/shish/src/my_app/logging.conf"

# in deployment
>>> resource_path("logging.conf")
"/tmp/_MEI34121/logging.conf"

Solution 2:

The error message ConfigParser.NoSectionError: No section: 'formatters' suggests that it's not a missing file but a file with a missing section that you should be looking for.


Solution 3:

I had a similar problem but couldn't find a elegant fix so far. The 'hack' I use that got me trough, say my project is located in '~/project/project_root', first in the .spec file:

excluded_sources = TOC([x for x in a.pure if not x[0].startswith('project_root')])

Here a is the Analysis object, basically I remove all of my projects files from the PYZ so no import is passed there and the logger's relative paths won't be computed from there. After this, create a Tree object from the project.

my_project_tree = Tree('~/project')

Then add this Tree to the list of TOC that is passed to COLLECT, so :

COLLECT( exe,
           a.binaries,
           a.zipfiles,
           a.datas,
           my_project_tree,
           ....)

You should have your project folder added to the dist folder. The problem is that you'll end up distributing the pyc's of your project too, but couldn't find a better way so far. Very interested in the valid solution.


Post a Comment for "Pyinstaller Seems Not To Find A Data File"