Skip to main content
 首页 » 编程设计

python之通过 pyenv 将 Python 构建为 UCS-4

2025年05月04日14hnrainll

我遇到了这个问题 ImportError numpy/core/multiarray.so: undefined symbol: PyUnicodeUCS2_AsASCIIString 在 pyenv-virtualenv 环境中安装 Python。

在我的例子中,它发生在 matplotlib 包而不是 numpy (如上述问题),但它基本上是同一个问题。

这个问题的答案很简单:

Rebuild NumPy against a Python built as UCS-4.

我不知道该怎么做。在 this other question 中,据说必须使用:

./configure --enable-unicode=ucs4 

但我不知道如何将该命令与 pyenv 一起使用。

pyenv 的 repo issue list 中也提到了这个问题,并在 a comment 中给出了解决方案。可悲的是(对我来说)我无法理解如何应用上述评论中解释的修复。

所以我的问题基本上是:如何通过 pyenv 将 Python 构建为 UCS-4?

请您参考如下方法:

使用 ucs2 使用 pyenv 安装 python:

$ export PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs2 
$ pyenv install -v 2.7.11 
  ... 
$ pyenv local 2.7.11 
$ pyenv versions 
  system 
* 2.7.11 (set by /home/nwani/.python-version) 
$ /home/nwani/.pyenv/shims/python 
Python 2.7.11 (default, Aug 13 2016, 13:42:13)  
[GCC 4.8.4] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import sysconfig 
>>> sysconfig.get_config_vars()['CONFIG_ARGS'] 
"'--prefix=/home/nwani/.pyenv/versions/2.7.11' '--enable-unicode=ucs2' '--libdir=/home/nwani/.pyenv/versions/2.7.11/lib' 'LDFLAGS=-L/home/nwani/.pyenv/versions/2.7.11/lib ' 'CPPFLAGS=-I/home/nwani/.pyenv/versions/2.7.11/include '" 

使用 ucs4 使用 pyenv 安装 python:

$ pyenv uninstall 2.7.11 
pyenv: remove /home/nwani/.pyenv/versions/2.7.11? y 
$ export PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs4 
$ pyenv install -v 2.7.11 
... 
$ pyenv local 2.7.11 
$ pyenv versions 
  system 
* 2.7.11 (set by /home/nwani/.python-version) 
$ /home/nwani/.pyenv/shims/python 
Python 2.7.11 (default, Aug 13 2016, 13:49:09)  
[GCC 4.8.4] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import sysconfig 
>>> sysconfig.get_config_vars()['CONFIG_ARGS'] 
"'--prefix=/home/nwani/.pyenv/versions/2.7.11' '--enable-unicode=ucs4' '--libdir=/home/nwani/.pyenv/versions/2.7.11/lib' 'LDFLAGS=-L/home/nwani/.pyenv/versions/2.7.11/lib ' 'CPPFLAGS=-I/home/nwani/.pyenv/versions/2.7.11/include '"