[GIS] QGIS 2.14 Plugin Encoding Error

asciiencodingpyqgisqgisutf-8

I've developed a plugin for QGIS 2.12 which uses data that is encoded in UTF-8. The plugin works fine for 2.12, but when I try to start it in QGIS 2.14 it throws me the following error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 4: ordinal not in range(128)

The code snippet which causes the exception is

value = QLabel(str(feature[field]).decode('utf-8')).

In former cases the error was thrown because I forgot to decode the characters, but I do convert them in that case. I already tried to remove the str() and the .decode('utf-8') calls, but the exception still gets thrown.

Why does this work for QGIS Lyon but not for QGIS Essen?

Best Answer

Ok, I just found a workaround by explicitly prevent python from using the ascii coding as standard:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

It might not be very elegant, but the exception is not thrown anymore. But it should be noted that the QGIS embedded python console stops outputting any information after reloading the plugin using PluginReloader.

Related Question