python - 无法安装 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 轮子也可用于 MacOS 10.11 和更高版本(链接到 1.13.1)。照常安装:
$ pip install tensorflow>=1.13
原始答案(已过时)
tensorflow
目前不支持 Python 3.7。这样做的原因是:
tensorflow
用作async
函数参数名称,async
并await
成为 Python 3.7 中的保留关键字(如 @phd 在此评论中指出的那样) - 这就是您收到导入错误的原因;Python 3.7 更改了
PyUnicode_AsUTF8AndSize
C API 中使用的函数的返回类型tensorflow
:在 3.7 版更改: 返回类型现在
const char *
是char *
.
这意味着必须先修复这两个问题,然后tensorflow
才能为 Python 3.7 和 Linux/MacOS 构建和发布。您可以在此处跟踪当前状态:问题 #20517。
因此,如果您需要继续使用tensorflow
. 暂时坚持使用 Python 3.6。
如果您愿意tensorflow
从源代码构建:有一个补丁可以解决这两个问题。如果您想尝试一下,请按照官方文档中的从 Sources 安装 TensorFlow教程进行操作,唯一的区别在于开头:
克隆存储库
$ git clone https://github.com/tensorflow/tensorflow
将补丁内容复制到文件中,例如
tf.patch
应用补丁:
$ git apply tf.patch
继续本教程的其余部分。
另请注意,您必须构建 latest protobuf
,因为最近添加了对 Python 3.7 的支持,但未包含在任何已发布的版本中。编辑tensorflow/contrib/cmake/external/protobuf.cmake
以指向当前HEAD
的protobuf
repo。