未定义的符号:_PyThreadState_Current 导入 tensorflow 时

undefined symbol: _PyThreadState_Current when importing tensorflow

我在 raspberry pi 3. 运行 Raspbian 9. Python 安装了 https://neoctobers.readthedocs.io/en/latest/rpi/install_python3.html 版本 3.7.2。使用 update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1

使 python3 指向 python3.7.2

这是我遇到的错误:

pi@raspberrypi:~ $ python3
Python 3.7.2 (default, May  5 2019, 18:41:29) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
Traceback (most recent call last):
  File "/usr/local/opt/python-3.7.2/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/local/opt/python-3.7.2/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/local/opt/python-3.7.2/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/local/opt/python-3.7.2/lib/python3.7/imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "/usr/local/opt/python-3.7.2/lib/python3.7/imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: /usr/local/opt/python-3.7.2/lib/python3.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: undefined symbol: _PyThreadState_Current

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/opt/python-3.7.2/lib/python3.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/usr/local/opt/python-3.7.2/lib/python3.7/site-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/opt/python-3.7.2/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/usr/local/opt/python-3.7.2/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/local/opt/python-3.7.2/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/local/opt/python-3.7.2/lib/python3.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/local/opt/python-3.7.2/lib/python3.7/imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "/usr/local/opt/python-3.7.2/lib/python3.7/imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: /usr/local/opt/python-3.7.2/lib/python3.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: undefined symbol: _PyThreadState_Current

提前致谢。

Tensorflow wheel 和 Python 3.7 (https://github.com/bennuttall/piwheels/issues/146) 存在已批准的问题。看来没办法温柔的解决了

我成功地在我的 RPi3 上安装了 tensorflow 1.13.0 Python 3.5.3:

$ python3 --version
Python 3.5.3

$ pip3 freeze | grep tensorflow
tensorflow==1.13.1
tensorflow-estimator==1.13.0

此设置有效,但 tensorflow 会引发与运行时版本兼容性相关的警告:

$ python3 -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime version 3.4 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.5
  return f(*args, **kwds)
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: builtins.type size changed, may indicate binary incompatibility. Expected 432, got 412
  return f(*args, **kwds)
tf.Tensor(624.73706, shape=(), dtype=float32)

您可以尝试忍受这些警告(抑制它们)或考虑从这个 repo https://github.com/lhelontra/tensorflow-on-arm/releases 为 RPi 构建替代的 tensorflow 构建(看起来它仅适用于 Python 3.5 和 2.7):

$ pip3 install https://github.com/lhelontra/tensorflow-on-arm/releases/download/v1.13.1/tensorflow-1.13.1-cp35-none-linux_armv7l.whl

在我的例子中,它工作正常,没有任何警告:

$ python3 -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
tf.Tensor(448.25854, shape=(), dtype=float32)