来自源主服务器的Tensorflow会从导入中产生语法错误(Tensorflow from source master gives a syntax error from import)

我尝试从源代码(master)编译tensorflow,并遵循使用python3而不是python2的张量传递指令 (并且pip默认为pip3 )。 在测试第一个模型之前,我可以完成所有步骤。

我注意到,通过做一个简单的import tensorflow可以最小程度地复制问题。 错误如下:

$ python3 Python 3.5.2 (default, Jul 5 2016, 12:43:10) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import tensorflow I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.so locally I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcudnn.so locally I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcufft.so locally I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcuda.so.1 locally I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcurand.so locally Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.5/dist-packages/tensorflow/__init__.py", line 23, in <module> from tensorflow.python import * File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/__init__.py", line 65, in <module> import tensorflow.contrib as contrib File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/__init__.py", line 30, in <module> from tensorflow.contrib import learn File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/__init__.py", line 72, in <module> from tensorflow.contrib.learn.python.learn import * File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/__init__.py", line 23, in <module> from tensorflow.contrib.learn.python.learn import * File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/__init__.py", line 26, in <module> from tensorflow.contrib.learn.python.learn import estimators File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/__init__.py", line 23, in <module> from tensorflow.contrib.learn.python.learn.estimators.autoencoder import TensorFlowDNNAutoencoder File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/autoencoder.py", line 25, in <module> from tensorflow.contrib.learn.python.learn.estimators.base import TensorFlowBaseTransformer File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/base.py", line 34, in <module> from tensorflow.contrib.learn.python.learn.estimators import estimator File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 39, in <module> from tensorflow.contrib.learn.python.learn.learn_io import data_feeder File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/learn_io/__init__.py", line 22, in <module> from tensorflow.contrib.learn.python.learn.learn_io.dask_io import extract_dask_data File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/learn_io/dask_io.py", line 26, in <module> import dask.dataframe as dd File "/usr/local/lib/python3.5/dist-packages/dask/__init__.py", line 7, in <module> from .delayed import do, delayed, value File "/usr/local/lib/python3.5/dist-packages/dask/delayed.py", line 12, in <module> from . import base File "/usr/local/lib/python3.5/dist-packages/dask/base.py", line 287, in <module> import pandas as pd File "/usr/local/lib/python3.5/dist-packages/pandas/__init__.py", line 22, in <module> from pandas.compat.numpy import * File "/usr/local/lib/python3.5/dist-packages/pandas/compat/__init__.py", line 350, in <module> from dateutil import parser as _date_parser File "/usr/local/lib/python3.5/dist-packages/dateutil/parser.py", line 158 l.append("%s=%s" % (attr, `value`)) ^ SyntaxError: invalid syntax

与类似语法问题相关的其他问题与旧版本的protobuf ,但我的版本是3

$ pip3 show protobuf --- Metadata-Version: 2.0 Name: protobuf Version: 3.0.0b2 Summary: Protocol Buffers Home-page: https://developers.google.com/protocol-buffers/ Author: protobuf@googlegroups.com Author-email: protobuf@googlegroups.com Installer: pip License: New BSD License Location: /usr/local/lib/python3.5/dist-packages Requires: six, setuptools Classifiers: Programming Language :: Python Programming Language :: Python :: 2 Programming Language :: Python :: 2.6 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 Programming Language :: Python :: 3.3 Programming Language :: Python :: 3.4

我怎么解决这个问题?

I tried compiling tensorflow from sources (master) and follow the tensorflow instructions using python3 instead of python2 (and pip defaults to pip3). I can achieve all steps until testing the first model.

I noticed that the problem can be minimally reproduced by doing a simple import tensorflow. The error is as follows:

$ python3 Python 3.5.2 (default, Jul 5 2016, 12:43:10) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import tensorflow I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.so locally I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcudnn.so locally I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcufft.so locally I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcuda.so.1 locally I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcurand.so locally Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.5/dist-packages/tensorflow/__init__.py", line 23, in <module> from tensorflow.python import * File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/__init__.py", line 65, in <module> import tensorflow.contrib as contrib File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/__init__.py", line 30, in <module> from tensorflow.contrib import learn File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/__init__.py", line 72, in <module> from tensorflow.contrib.learn.python.learn import * File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/__init__.py", line 23, in <module> from tensorflow.contrib.learn.python.learn import * File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/__init__.py", line 26, in <module> from tensorflow.contrib.learn.python.learn import estimators File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/__init__.py", line 23, in <module> from tensorflow.contrib.learn.python.learn.estimators.autoencoder import TensorFlowDNNAutoencoder File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/autoencoder.py", line 25, in <module> from tensorflow.contrib.learn.python.learn.estimators.base import TensorFlowBaseTransformer File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/base.py", line 34, in <module> from tensorflow.contrib.learn.python.learn.estimators import estimator File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 39, in <module> from tensorflow.contrib.learn.python.learn.learn_io import data_feeder File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/learn_io/__init__.py", line 22, in <module> from tensorflow.contrib.learn.python.learn.learn_io.dask_io import extract_dask_data File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/learn_io/dask_io.py", line 26, in <module> import dask.dataframe as dd File "/usr/local/lib/python3.5/dist-packages/dask/__init__.py", line 7, in <module> from .delayed import do, delayed, value File "/usr/local/lib/python3.5/dist-packages/dask/delayed.py", line 12, in <module> from . import base File "/usr/local/lib/python3.5/dist-packages/dask/base.py", line 287, in <module> import pandas as pd File "/usr/local/lib/python3.5/dist-packages/pandas/__init__.py", line 22, in <module> from pandas.compat.numpy import * File "/usr/local/lib/python3.5/dist-packages/pandas/compat/__init__.py", line 350, in <module> from dateutil import parser as _date_parser File "/usr/local/lib/python3.5/dist-packages/dateutil/parser.py", line 158 l.append("%s=%s" % (attr, `value`)) ^ SyntaxError: invalid syntax

Other questions related to similar syntax problems are related to older versions of protobuf, however my version is 3

$ pip3 show protobuf --- Metadata-Version: 2.0 Name: protobuf Version: 3.0.0b2 Summary: Protocol Buffers Home-page: https://developers.google.com/protocol-buffers/ Author: protobuf@googlegroups.com Author-email: protobuf@googlegroups.com Installer: pip License: New BSD License Location: /usr/local/lib/python3.5/dist-packages Requires: six, setuptools Classifiers: Programming Language :: Python Programming Language :: Python :: 2 Programming Language :: Python :: 2.6 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 Programming Language :: Python :: 3.3 Programming Language :: Python :: 3.4

How can I solve this problem?

最满意答案

它看起来像已安装的dateutil包(TensorFlow依赖于dask和pandas )与Python 3不兼容,使用反引号作为repr()的同义词,在3.0中删除了它。 2011年为Python 3更新了发生错误的dateutil/parser.py中的特定行。

尝试运行以下命令来升级dateutil :

$ pip3 install python-dateutil --upgrade

It looks like the installed dateutil package (which TensorFlow depends on via dask and pandas) is incompatible with Python 3, using backticks as a synonym for repr(), which was removed in 3.0. The particular line from dateutil/parser.py where the error occurs was updated for Python 3 in 2011.

Try running the following command to upgrade dateutil:

$ pip3 install python-dateutil --upgrade

更多推荐