My .exe file cannot find its data files
My .exe file cannot find its data files
I used cx_freeze to create an .exe from my .py file. When I build the .exe file, my 2 data files are successfully included in the build folder, but when I run the .exe, the program cannot find the files.
My setup():
from cx_Freeze import setup, Executable
setup(name='pipermain',
options={ 'build_exe':{'packages':[ 'tkinter','PIL' ],'include_files': ['Transactions.csv', 'piper_copy.png' ]}},
description='',
executables= [Executable('pipermain.py')]
)
I believe this to be a problem with the filepath in my .py code-- How to I write the correct filepath for my .exe to find the data files, no matter what computer the .exe is run on?
my file path was:
filepath = 'Transactions.csv'
because the data file was in my .py project folder. This worked successfully when I ran the .py from PyCharm. But it couldn't locate the file in the build folder when I ran the .exe. Now, in an effort to make a file path that works with any system, it is:
filepath = os.path.abspath( 'Transactions.csv' )
The .exe only works if my filepath caters to my own system( /User/ethan/documents/project/transactions.csv). But if I send this .exe to others, the file path obviously won't work.
I have tried to make a variable file path by assigning, p = os.path.abspath( 'transactions.csv' ) to work on any system, but upon running the .exe I receive errors that the file does not exist. What am I doing wrong?
1 Answer
1
By including the method:
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
and then reference any file paths using:
logopath =resource_path('Logo.jpg')
Should hopfully solve you issue
Hmm. I used your method and I am getting the same problem as I was when using os.path.abspath( 'Transactions.csv' ). When trying to access the csv from my .exe, the terminal gives me an error that says: The file /Users/ethan/Transactions.csv does not exist. This is true, because the file isn't in the "ethan" directory. It is in the build folder. I just can't seem to get it to call the build folder for its files.
– etpt
2 days ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Dont forget to import os.path and sys
– Daniel.Bourne
2 days ago