“ValueError: unknown locale: UTF-8” when importing

2019-03-09 04:21发布

问题:

This question already has an answer here:

  • IPython Notebook locale error [duplicate] 4 answers

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.

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:

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....:

['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.

  1. Whats wrong with it?
  2. How can we fix it?

回答1:

Looks like you're on OS X. Append this to your ~/.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.