Search code examples
pythonmacostensorflowpippython-3.7

Importing TensorFlow fails with a SyntaxError, complaining about a parameter called "async"


I checked my pip3 & python3 version:

  (tensorflow) MacBook-Pro-de-Hector-2:tensorflow hectoresteban$ pip3 -V
    pip 10.0.1 from /Users/hectoresteban/.virtualenvs/tensorflow/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip (python 3.7)

(tensorflow) MacBook-Pro-de-Hector-2:tensorflow hectoresteban$ python3 -V
Python 3.7.0

In the virtual environment I am currently using I did:

pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.9.0-py3-none-any.whl

As the standard way pip3 install tensorflow output the following message:

could not find a version that satisfies the requirement tensorflow (from versions: )

After installed using the first method explained:

(tensorflow) MacBook-Pro-de-Hector-2:tensorflow hectoresteban$ python3
>>> import tensorflow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/hectoresteban/.virtualenvs/tensorflow/lib/python3.7/site-packages/tensorflow/__init__.py", line 22, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/Users/hectoresteban/.virtualenvs/tensorflow/lib/python3.7/site-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/Users/hectoresteban/.virtualenvs/tensorflow/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/Users/hectoresteban/.virtualenvs/tensorflow/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 114
    def TFE_ContextOptionsSetAsync(arg1, async):
                                             ^
SyntaxError: invalid syntax

What is the issue? I can download other packages such as numpy but no Tensorflow. (MacOS 10.13.4)


Solution

  • Update: version 1.13 introduces Python 3.7 support

    The recent release candidate for the 1.13 version brings Python 3.7 support, in particular precompiled CPU wheels are also available for MacOS 10.11 and newer (link to 1.13.1). Install as usual:

    $ pip install tensorflow>=1.13
    

    Original answer (outdated)

    tensorflow does not support Python 3.7 at the moment. The reason for this is that:

    • tensorflow uses async as function parameter name, and async and await became reserved keywords in Python 3.7 (as pointed by @phd in this comment) - this is why you're getting the import error;

    • Python 3.7 changed the return type of PyUnicode_AsUTF8AndSize function in the C API used by tensorflow:

      Changed in version 3.7: The return type is now const char * rather of char *.

    This means both issues must be fixed before tensorflow can be built and released for Python 3.7 & Linux/MacOS. You can track the current status here: issue #20517.

    The solution hence would be avoiding Python 3.7 if you need to continue working with tensorflow. Stick with Python 3.6 for the time being.

    If you are willing to build tensorflow from source: There is a patch proposed to fix both issues. If you want to try it out, follow the Install TensorFlow from Sources tutorial from the official docs, the only difference being on the beginning:

    1. Clone the repository

      $ git clone https://github.com/tensorflow/tensorflow
      
    2. Copy the patch contents to a file, e.g. tf.patch

    3. Apply the patch:

      $ git apply tf.patch
      
    4. Proceed with the rest of the tutorial.

    Also note that you will have to build the latest protobuf, as the support for Python 3.7 was added to it lately, but is not contained in any released version. Edit tensorflow/contrib/cmake/external/protobuf.cmake to point to the current HEAD of the protobuf repo.