无法安装 Tensorflow Mac

Can't install Tensorflow Mac

我检查了我的 pip3 & python3 版本:

  (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

在我目前使用的虚拟环境中我做了:

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

作为标准方式pip3 install tensorflow输出以下信息:

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

使用第一种方法安装后说明:

(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

这是什么问题?我可以下载其他软件包,例如 numpy,但不能下载 Tensorflow。 (MacOS 10.13.4)

更新:版本 1.13 引入了 Python 3.7 支持

1.13 版本的最新候选版本带来了 Python 3.7 支持,特别是预编译的 CPU wheels 也可用于 MacOS 10.11 和更高版本 (link to 1.13.1)。照常安装:

$ pip install tensorflow>=1.13

原始答案(已过时)

tensorflow 目前不支持 Python 3.7。这样做的原因是:

  • tensorflow使用async作为函数参数名,asyncawait在Python3.7中成为保留关键字(如由@phd 在 ) 中指出 - 这就是您收到导入错误的原因;

  • Python 3.7 更改了 return 函数在 C API 中的 return 类型 tensorflow:

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

这意味着必须先解决这两个问题,然后才能为 Python 3.7 和 Linux/MacOS 构建和发布 tensorflow。您可以在此处跟踪当前状态:issue #20517.

因此,如果您需要继续使用 tensorflow,解决方案将是避免使用 Python 3.7。暂时坚持 Python 3.6。

如果您愿意从源代码构建 tensorflow:来自官方文档的 There is a patch proposed to fix both issues. If you want to try it out, follow the Install TensorFlow from Sources 教程,唯一的区别在于开头:

  1. 克隆存储库

    $ git clone https://github.com/tensorflow/tensorflow
    
  2. 将补丁内容复制到文件中,例如tf.patch

  3. 应用补丁:

    $ git apply tf.patch
    
  4. 继续教程的其余部分。

另请注意,您必须构建最新的 protobuf,因为最近添加了对 Python 3.7 的支持,但未包含在任何已发布的版本中。编辑 tensorflow/contrib/cmake/external/protobuf.cmake 以指向 protobuf 存储库的当前 HEAD

我在 macOS Catalina (Python 3.6) 上试过这个命令,它 运行 正确:

$ python3 -m pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.12.0-py3-none-any.whl

试试这个觉得行得通

pip 安装 tensorflow-macos