Cannot import tensorflow_text

Question:

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
Asked By: albert

||

Answers:

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 
Answered By: I'mahdi

I have tried I’mahdi’s answer and it was not working, but the following solution worked for me.

Here are the steps that solved it for me.

  1. Visit the Tensorflow-Text official site (https://www.tensorflow.org/text)
  2. There is a little scroll bar down the right side RUN IN COLAB NOTEBBOK you will see, click on it (else use this link https://colab.research.google.com/github/tensorflow/text/blob/master/docs/guide/bert_preprocessing_guide.ipynb)
  3. In this notebook, change runtime to GPU and RUN cell by cell until the setup section
  4. And then it will work (WAIT)
  5. Delete all other cells except 1st 2 cells of Setup section
  6. Then continue your code…

NOTE: If it didn’t work try do the restart once again from step 1, it will work.

Answered By: Raj

Please use the latest TF version in colab with the below command –
!pip install tensorflow-text==2.13.0
Please restart the runtime post executing the command

Answered By: Nilanjan Dutta
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.