无法安装Tensorflow Mac

时间:2018-07-14 11:16:22

标签: python macos tensorflow pip python-3.7

我检查了我的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)

2 个答案:

答案 0 :(得分:12)

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

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

$ pip install tensorflow>=1.13

原始答案(已过时)

tensorflow目前不支持Python 3.7。原因是:

  • tensorflow使用async作为函数参数名称,并且asyncawait成为Python 3.7中的保留关键字(如{{3中的@phd所指出的) }})-这就是为什么出现导入错误的原因;

  • Python 3.7更改了tensorflow使用的C API中this comment函数的返回类型:

      

    版本3.7中已更改:返回类型现在为const char *,而不是char *

这意味着必须为Python 3.7和Linux / MacOS构建并发布tensorflow之前必须解决两个问题。您可以在此处跟踪当前状态:PyUnicode_AsUTF8AndSize

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

如果您愿意从源issue #20517构建tensorflow。如果您想尝试一下,请遵循官方文档中的There is a patch proposed to fix both issues教程,唯一的区别是在开始时:

  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指向HEAD回购中的当前protobuf

答案 1 :(得分:1)

我在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