【问题标题】:Python 2.7 on MacOS : Matplotlib causing ValueError: unknown locale: UTF-8 [duplicate]MacOS 上的 Python 2.7:Matplotlib 导致 ValueError:未知语言环境:UTF-8 [重复]
【发布时间】:2016-08-01 07:08:14
【问题描述】:

我正在运行 El Capitan MacOS 10.11.3 我对 Matplotlib 有疑问

以下 sn-p 导入 Matplotlib,我在 Chrome 中的 jupyter notebook 中执行

#These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import matplotlib.pyplot as plt

给出(长)错误信息


ValueError                                Traceback (most recent call last)
<ipython-input-3-de762420594c> in <module>()
      2 # before proceeding further.
      3 from __future__ import print_function
----> 4 import matplotlib.pyplot as plt

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py in <module>()
   1129 
   1130 # this is the instance used by the matplotlib classes
-> 1131 rcParams = rc_params()
   1132 
   1133 if rcParams['examples.directory']:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py in rc_params(fail_on_error)
    973         return ret
    974 
--> 975     return rc_params_from_file(fname, fail_on_error)
    976 
    977 

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py in rc_params_from_file(fname, fail_on_error, use_default_template)
   1098         parameters specified in the file. (Useful for updating dicts.)
   1099     """
-> 1100     config_from_file = _rc_params_in_file(fname, fail_on_error)
   1101 
   1102     if not use_default_template:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py in _rc_params_in_file(fname, fail_on_error)
   1016     cnt = 0
   1017     rc_temp = {}
-> 1018     with _open_file_or_url(fname) as fd:
   1019         try:
   1020             for line in fd:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.pyc in __enter__(self)
     15     def __enter__(self):
     16         try:
---> 17             return self.gen.next()
     18         except StopIteration:
     19             raise RuntimeError("generator didn't yield")

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py in _open_file_or_url(fname)
    998     else:
    999         fname = os.path.expanduser(fname)
-> 1000         encoding = locale.getdefaultlocale()[1]
   1001         if encoding is None:
   1002             encoding = "utf-8"

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.pyc in getdefaultlocale(envvars)
    541     else:
    542         localename = 'C'
--> 543     return _parse_localename(localename)
    544 
    545 

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.pyc in _parse_localename(localename)
    473     elif code == 'C':
    474         return None, None
--> 475     raise ValueError, 'unknown locale: %s' % localename
    476 
    477 def _build_localename(localetuple):

ValueError: unknown locale: UTF-8

如果我在终端输入 pip install matplotlib,

 $ pip install matplotlib

看起来效果不错

$ pip install matplotlib

Requirement already satisfied (use --upgrade to upgrade): matplotlib in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.6 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): pytz in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): cycler in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): pyparsing!=2.0.0,!=2.0.4,>=1.5.6 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six-1.10.0-py2.7.egg (from python-dateutil->matplotlib)

怎么了???
谢谢

【问题讨论】:

    标签: python macos matplotlib


    【解决方案1】:

    这是未知的语言环境错误。在您的 ~/.bash_profile 文件中添加这些行:

    export LC_ALL=en_US.UTF-8
    export LANG=en_US.UTF-8
    

    同样在终端运行这个命令来重新加载文件。

    source ~/.bash_profile
    

    【讨论】:

    • 非常感谢,你拯救了我的一天,你是个天才
    猜你喜欢
    • 2016-02-16
    • 2016-12-19
    • 2015-10-02
    • 1970-01-01
    • 2016-06-25
    • 2013-11-26
    • 1970-01-01
    相关资源
    最近更新 更多