Search code examples
pythontensorflowpipmodulepython-import

How to solve Syntax error when importing tensorflow 1.8.0?


I imported tensorflow as: import tensorflow as tf

and got this error:

Traceback (most recent call last):
  File "C:\Users\USER\Downloads\import_tensorflow.py", line 1, in <module>
    import tensorflow as tf
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 114
    def TFE_ContextOptionsSetAsync(arg1, async):
                                         ^^^^^
SyntaxError: invalid syntax

reults of pip list and pip show shows that I have tensorflow installed on my computer...

pip list: tensorflow 1.8.0

pip show tensorflow:

Version: 1.8.0
Summary: TensorFlow helps the tensors flow
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: [email protected]
License: Apache 2.0 ```

Solution

  • This is a bug of an old version that used async as a variable name, which became a Python keyword on version 3.7.

    Just install the latest version of Tensorflow (currently 2.15.0.post1) and this should be fixed.