python-3.ximporterrorpyicu

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


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

Solution

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