Ao tentar importar o Matplotlib.pyplot, obtenha “UnicodeDecodeError”

2

Ao tentar executar um script Python começando com

import numpy as np
import matplotlib.pyplot as plt

Estou recebendo a seguinte mensagem de erro:

/usr/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
Traceback (most recent call last):
  File "/home/kurt/Documents/Python/tax_rates.py", line 2, in <module>
    import matplotlib.pyplot as plt
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 29, in <module>
    import matplotlib.colorbar
  File "/usr/lib/python2.7/dist-packages/matplotlib/colorbar.py", line 34, in <module>
    import matplotlib.collections as collections
  File "/usr/lib/python2.7/dist-packages/matplotlib/collections.py", line 27, in <module>
    import matplotlib.backend_bases as backend_bases
  File "/usr/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 62, in <module>
    import matplotlib.textpath as textpath
  File "/usr/lib/python2.7/dist-packages/matplotlib/textpath.py", line 15, in <module>
    import matplotlib.font_manager as font_manager
  File "/usr/lib/python2.7/dist-packages/matplotlib/font_manager.py", line 1421, in <module>
    _rebuild()
  File "/usr/lib/python2.7/dist-packages/matplotlib/font_manager.py", line 1406, in _rebuild
    fontManager = FontManager()
  File "/usr/lib/python2.7/dist-packages/matplotlib/font_manager.py", line 1044, in __init__
    self.ttffiles = findSystemFonts(paths) + findSystemFonts()
  File "/usr/lib/python2.7/dist-packages/matplotlib/font_manager.py", line 324, in findSystemFonts
    for f in get_fontconfig_fonts(fontext):
  File "/usr/lib/python2.7/dist-packages/matplotlib/font_manager.py", line 276, in get_fontconfig_fonts
    stderr=subprocess.PIPE)
  File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1340, in _execute_child
    raise child_exception
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

Eu tentei reinstalar o Python e o Matplotlib usando os comandos

sudo apt-get --reinstall install python
sudo apt-get --reinstall install python-matplotlib

mas sem sucesso. Como posso fazer o Pyplot importar corretamente?

    
por Kurt Peek 18.06.2016 / 12:18

1 resposta

1

Obrigado pelos seus comentários. Eu tentei a sugestão de Jos de adicionar as seguintes linhas ao início de tax_rates.py :

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

Fiquei feliz em ver que o script agora roda e produz um enredo (veja abaixo uma captura de tela do editor do Atom).

Eu gostaria de receber uma solução mais 'permanente', no entanto.

P.S. Para sua referência, todo o script tax_rates.py é copiado abaixo.

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

import numpy as np
import matplotlib.pyplot as plt

# income = np.linspace(0,2e6,2001)

thresholds = np.array([0, 9725, 37650, 190150, 413350, 415050])

print(thresholds)

plt.figure()
plt.plot(thresholds,thresholds)
plt.show()
    
por Kurt Peek 18.06.2016 / 14:23