0

I am trying to generate a .exe for my python app. Inside the code I import some embed files like so:

f = open('source/store/data_dictionary.json')

Trying to run the app from the generated .exe, I obtain an error like this:

FileNotFoundError: [Errno 2] No such file or directory: 'source/store/data_dictionary.json'

full output error from the console

To generate the .exe, I've run:

pyinstaller --onefile app.py

I don't know if there's a better way to import files inside a python scrip to generate executables, or if I have to write some setup.

You can check my source code here: https://github.com/GraphFilter/GraphFilter

Lavínia Beghini
  • 165
  • 1
  • 11
  • Delete the dist folder and run you cut your exe and paste it where should code was – Arkodeep Ray May 24 '21 at 04:38
  • It works, but the problem is that I don't want to share this folder. I'm planning to deploy it, and all I want the user to have is the .exe (and the bundled files). There's also images that I'm calling in the relative path. Is there a way to tell PyInstaller to not bundle this image and info files and redo the relative path? – Lavínia Beghini May 24 '21 at 04:42
  • Then if you want it to work in the dist folder you have to change your path of the script `f = open('source/store/data_dictionary.json')` you have to give the path from your C:/ directory for it to work – Arkodeep Ray May 24 '21 at 04:45
  • So you want to embed some data files in your onefile exe? You’ll have to add the data files into a specific path in the exe, and modify your code so that when it’s run from the onefile exe it uses a different way to find the files - read the documentation https://pyinstaller.readthedocs.io/en/stable/runtime-information.html#placing-data-files-at-expected-locations-inside-the-bundle – DisappointedByUnaccountableMod May 24 '21 at 09:56
  • 1
    See Jonathon Reiner’s answer here for a helpful function `resource_path()` which will allow you to access files regardless of running from ee or from source https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile/54881911#54881911 -note it’s in general a bad idea to use `os.chdir()` and this function avoids that. – DisappointedByUnaccountableMod May 25 '21 at 21:55

1 Answers1

0

Having had a look at the source code, I notice that the path is relative to the location of the file app.py. So as long as that relative path is the same once you create the executable. ie. if you make it a bundle in which the exe is in the same place as app.py. Then the following should work regardless of where that bundle is

For this you can use the following code to get the directory of the exe file when it is run and use that to get to your relative files

import os

cur_dir = os.path.dirname(__file__) # print this to see what it looks like
             # something like: 'c:/path/to/file'

# then using an f-string to get the absolute path of the required files
f = open(f'{cur_dir}/source/store/data_dictionary.json') # essentially 'c:/path/to/file/source/store....'

EDIT: (not tested, but you could also change the working directory and use the same code as before)

import os

cur_dir = os.path.dirname(__file__)
os.chdir(cur_dir) # change working directory

# print os.cwd() to see the current working directory, it should be the path to the file

#then proceed as before
f = open('source/store/data_dictionary.json')
.
.
.
Muhd Mairaj
  • 557
  • 4
  • 13
  • The problem is that I wanted to distribute only the executable file. But with your answer I figured that I have to distribute also the resources such as images and this json file. – Lavínia Beghini May 31 '21 at 03:41