Mac 和 Ubuntu 中的 icu 导入错误,尽管 pyicu 已正确安装

Import error for icu in Mac and Ubuntu, although pyicu is installed correctly

我在 MacOS 和 Ubuntu 14.04 中都安装了 pyicu,但在导入时显示 ImportError。 对于 MacOS high sierra 输出是:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/Users/siddharthdas/venvs/chai/lib/python3.6/site-packages/icu/__init__.py", line 37, in <module>
  from _icu import *
ImportError: dlopen(/Users/siddharthdas/venvs/chai/lib/python3.6/site-packages/_icu.cpython-36m-darwin.so, 2): Symbol not found: __ZNK6icu_6114Transliterator12getTargetSetERNS_10UnicodeSetE
 Referenced from: /Users/siddharthdas/venvs/chai/lib/python3.6/site-packages/_icu.cpython-36m-darwin.so
 Expected in: flat namespace
in /Users/siddharthdas/venvs/chai/lib/python3.6/site-packages/_icu.cpython-36m-darwin.so

和 ubuntu 14.0 这个:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/home/hackathon/venvs/grey_worm/lib/python3.4/site-packages/icu/__init__.py", line 37, in <module>
  from _icu import *
ImportError: libicui18n.so.58: cannot open shared object file: No such file or directory

我在 Mac High Sierra 中从源代码构建和安装 pyicu 时也有同样的经历。

错误消息 Symbol not found: __ZNK6icu_6114Transliterator12getTargetSetERNS_10UnicodeSetE 表明我们使用的 ICU 版本与构建包时实际使用的 ICU 版本不匹配(Mac 在 /usr/library/libicucore.dylib - 我怀疑它在构建过程中被用作默认值)。

所以,我做了以下操作来启动 pyicu 和 运行 正确的 icu 库:

  1. 使用自制程序删除并重新安装 icu4c(brew remove icu4cbrew install icu4c

  2. 在标准路径中创建一个 icu-config 符号链接 (ln -s /usr/local/Cellar/icu4c/61.1/bin/icu-config /usr/local/bin/icu-config)

  3. 从 repo 克隆 pyicu,编辑 setup.py 文件并在 INCLUDESCFLAGS、[= 下填写 'darwin' 的条目20=],LIBRARIES栏目如下:

    INCLUDES = {
        'darwin': ['/usr/local/Cellar/icu4c/61.1/include']
    }
    
    CFLAGS = {
        'darwin': ['-DPYICU_VER="%s"' %(VERSION), '-std=c++11']
    }
    
    LFLAGS = {
        'darwin': ['-L/usr/local/Cellar/icu4c/61.1/lib']
    }
    
    LIBRARIES = {
        'darwin': ['/usr/local/Cellar/icu4c/61.1/lib']
    }
    
  4. 构建并安装包,即 python3 setup.py buildpython3 setup.py install

注意:如果您之前曾尝试(未成功)构建包,请确保在重建之前先清除 build/ 目录的内容,因为构建过程似乎跳过了新文件的创建如果看到目录中填充了以前构建的文件,则构建文件。

在针对 python2.7 在 macOS (64.2) 上安装 pycu 最新版本时,我在 setup.py 中执行了上述操作:

INCLUDES = {
    'darwin': ['/usr/local/Cellar/icu4c/64.2'],
    'linux': [],
    'freebsd': ['/usr/local/include'],
    'win32': ['c:/icu/include'],
    'sunos5': [],
    'cygwin': [],
}

CFLAGS = {
    'darwin': ['-DPYICU_VER="%s"' %(VERSION), '-std=c++11'],
    'linux': [],
    'freebsd': ['-std=c++11'],
    'win32': ['/Zc:wchar_t', '/EHsc'],
    'sunos5': ['-std=c++11'],
    'cygwin': ['-D_GNU_SOURCE=1', '-std=c++11'],
}

LFLAGS = {
    'darwin': ['-L/usr/local/Cellar/icu4c/64.2/lib'],
    'linux': [],
    'freebsd': ['-L/usr/local/lib'],
    'win32': ['/LIBPATH:c:/icu/lib'],
    'sunos5': [],
    'cygwin': [],
}

LIBRARIES = {
    'darwin': ['/usr/local/Cellar/icu4c/64.2/lib'],
    'linux': [],
    'freebsd': ['icui18n', 'icuuc', 'icudata'],
    'win32': ['icuin', 'icuuc', 'icudt'],
    'sunos5': ['icui18n', 'icuuc', 'icudata'],
    'cygwin': ['icui18n', 'icuuc', 'icudata'],
}

构建:

CFLAGS=-I/usr/local/opt/icu4c/include LDFLAGS=-L/usr/local/opt/icu4c/lib python setup.py build
python setup.py install

然后我安装了 PyICU 2.3.1

Installed /usr/local/lib/python2.7/site-packages/PyICU-2.3.1-py2.7-macosx-10.14-x86_64.egg
Processing dependencies for PyICU==2.3.1
Finished processing dependencies for PyICU==2.3.1