Probability: `ImportError: cannot import name 'abs'` when importing TFP in Python 3 (and in Python 2)

Created on 19 May 2018  ·  26Comments  ·  Source: tensorflow/probability

So I used the following code to import my dependencies:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import seaborn as sns
import tensorflow as tf                            # importing Tensorflow
import tensorflow_probability as tfp               # and Tensorflow probability
from tensorflow_probability import edward2 as ed   # Edwardlib extension

tfd = tfp.distributions             # Basic probability distribution toolkit
tfb = tfp.distributions.bijectors   # and their modifiers

# Eager Execution
# tfe = tf.contrib.eager
# tfe.enable_eager_execution()

%matplotlib inline
plt.style.use("fivethirtyeight")        # Styling plots like FiveThirtyEight

import warnings
warnings.filterwarnings('ignore')
%config InlineBackend.figure_format="retina" # improves resolution of plots

But I get this error when trying to import tensorflow_probability

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-3-47fdbecb20a4> in <module>()
      7 from matplotlib.patches import Ellipse
      8 import seaborn as sns
----> 9 import tensorflow as tf                            # importing Tensorflow
     10 import tensorflow_probability as tfp               # and Tensorflow probability
     11 from tensorflow_probability import edward2 as ed   # Edwardlib extension

/usr/local/lib/python3.6/dist-packages/tensorflow/__init__.py in <module>()
     22 
     23 # pylint: disable=g-bad-import-order
---> 24 from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
     25 # pylint: disable=wildcard-import
     26 from tensorflow.tools.api.generator.api import *  # pylint: disable=redefined-builtin

/usr/local/lib/python3.6/dist-packages/tensorflow/python/__init__.py in <module>()
     79 # Bring in subpackages.
     80 from tensorflow.python import data
---> 81 from tensorflow.python import keras
     82 from tensorflow.python.estimator import estimator_lib as estimator
     83 from tensorflow.python.feature_column import feature_column_lib as feature_column

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/__init__.py in <module>()
     22 from __future__ import print_function
     23 
---> 24 from tensorflow.python.keras import activations
     25 from tensorflow.python.keras import applications
     26 from tensorflow.python.keras import backend

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/activations/__init__.py in <module>()
     20 
     21 # Activation functions.
---> 22 from tensorflow.python.keras._impl.keras.activations import elu
     23 from tensorflow.python.keras._impl.keras.activations import hard_sigmoid
     24 from tensorflow.python.keras._impl.keras.activations import linear

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/_impl/keras/__init__.py in <module>()
     19 from __future__ import print_function
     20 
---> 21 from tensorflow.python.keras._impl.keras import activations
     22 from tensorflow.python.keras._impl.keras import applications
     23 from tensorflow.python.keras._impl.keras import backend

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/_impl/keras/activations.py in <module>()
     21 import six
     22 
---> 23 from tensorflow.python.keras._impl.keras import backend as K
     24 from tensorflow.python.keras._impl.keras.utils.generic_utils import deserialize_keras_object
     25 from tensorflow.python.layers.base import Layer

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/_impl/keras/backend.py in <module>()
     35 from tensorflow.python.framework import ops
     36 from tensorflow.python.framework import sparse_tensor
---> 37 from tensorflow.python.layers import base as tf_base_layers
     38 from tensorflow.python.ops import array_ops
     39 from tensorflow.python.ops import clip_ops

/usr/local/lib/python3.6/dist-packages/tensorflow/python/layers/base.py in <module>()
     23 from tensorflow.python.framework import dtypes
     24 from tensorflow.python.framework import ops
---> 25 from tensorflow.python.keras.engine import base_layer
     26 from tensorflow.python.ops import variable_scope as vs
     27 from tensorflow.python.ops import variables as tf_variables

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/__init__.py in <module>()
     19 from __future__ import print_function
     20 
---> 21 from tensorflow.python.keras.engine.base_layer import InputSpec
     22 from tensorflow.python.keras.engine.base_layer import Layer
     23 from tensorflow.python.keras.engine.input_layer import Input

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in <module>()
     30 from tensorflow.python.framework import tensor_shape
     31 from tensorflow.python.framework import tensor_util
---> 32 from tensorflow.python.keras import backend
     33 from tensorflow.python.keras import constraints
     34 from tensorflow.python.keras import initializers

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend/__init__.py in <module>()
     20 
     21 # pylint: disable=redefined-builtin
---> 22 from tensorflow.python.keras._impl.keras.backend import abs
     23 from tensorflow.python.keras._impl.keras.backend import all
     24 from tensorflow.python.keras._impl.keras.backend import any

ImportError: cannot import name 'abs'

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

This only just started happening after I entered pip install --upgrade tfp-nightly for into the terminal, which leads me to conclude that something within the combination of tb-nightly-1.9.0a20180519, tf-nightly-1.9.0.dev20180519, and tfp-nightly-0.0.1.dev20180519 is not working.

It was working previously but is not any longer. This error applies to both Python 3 AND python 2, so it's not as simple as just fixing the runtime type.

Most helpful comment

I've dealt with this problem (in conda env on Ubuntu 18.04):

pip uninstall tensorflow protobuf --yes
find $CONDA_PREFIX -name "tensorflow" | xargs -Ipkg rm -rfv pkg
pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.9.0-cp36-cp36m-linux_x86_64.whl --no-cache-dir

