Python locale error: unsupported locale setting


在Linux安装python3.5.2时遇到了此问题,以下是我的编译安装命令

./configure --prefix=/usr/local/python3.5.2
make
make install

./configure步骤没问题,make步骤没有问题,在make intall步骤出现如下报错:

Traceback (most recent call last):
  File "/home/huichaozh/download/Python-3.5.2/Lib/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/huichaozh/download/Python-3.5.2/Lib/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/huichaozh/download/Python-3.5.2/Lib/ensurepip/__main__.py", line 4, in <module>
    ensurepip._main()
  File "/home/huichaozh/download/Python-3.5.2/Lib/ensurepip/__init__.py", line 209, in _main
    default_pip=args.default_pip,
  File "/home/huichaozh/download/Python-3.5.2/Lib/ensurepip/__init__.py", line 116, in bootstrap
    _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/home/huichaozh/download/Python-3.5.2/Lib/ensurepip/__init__.py", line 41, in _run_pip
    pip.main(args)
  File "/tmp/tmp_sfys7rm/pip-8.1.1-py2.py3-none-any.whl/pip/__init__.py", line 215, in main
  File "/home/huichaozh/download/Python-3.5.2/Lib/locale.py", line 594, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting
make: *** [install] Error 1

以上报错信息中,最后两句,表明是python在执行”_setlocale(category, locale)”时,出现了错误”locale.Error: unsupported locale setting”;
而是谁在执行”_setlocale(category, locale)”呢,再往上看到”ensurepip”相关代码,可以猜测其实python已经安装好了,在安装pip时,遇到了以上的报错信息;
通过google搜索”Python locale error: unsupported locale setting”也能够看到很多关于此错误的讲解,但google到的结果里大部分记录的是在执行pip命令或者使用locale库时遇到的此问题;

到了目前为止,问题出在了locale上,执行locale -a, 我的机器会报如下错误”locale: Cannot set LC_CTYPE to default locale: No such file or directory”, 具体如下:

# locale -a | more
locale: Cannot set LC_CTYPE to default locale: No such file or directory
aa_DJ
aa_DJ.iso88591
aa_DJ.utf8
...
...

解决办法:编辑~/.bashrc文件,在文件末尾加入”export LC_ALL=C”, 执行source ~/.bashrc
之后,再执行make install,如下为安装成功的结尾信息:

...
Ignoring indexes: https://pypi.python.org/simple
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-8.1.1 setuptools-20.10.1

也印证了之前的猜测,在安装完python后,会默认把pip安装进去。