1

I'm importing mtplotlib.pyplot to my python module and get UnicodeDecodeError that I didn't manage to fix with previous solutions to similar problems.

I run python 2.7 on windows 10. Installed matplotlib version 2.2.3

import matplotlib.pyplot as plt

fails with the error:

  File "C:\Users\my_user\Desktop\csv_cleaning\utils.py", line 6, in <module>
    import matplotlib.pyplot as plt
  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 72, in <module>
    from matplotlib.backends import pylab_setup
  File "C:\Python27\lib\site-packages\matplotlib\backends\__init__.py", line 16, in <module>
    line for line in traceback.format_stack()
  File "C:\Python27\lib\site-packages\matplotlib\backends\__init__.py", line 18, in <genexpr>
    if not line.startswith('  File "<frozen importlib._bootstrap'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 17: ordinal not in range(128)
  • 2
    Is there a good reason you are not using Python 3 already? It is the recommended and supported version of the language. – tripleee Dec 31 '18 at 11:38
  • I would love to but university servers supports only python 2.7. Moreover, I have already saw that people with the same version of python and matplotlib didn't encounter this error. – Ori Netanel Ben-Zaken Dec 31 '18 at 11:42

1 Answers1

2

Make sure your filename and folder does not have any non ASCII characters. This does not occur usually, the matplotlib team right now is focused on solving bugs in python3 only as python2 will soon be deprecated. That will mostly clear the error. This is something you can try if that doesn't work as a last resort. You can try adding

import sys  
reload(sys)   
sys.setdefaultencoding('utf8')

import matplotlib.pyplot as plt
Sreekiran A R
  • 3,123
  • 2
  • 20
  • 41
  • I tried that and it's not working. Still get the same error. Should I put it in the file of the main function? – Ori Netanel Ben-Zaken Dec 31 '18 at 11:38
  • 1
    make sure your filename and folder does not have any non ASCII characters. check https://stackoverflow.com/questions/30095006/python-unicode-decode-error-when-importing-matplotlib – Sreekiran A R Dec 31 '18 at 11:57
  • 2
    The reload trick to bypass the disable of setdefaultencoding is [not recommended](https://anonbadger.wordpress.com/2015/06/16/why-sys-setdefaultencoding-will-break-code/). – Mark Tolonen Dec 31 '18 at 20:55
  • Edited the answer with the acual solution which helped. @Mark thanks for the information. – Sreekiran A R Jan 01 '19 at 05:17