All 26 comments

This appeared to be fixable with the following steps:

  1. uninstalling tensorflow
  2. uninstalling protobuf
  3. reinstalling tensorlfow (which should come along with the correct protobuf version.

which protobuf version is supported? thanks!

I suspect this is not fixed by the protobuf version but rather by the reinstall of tensorflow which installs 1.8.0 I assume. I built 1.9.0 from source and see the same error.

~What I can see is that the _impl folder is missing under the site-packages folder of the keras package included in tensorflow. hence, the resolution fails.~

But it seems to have something to do with this commit

manually deleting the tensorflow folder inside the virtual environment and reinstalling the locally installed variant helped. I assume it has something to do with __pycache__ files.

hi, I got the same problem with my program, but neither reinstalling tf nor deleting the folder worked.
I have problems with installing via web, so I download the wheel files of the modules and use pip install locally.
I'm trying to run tf 1.9.0 and I'm trying with all the latest releases of the submodules.
any hints on what to try?

I've dealt with this problem (in conda env on Ubuntu 18.04):

pip uninstall tensorflow protobuf --yes
find $CONDA_PREFIX -name "tensorflow" | xargs -Ipkg rm -rfv pkg
pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.9.0-cp36-cp36m-linux_x86_64.whl --no-cache-dir

thank you very much

FWIW if you're using a local install (pip install --user) rather than conda you can replace the second command with this:
find ~/.local/ -name "tensorflow" | xargs -Ipkg rm -rfv pkg

Thank you!

I also encounter this problem. I have installed tensorflow 1.8.0 before and I just pip install tensorflow-gpu==1.9.0.
Then import tensorflow as tf, I saw this error.
So I uninstall tensorflow 1.8.0 and reinstall tensorflow-gpu 1.9.0.
And the error is gone! The problem is solved.

Same problem. Uninstalling & reinstalling did not help :(

Hi,
I am using tensorflow 1.10.0
I agree with matthew-mcateer, It worked on my machine by uninstalling tensorflow and protobuf and reinstalling tensorflow.

I got the problem too. Tensorflow 1.8.0 was installed as sudo and Tensorflow 1.10.0 installed as user triggered the problem.

sudo pip3 uninstall tensorflow
pip3 install tensorflow

solved it.

@rdinse good point -- the original solution also has this problem (except for conda). I guess if you have more important things in .local then you should rm with more care :)

@rodrigob did you found solution?
I have same error (windows 10, anaconda) at from keras import backend as K

  File "\Anaconda3\envs\art\lib\site-packages\tensorflow\python\keras\engine\__init__.py", line 21, in <module>
    from tensorflow.python.keras.engine.base_layer import InputSpec
  File "\Anaconda3\envs\art\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 33, in <module>
    from tensorflow.python.keras import backend
  File "\Anaconda3\envs\art\lib\site-packages\tensorflow\python\keras\backend\__init__.py", line 22, in <module>
    from tensorflow.python.keras._impl.keras.backend import abs
ImportError: cannot import name 'abs'

Hey Dok11 , im getting the same error, please ping me if you got any solution for that.
https://www.linkedin.com/in/tarun-yellogi-51b41466/

@Tarunyellogi I installed 1.8 instead 1.9 and its worked for me

I have the same issue that Dok11 and Tarunyellogi

Deleting directories of older tensorflow installations (after uninstalling tf) and then installing tf-1.10.0 worked for me.

Hi,
1-) conda uninstall tensorflow-gpu and conda uninstall protobuf,
2-) Deleting directories of older tensorflow installations,
3-) conda install tensorflow-gpu==1.8
worked for me.

but, conda install tensorflow-gpu==1.10 not worked for me.

The problem is that you updated the tensorflow but not tensorflow_gpu.

pip install --upgrade tensorflow
pip install --upgrade tensorflow-gpu

Best wishes.

The answer from AramNaseer solved this . But when I upgraded the tensofflow and tensorflow-gpu got this error - tensorflow cannot load libcublas.so.9 (I had cuda 8 installed, now need to upgrade it and hope everything else works)

"As our installation pages list, prebuilt TF pip packages on pypi are built with CUDA 9.0 support only:
https://www.tensorflow.org/install/gpu"

https://github.com/tensorflow/tensorflow/issues/20569#issuecomment-423887354

Deleting the virtualenv which stored caches of tensorflow installation did the trick. Making a new env, and then installing tensorflow again likely installed the packages afresh.

Hello everyone,
Please make a new environment. I was struggling the whole day with this problem. I was about to quit but for the last time I decided to create a new environment and it worked.

how to create a new environment

I got the problem too. Tensorflow 1.8.0 was installed as sudo and Tensorflow 1.10.0 installed as user triggered the problem.

sudo pip3 uninstall tensorflow
pip3 install tensorflow

solved it.

worked for me

This appeared to be fixable with the following steps:

  1. uninstalling tensorflow
  2. uninstalling protobuf
  3. reinstalling tensorlfow (which should come along with the correct protobuf version.

worked for me

  1. pip uninstall tensorflow
  2. pip uninstall protobuf
  3. pip install tensorflow==1.4.0
Was this page helpful?
0 / 5 - 0 ratings