pandas 在 python 2.7 中导入熊猫时出现“ValueError: unknown locale: UTF-8”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38916461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:48:28  来源:igfitidea点击:

"ValueError: unknown locale: UTF-8" when importing pandas in python 2.7

pythonpython-2.7pandasimportpackages

提问by Hernando Andrianto Willy Ren

I'm very new in python and would like to learn using pandas, but I can't even use the package properly in my python.

我是 Python 新手,想学习使用 Pandas,但我什至无法在我的 Python 中正确使用该包。

In the terminal I tried

在终端我试过

$ conda install pandas

The result is:

结果是:

Fetching package metadata .......
Solving package specifications: ..........

# All requested packages already installed.
# packages in environment at /Users/rudyyuan/anaconda:
#
pandas                    0.18.1              np111py27_0  
Rudy-Yuans-MBP:~ rudyyuan$ 

Then I tried to install using pip:

然后我尝试使用 pip 安装:

Rudy-Yuans-MBP:~ rudyyuan$ pip install pandas
Requirement already satisfied (use --upgrade to upgrade): pandas in        
./anaconda/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): python-dateutil in     
./anaconda/lib/python2.7/site-packages (from pandas)
Requirement already satisfied (use --upgrade to upgrade): pytz>=2011k in    
./anaconda/lib/python2.7/site-packages (from pandas)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.7.0 in 
./anaconda/lib/python2.7/site-packages (from pandas)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in
./anaconda/lib/python2.7/site-packages (from python-dateutil->pandas)

Then I enter

然后我输入

$ python

Then I enter

然后我输入

$ import pandas

The result of the commands entered:

输入命令的结果:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rudyyuan/anaconda/lib/python2.7/site-  
packages/pandas/__init__.py", line 39, in <module>
    from pandas.core.api import *
  File "/Users/rudyyuan/anaconda/lib/python2.7/site-  packages/pandas/core/api.py", line 10, in <module>
from pandas.core.groupby import Grouper
  File "/Users/rudyyuan/anaconda/lib/python2.7/site-packages/pandas/core/groupby.py", line 18, in <module>
    from pandas.core.frame import DataFrame
  File "/Users/rudyyuan/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 39, in <module>
    from pandas.core.series import Series
  File "/Users/rudyyuan/anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 2944, in <module>
    import pandas.tools.plotting as _gfx  # noqa
  File "/Users/rudyyuan/anaconda/lib/python2.7/site-packages/pandas/tools/plotting.py", line 27, in <module>
    import pandas.tseries.converter as conv
  File "/Users/rudyyuan/anaconda/lib/python2.7/site-   packages/pandas/tseries/converter.py", line 7, in <module>
    import matplotlib.units as units
  File "/Users/rudyyuan/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py", line 1131, in <module>
    rcParams = rc_params()
  File "/Users/rudyyuan/anaconda/lib/python2.7/site-  
packages/matplotlib/__init__.py", line 975, in rc_params
    return rc_params_from_file(fname, fail_on_error)
  File "/Users/rudyyuan/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py", line 1100, in rc_params_from_file
    config_from_file = _rc_params_in_file(fname, fail_on_error)
  File "/Users/rudyyuan/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py", line 1018, in _rc_params_in_file
     with _open_file_or_url(fname) as fd:
  File "/Users/rudyyuan/anaconda/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/Users/rudyyuan/anaconda/lib/python2.7/site-   packages/matplotlib/__init__.py", line 1000, in _open_file_or_url
    encoding = locale.getdefaultlocale()[1]
  File "/Users/rudyyuan/anaconda/lib/python2.7/locale.py", line 545, in getdefaultlocale
    return _parse_localename(localename)
  File "/Users/rudyyuan/anaconda/lib/python2.7/locale.py", line 477, in   _parse_localename
    raise ValueError, 'unknown locale: %s' % localename
ValueError: unknown locale: UTF-8

But when I tried to check packages locally using this:

但是当我尝试使用这个在本地检查包时:

import pip
sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()])

it shown pandas already inside....:

它显示Pandas已经在里面了....:

