I have the following python code that is working when I run it using Pycharm :
def get_csv(): # Import data from a csv file
global df
import_file_path = filedialog.askopenfilename(filetypes=[("CSV file", '*.csv')])
if not import_file_path: # If user press cancel select path display error message
tk.messagebox.showwarning(title="Error message", message="CSV file import aborted.")
else:
# print("CSV path :", import_file_path)
with open(import_file_path, "rb") as csv_bin: # automatic CSV encoding detection
result = chardet.detect(csv_bin.read(10000))
encode = result['encoding']
# print("encoding :", encode)
with open(import_file_path) as csv_file:
reader = csv.reader(csv_file, delimiter=';')
first_row = next(reader)
num_cols = len(first_row)
if num_cols > 445:
df = pd.read_csv(import_file_path, sep=';', header=0, na_values=['#NV', ' '], decimal=',', encoding=encode)
tk.messagebox.showinfo(title="Import success", message="CSV file import successful !")
return df
else:
tk.messagebox.showwarning(title="Error message", message="CSV file not compatible")
I have used pyinstaller to get EXE file corresponding to my python code but I'm getting this error :
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1702, in __call__
File "import_CSV_python.py", line 30, in get_csv
File "c:\users\cyrille\appdata\local\programs\python\python37-32\lib\codecs.py
", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 1759: inval
id start byte
I don't have this error when I run my python code using Pycharm. It is working correctly. On my other machine I didn't have any problems with pyinstaller to create working EXE from python file.
I use this command to call pyinstaller and to create the EXE.
pyinstaller --onefile import_CSV_python.py
I have looked online and I found some people that have the same error as I do and their solution is modifying the compat.py file. This sounds complicated for me and I'm completely lost. I have struggled beforehand to have a somewhat working EXE that display the little window with button to launch my different functions. On my other computer (I'm currently working from my parents house from a different machine) running pyinstaller to get working EXE from python file isn't a problem at all it works great.
Edit :
I tried to replace the line out = out.decode(encoding)
in the compat.py file from pyinstaller package by out = out.decode(encoding, "replace")
or out = out.decode(encoding, "ingore")
then saving the compat.py file and generating EXE using pyinstaller again with no result. I got the same error : utf8 can't decode
Edit2 : Thanks to John Szakmeister I changed this line
with open(import_file_path, encoding=encode) as csv_file:
It works fine now.