Import error for icu in Mac and Ubuntu, although p

2019-06-01 09:20发布

问题:

I have pyicu installed in both MacOS and Ubuntu 14.04 but it shows ImportError upon importing. For MacOS high sierra output is:

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

and on ubuntu 14.0 this:

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

回答1:

I had the same experience when building and installing the pyicu from source in my Mac High Sierra.

The error message Symbol not found: __ZNK6icu_6114Transliterator12getTargetSetERNS_10UnicodeSetE is a sign of shared library mismatch between the ICU version we are using and the one actually used when building the package (Mac has built-in ICU library in /usr/library/libicucore.dylib - which i suspect is used as default during build process).

So, i did the following to get the pyicu up and running with the correct icu lib:

  1. Remove and reinstall icu4c using homebrew (brew remove icu4c and brew install icu4c)

  2. Create a icu-config symlink in standard path (ln -s /usr/local/Cellar/icu4c/61.1/bin/icu-config /usr/local/bin/icu-config)

  3. Clone the pyicu from repo, edit the setup.py file and fill out the entries for 'darwin' under INCLUDES, CFLAGS, LFLAGS, LIBRARIES sections as follows:

    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. Build and install the package, i.e. python3 setup.py build and python3 setup.py install

Note: If you previously had tried (unsuccessfully) build the package, make sure that you clean out the content of the build/ dir first before rebuilding, as the build process seems to skip the creation of new build files if it sees the directory populated with files from previous build.



回答2:

When installing pycu latest version on macOS (64.2) against python2.7 I did in setup.py as described above:

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'],
}

and for the build:

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

then I have got PyICU 2.3.1 installed:

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