Tensorflow cannot be imported on ubuntu

Viewed 87

System information

  • Linux Ubuntu 16.04
  • TensorFlow version: tensorflow-gpu 1.5
  • Python version: 2.7
  • Installed using virtualenv: pip
  • CUDA/cuDNN version: CUDA9.0 & CUDNN7.0

Enviroment

  • Using anaconda to create a virtual environment named tf.

Error Msg

(tf) ➜  ~ python                         
Python 2.7.18 |Anaconda, Inc.| (default, Jun  4 2021, 14:47:46) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xander/anaconda3/envs/tf/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/home/xander/anaconda3/envs/tf/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 52, in <module>
    from tensorflow.core.framework.graph_pb2 import *
  File "/home/xander/anaconda3/envs/tf/lib/python2.7/site-packages/tensorflow/core/framework/graph_pb2.py", line 6, in <module>
    from google.protobuf import descriptor as _descriptor
  File "/home/xander/anaconda3/envs/tf/lib/python2.7/site-packages/google/protobuf/descriptor.py", line 113
    class DescriptorBase(metaclass=DescriptorMetaclass):
                                  ^
SyntaxError: invalid syntax
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xander/anaconda3/envs/tf/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/home/xander/anaconda3/envs/tf/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
ImportError: cannot import name pywrap_tensorflow
2 Answers

As mentioned in this link, You're running the script in the same directory as TensorFlow. Try to move it out and you should be fine.

For example, Open the Linux command line and execute the script that you've just copied to desktop

Restarting the conda (using miniconda + conda-forge channel) works for me. But I don't know why, though...

Related