在导入tensorflow后出现的Python错误:TypeError:__init __()得到了一个意外的关键字参数'syntax'。

39

我按照仅支持CPU的指示,在我的Ubuntu 15.10机器上安装了TensorFlow:

$ pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl

然后当我运行Python REPL并导入tensorflow时,我得到:

$ python
Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/phil/.local/lib/python2.7/site-packages/tensorflow/__init__.py", line 4, in <module>
   from tensorflow.python import *
  File "/home/phil/.local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 13, in <module>
    from tensorflow.core.framework.graph_pb2 import *
  File "/home/phil/.local/lib/python2.7/site-packages/tensorflow/core/framework/graph_pb2.py", line 16, in <module>
    from tensorflow.core.framework import attr_value_pb2 as     tensorflow_dot_core_dot_framework_dot_attr__value__pb2
  File "/home/phil/.local/lib/python2.7/site-packages/tensorflow/core/framework/attr_value_pb2.py", line 16, in <module>
    from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__pb2
  File "/home/phil/.local/lib/python2.7/site-packages/tensorflow/core/framework/tensor_pb2.py", line 16, in <module>
    from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2
  File "/home/phil/.local/lib/python2.7/site-packages/tensorflow/core/framework/tensor_shape_pb2.py", line 22, in <module>
    serialized_pb=_b('\n,tensorflow/core/framework/tensor_shape.proto \x12\ntensorflow\"d\n\x10TensorShapeProto\x12-\n\x03\x64im\x18\x02 \x03(\x0b\x32 .tensorflow.TensorShapeProto.Dim\x1a!\n\x03\x44im\x12\x0c\n\x04size\x18\x01 \x01(\x03\x12\x0c\n\x04name\x18\x02 \x01(\tb\x06proto3')
TypeError: __init__() got an unexpected keyword argument 'syntax'

我已经安装了Ubuntu的protobuf-compiler软件包,它的版本是2.6.1-1.2

5个回答

46

一些用户报告安装旧版protobuf时出现问题。

TensorFlow需要(并使用)protobuf-3.0.0a4的副本。但是似乎与您安装的版本发生冲突(可能是由于系统上PYTHONPATH环境变量的配置方式引起的?)。如果已安装python-protobuf包,请尝试卸载它,然后查看是否可以正确启动TensorFlow?

有些用户在使用virtualenv时取得了成功。有关在virtualenv中安装TensorFlow的说明,请参见入门指南


5
好的,我也不得不执行“pip uninstall protobuf”的操作。 然后通过pip重新安装了tensorflow,重启了REPL,一切正常,谢谢。 - aneccodeal
10
在我的 Mac OS X 上,我还需要执行 'brew uninstall protobuf'。 - fdelia
9
我之前错误地使用了Brew安装了软件。我也需要运行brew uninstall protobuf来卸载它。 - Thamme Gowda

29

这个Github评论解决了我的问题。

简而言之,为了使TensorFlow工作,您需要安装protobuf>=3.0

首先卸载您当前拥有的任何protobuf版本,然后使用pip install 'protobuf>=3.0.0a3'来安装。

这不仅是Ubuntu的问题,MacOS也有类似的问题。


4
确认OSX存在相同的错误,因为大多数人安装的brew版本仍然是2.6.1。 - piggybox
这绝对可以在OS X 10.11.5上解决问题。pip install protobuf安装的是2.6.1版本,当然不能工作。 - carl_h

24

对我来说,这很有效:

pip uninstall protobuf
pip uninstall tensorflow
brew uninstall protobuf
pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl

import google.protobuf
>>> print google.protobuf.__version__
3.0.0a4
>>> import tensorflow

谢谢!尝试了几分钟后,终于成功了! - Meghna Natraj

5
我今天也遇到了类似的问题。这是因为我安装了截至2018年7月30日与tensorflow不兼容的python 3.7。 降级到3.6解决了这个问题。如果有人感兴趣,可以在这里查看我的解决方案。 在解决这个问题时,下面的github issue对我很有帮助:https://github.com/tensorflow/tensorflow/issues/17022

0

在运行 Python 2.7 版本时,我遇到了同样的错误。但是当我使用 Python 3 运行它时,一切都很顺利。

请在 Bash shell 上尝试以下命令:

python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux 
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>> exit()

这对我有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接