['alabaster==0.7.8', 'anaconda-client==1.4.0', 'anaconda-navigator==1.2.1',    
'appnope==0.1.0', 'appscript==1.0.1', 'argcomplete==1.0.0', 'astropy==1.2.1',   
'babel==2.3.3', 'backports-abc==0.4', 'backports.shutil-get-terminal-
size==1.0.0', 'backports.ssl-match-hostname==3.4.0.2', 
'beautifulsoup4==4.4.1', 'bitarray==0.8.1', 'blaze==0.10.1', 'bokeh==0.12.0', 
'boto==2.40.0', 'bottleneck==1.1.0', 'bz2file==0.98', 'cdecimal==2.3', '      
cffi==1.6.0', 'chest==0.2.3', 'click==6.6', 'cloudpickle==0.2.1', 'clyent==1.2.2', 'colorama==0.3.7', 'conda-build==1.21.3', 'conda-env==2.5.0a0', 'conda==4.1.11', 'configobj==5.0.6', 'configparser==3.5.0b2', 'contextlib2==0.5.3', 'cryptography==1.4', 'cycler==0.10.0', 'cymem==1.31.2', 'cython==0.24', 'cytoolz==0.8.0', 'dask==0.10.0', 'datashape==0.5.2', 'decorator==4.0.10', 'dill==0.2.5', 'docutils==0.12', 'dynd==0.7.3.dev1', 'enum34==1.1.6', 'et-xmlfile==1.0.1', 'fastcache==1.0.2', 'flake8-docstrings==1.0.2', 'flake8==3.0.4', 'flask-cors==2.1.2', 'flask==0.11.1', 'funcsigs==1.0.2', 'functools32==3.2.3.post2', 'futures==3.0.5', 'gensim==0.12.4', 'gevent==1.1.1', 'greenlet==0.4.10', 'grin==1.2.1', 'h5py==2.6.0', 'heapdict==1.0.0', 'idna==2.1', 'imagesize==0.7.1', 'ipaddress==1.0.16', 'ipykernel==4.3.1', 'ipython-genutils==0.1.0', 'ipython==4.2.0', 'ipywidgets==4.1.1', 'itsdangerous==0.24', 'jdcal==1.2', 'jedi==0.9.0', 'jinja2==2.8', 'jsonschema==2.5.1', 'jupyter-client==4.3.0', 'jupyter-console==4.1.1', 'jupyter-core==4.1.0', 'jupyter==1.0.0', 'llvmlite==0.11.0', 'locket==0.2.0', 'lxml==3.6.0', 'markupsafe==0.23', 'matplotlib==1.5.1', 'mccabe==0.5.2', 'mistune==0.7.2', 'mpmath==0.19', 'multipledispatch==0.4.8', 'murmurhash==0.26.4', 'nb-anacondacloud==1.1.0', 'nb-conda-kernels==1.0.3', 'nb-conda==1.1.0', 'nbconvert==4.2.0', 'nbformat==4.0.1', 'nbpresent==3.0.2', 'networkx==1.11', 'nltk==3.2.1', 'nose==1.3.7', 'notebook==4.2.1', 'numba==0.26.0', 'numexpr==2.6.0', 'numpy==1.11.1', 'odo==0.5.0',  
`enter code here`'openpyxl==2.3.2', 'pandas==0.18.1', 'partd==0.3.4', 'path.py==0.0.0', 'pathlib2==2.1.0', 'patsy==0.4.1', 'pep8==1.7.0', 'pexpect==4.0.1', 'pickleshare==0.7.2', 'pillow==3.2.0', 'pip==8.1.2', 'plac==0.9.6', 'ply==3.8', 'preshed==0.46.4', 'psutil==4.3.0', 'ptyprocess==0.5.1', 'py==1.4.31', 'pyasn1==0.1.9', 'pyaudio==0.2.7', 'pycodestyle==2.0.0', 'pycosat==0.6.1', 'pycparser==2.14', 'pycrypto==2.6.1', 'pycurl==7.43.0', 'pydocstyle==1.0.0', 'pyflakes==1.2.3', 'pygments==2.1.3', 'pyopenssl==16.0.0', 'pyparsing==2.1.4', 'pytest==2.9.2', 'python-',

Whats the issue and how to fix this? Anything related with path or something?

这是什么问题以及如何解决这个问题?任何与路径有关的东西?

I'm very new to python.

我对python很陌生。

  1. Whats wrong with it?
  2. How can we fix it?
  1. 它出什么问题了?
  2. 我们该如何解决?

回答by ComputerFellow

Looks like you're on OS X. Append this to your ~/.bash_profile:

看起来您使用的是 OS X。将其附加到您的~/.bash_profile

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

And then run:

然后运行:

$ source ~/.bash_profile

Now try import pandas.

现在试试import pandas