Search code examples
pythontensorflowtensorflow2.0

Cannot import tensorflow_text


I have problem with importing tensorflow_text I tried importing like below two methods but none of them worked

import tensorflow_text as text
import tensorflow_text as tf_text

My tensorflow version is 2.9.1 and python version is Python 3.7.13. I tried installing tensorflow_text using below two methods but none of them is working.

!pip install tensorflow-text
!pip install -U tensorflow-text==2.9.0

I am using colab, I also tried reinstalling tensorflow but it still generates below error.

NotFoundError: 
/usr/local/lib/python3.7/distpackages/tensorflow_text/python/ops/_sentencepiece_tokenizer.so: 
undefined symbol: _ZNSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEC1Ev

Solution

  • Update, Sometimes you need to reinstall and update tensorflow then install tensorflow_text. (Because you need your tensorflow.__version__ and tensorflow_text.__version__ to have the same version)

    !pip install -U tensorflow
    !pip install -U tensorflow-text
    import tensorflow as tf
    import tensorflow_text as text
    
    # Or install with a specific Version
    !pip install -U "tensorflow==2.8.*"
    !pip install -U "tensorflow-text==2.8.*"
    import tensorflow as tf
    import tensorflow_text as text
    

    Old First, install tensorflow-text version 2.8.* like below:

    !pip install -q -U "tensorflow-text==2.8.*"
    

    Then import tensorflow-text like below:

    import tensorflow_text as text