导入matplotlib时出现Python Unicode解码错误 [英] Python Unicode Decode Error when importing matplotlib

查看:87
本文介绍了导入matplotlib时出现Python Unicode解码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 python 脚本中使用 matplotlib,但在终端中出现此错误:

I try to use matplotlib in my python script but I got this error in the terminal:

Traceback (most recent call last):
  File "graphique.py", line 5, in <module>
    import matplotlib.pyplot as plt
  File "/home/xavier/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py", line 1048, in <module>
    rcParams = rc_params()
  File "/home/xavier/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py", line 897, in rc_params
    fname = matplotlib_fname()
  File "/home/xavier/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py", line 748, in matplotlib_fname
    fname = os.path.join(os.getcwd(), 'matplotlibrc')
  File "/home/xavier/anaconda/lib/python2.7/posixpath.py", line 80, in join
    path += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 66: ordinal not in range(128)

这是我的python代码,我简单写了

Here is my python code, I simply wrote

# -*- coding: utf-8 -*-
import numpy as np
from math import *
import matplotlib.pyplot as plt

我该怎么办?

推荐答案

问题是您当前的工作目录中包含非ASCII字符.

The problem is that you have a non-ASCII character in your current working directory.

这实际上根本不应该成为问题,但这是因为其他因素的组合:

That actually shouldn't be a problem at all, but it is because of a combination of other things:

  • matplotlib 想要在您当前的工作目录中查找覆盖默认目录的本地 matplotlibrc 文件.
  • Python 认为您使用的是 C 语言环境,而不是 Ubuntu 14 应该默认使用的漂亮的 UTF-8 语言环境.
  • matplotlib wants to look in your current working directory for a local matplotlibrc file that overrides your default one.
  • Python thinks you're using the C locale instead of the nice UTF-8 locale that Ubuntu 14 should be defaulting to.

因此,为了快速解决此问题,只需从另一个目录中运行脚本,该目录中不包含非ASCII字符.

So, for a quick workaround, just run your script from a different directory, that has no non-ASCII characters in it.

如果你真的想解决问题:

  • 确保您具有最新的Ubuntu 14,Anaconda和matplotlib.
  • 从外壳中, echo $ LANG .结果应该是空的,或者是带有 UTF-8 的东西.如果没有,请搜索 AskUbuntu 以了解如何解决该问题.
  • 确保您的外壳和终端都设置为UTF-8.
  • Make sure you have the latest Ubuntu 14, Anaconda, and matplotlib.
  • From the shell, echo $LANG. The result should be either empty, or something with UTF-8 in it. If not, search AskUbuntu for how to fix that.
  • Make sure your shell and terminal are both set to UTF-8.

通过快速搜索 matplotlib 问题,它看起来像#3516,看起来它已在 #3594 中修复,我认为应该在 matplotlib 1.4.1+中.另请参见#3487 .因此,假设您 $ LANG 和终端是正确的,并且您的 matplotlib 为1.4.0或更早版本,这是最可能的原因,并进行更新(通过condapipapt-get 或更新 Anaconda 本身,但您最初安装它)应该是修复.

From a quick search of the matplotlib issues, this looks like #3516, which looks like it was fixed in #3594, which I think should be in matplotlib 1.4.1+. Also see #3487. So, assuming you $LANG and terminal are correct, and your matplotlib is 1.4.0 or earlier, this is the most likely cause, and updating it (via conda or pip or apt-get or updating Anaconda itself, however you originally installed it) should be the fix.

或者,当然,您可以升级到 Python 3,这可能会解决问题,或者为您提供更好的错误消息,告诉您究竟出了什么问题.(不过,从扫描问题报告来看, matplotlib 1.4.0在Python 3中似乎没有这个确切的错误,只有Python 2符合预期,但是可能有一个相关的错误……)

Or, of course, you can upgrade to Python 3, which will probably either solve the problem, or give you a better error message that tells you exactly what's wrong. (Although, from scanning the issue report, it looks like matplotlib 1.4.0 doesn't have this exact bug in Python 3, only Python 2, as expected… but it may have a related bug…)

这篇关于导入matplotlib时出现Python Unicode解码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