Error when using pyinstaller: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff
Question:
I have an issue when i compile a PyQt code with pyinstaller.
I use this line to compile:
c:Anaconda3Scriptspyinstaller.exe -y -F --distpath="." MyQt.py
then I get this error message:
File "c:anaconda36bislibsite-packagesPyInstallerhookshook-zmq.py", line
18, in <module>
hiddenimports.extend(collect_submodules('zmq.backend'))
File "c:anaconda36bislibsite-packagesPyInstallerutilshooks__init__.py",
line 619, in collect_submodules
repr(pkg_dir), package))
File "c:anaconda36bislibsite-packagesPyInstallerutilshooks__init__.py",
line 90, in exec_statement
return __exec_python_cmd(cmd)
File "c:anaconda36bislibsite-packagesPyInstallerutilshooks__init__.py",
line 77, in __exec_python_cmd
txt = exec_python(*cmd, env=pp_env)
File "c:anaconda36bislibsite-packagesPyInstallercompat.py", line 562, in
exec_python
return exec_command(*cmdargs, **kwargs)
File "c:anaconda36bislibsite-packagesPyInstallercompat.py", line 369, in
exec_command
out = out.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 152: invali
d start byte
The error message is not clear to me and I don’t understand why this happens.
Is it possible that pyinstaller try to use a module which is not compatible?
I use these in my script:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
# from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
import numpy as np
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from scipy.ndimage import imread
from scipy.ndimage.morphology import binary_dilation
from scipy.optimize import curve_fit, leastsq
update
the issue printed in the console come directly after
142859 INFO: Loading module hook "hook-zmq.py"...
So it should mean that the error is coming from zmq?
Answers:
I found an answer on another forum.
I change the line number 369 in the PythonLibsite-packagesPyinstallercompat.py
file:
out = out.decode(encoding)
to
out = out.decode(encoding, errors='ignore')
or
out = out.decode(encoding, "replace")
Now I can compile my script without any issue.
I still don’t know why my issue happened in the first place but at least that compiles now.
Changing compat.py works for me:
out = out.decode(encoding, “replace”)
This is a known problem on pyinstaller and the developers are working on it.
https://github.com/pyinstaller/pyinstaller/pull/3895
I hope this bug will solved on the next update.
The answer still works 2 years later BUT the line changed from 368 to 428.
In the newest version (3.5) , the line moved slightly to 427.
The best thing to do is to search for
out = out.decode(encoding)
and replace it with
out = out.decode(encoding, "replace")
I don’t understand why they do not fix this annoying problem!
The error is fixed since Version 4.0
pip install "pyinstaller>=4.0.0"
I faced this problem running under MSYS2.
To fix that i copy the project folder from C:/Users/Χρήστος/Desktop to C:/Python/Scripts
then i run the pyinstaller command from MSYS2 terminal, and finally i run the executable the command prompt console.
The error was that in the first path there is "Χρήστος" (greek name), but in the second there isn’t.
Note: I search for file compat.py in msys64 directory but i don’t found it.
Note necessarily the answer to this issue, but for others who might find this.
I’ve use TAB to auto complete my pyinstaller command and it defaulted to the .exe file in my root instead of the .py file.
I have an issue when i compile a PyQt code with pyinstaller.
I use this line to compile:
c:Anaconda3Scriptspyinstaller.exe -y -F --distpath="." MyQt.py
then I get this error message:
File "c:anaconda36bislibsite-packagesPyInstallerhookshook-zmq.py", line
18, in <module>
hiddenimports.extend(collect_submodules('zmq.backend'))
File "c:anaconda36bislibsite-packagesPyInstallerutilshooks__init__.py",
line 619, in collect_submodules
repr(pkg_dir), package))
File "c:anaconda36bislibsite-packagesPyInstallerutilshooks__init__.py",
line 90, in exec_statement
return __exec_python_cmd(cmd)
File "c:anaconda36bislibsite-packagesPyInstallerutilshooks__init__.py",
line 77, in __exec_python_cmd
txt = exec_python(*cmd, env=pp_env)
File "c:anaconda36bislibsite-packagesPyInstallercompat.py", line 562, in
exec_python
return exec_command(*cmdargs, **kwargs)
File "c:anaconda36bislibsite-packagesPyInstallercompat.py", line 369, in
exec_command
out = out.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 152: invali
d start byte
The error message is not clear to me and I don’t understand why this happens.
Is it possible that pyinstaller try to use a module which is not compatible?
I use these in my script:
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
# from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
import numpy as np
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from scipy.ndimage import imread
from scipy.ndimage.morphology import binary_dilation
from scipy.optimize import curve_fit, leastsq
update
the issue printed in the console come directly after
142859 INFO: Loading module hook "hook-zmq.py"...
So it should mean that the error is coming from zmq?
I found an answer on another forum.
I change the line number 369 in the PythonLibsite-packagesPyinstallercompat.py
file:
out = out.decode(encoding)
to
out = out.decode(encoding, errors='ignore')
or
out = out.decode(encoding, "replace")
Now I can compile my script without any issue.
I still don’t know why my issue happened in the first place but at least that compiles now.
Changing compat.py works for me:
out = out.decode(encoding, “replace”)
This is a known problem on pyinstaller and the developers are working on it.
https://github.com/pyinstaller/pyinstaller/pull/3895
I hope this bug will solved on the next update.
The answer still works 2 years later BUT the line changed from 368 to 428.
In the newest version (3.5) , the line moved slightly to 427.
The best thing to do is to search for
out = out.decode(encoding)
and replace it with
out = out.decode(encoding, "replace")
I don’t understand why they do not fix this annoying problem!
The error is fixed since Version 4.0
pip install "pyinstaller>=4.0.0"
I faced this problem running under MSYS2.
To fix that i copy the project folder from C:/Users/Χρήστος/Desktop to C:/Python/Scripts
then i run the pyinstaller command from MSYS2 terminal, and finally i run the executable the command prompt console.
The error was that in the first path there is "Χρήστος" (greek name), but in the second there isn’t.
Note: I search for file compat.py in msys64 directory but i don’t found it.
Note necessarily the answer to this issue, but for others who might find this.
I’ve use TAB to auto complete my pyinstaller command and it defaulted to the .exe file in my root instead of the .py file.