Search code examples
python-3.xmacospolyglotpyicuicu4c

Import or Symbol not Found Error with polyglot when PyICU and icu4c installed correctly


I'm really struggling with polyglot, pyicu and icu4c - and it seems like I'm not the only one...

I'm running Mac OS High Sierra 10.13.6 and python3 with a virtualenv

Here are the steps I've taken, largely based on this article:

  1. Start from a new virtualenv
  2. Remove and re-install icu4c (v.62.1) with homebrew
  3. Check ICU_VERSION is "62.1"
  4. Do ln -s /usr/local/Cellar/icu4c/62.1/bin/icu-config /usr/local/bin/icu-config
  5. Download the pyicu tar file (2.1)
  6. Extract pyicu and edit setup.py as indicated in the linked article
  7. build and install pyicu

I see:

    Adding CXXFLAGS="-std=c++11 -I/usr/local/Cellar/icu4c/62.1/include" from /usr/local/bin/icu-config
    Adding LDFLAGS="-L/usr/local/Cellar/icu4c/62.1/lib -licui18n -licuuc -licudata" from /usr/local/bin/icu-config                    
    Installed/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyICU-2.1-py3.6-macosx-10.6-intel.egg
    ...
    Processing dependencies for PyICU==2.1
    Finished processing dependencies for PyICU==2.1

as expected.

  1. pip3 install polyglot

when I run my script I get the following:

File "/Users/Chris/MindWeb/Server/lib/python3.6/site-packages/polyglot/detect/__init__.py", line 1, in <module>
from .base import Detector, Language
File "/Users/Chris/MindWeb/Server/lib/python3.6/site-packages/polyglot/detect/base.py", line 11, in <module>
from icu import Locale
ModuleNotFoundError: No module named 'icu'

Unsurprising because there isn't actually an icu module in PATH.

If, instead of steps 5-7 I do pip3 install pyicu (which creates the icu folder), I get:

ImportError: dlopen(/Users/Chris/MindWeb/Server/lib/python3.6/site-packages/_icu.cpython-36m-darwin.so, 2): Symbol not found: __ZNK6icu_6214Transliterator12getTargetSetERNS_10UnicodeSetE
Referenced from: /Users/Chris/MindWeb/Server/lib/python3.6/site-packages/_icu.cpython-36m-darwin.so
Expected in: flat namespace
in /Users/Chris/MindWeb/Server/lib/python3.6/site-packages/_icu.cpython-36m-darwin.so

output from otool -L /Users/Chris/MindWeb/Server/lib/python3.6/site-packages/_icu.cpython-36m-darwin.so is:

/Users/Chris/MindWeb/Server/lib/python3.6/site-packages/_icu.cpython-36m-darwin.so:
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 104.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)

Solution

  • This method actually does work, the problem I was experiencing was related to the fact that the scratch install of pyICU was located outside of my python virtualenv and therefore not in $PATH.

    1. run all the build commands (whether pip or setup.py from the linked article) from inside your virtual environment

    2. run echo $PATH from within your python virtual environment, and ensure that the pyICU folder is located correctly.