【问题标题】:undefined symbol: _PyThreadState_Current when importing tensorflow未定义符号:导入张量流时的_PyThreadState_Current
【发布时间】:2019-05-23 23:05:50
【问题描述】:

我在树莓派 3 上。运行 Raspbian 9。在 https://neoctobers.readthedocs.io/en/latest/rpi/install_python3.html 之后安装了 Python 版本 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

提前致谢。

【问题讨论】:

  • python3.7.3 也有同样的问题。

标签: python-3.x tensorflow raspberry-pi3


【解决方案1】:

Tensorflow Wheel 和 Python 3.7 (https://github.com/bennuttall/piwheels/issues/146) 存在一个已获批准的问题。看来没有办法轻轻解决。

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

$ 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)

【讨论】:

  • 是的,我面对这些警告并决定接受它。但后来 tflearn 遇到了问题。这就是我决定尝试 Python3.7 的原因。但是不久前,在神奇的第 10 次重新安装时,它起作用了。不知道为什么。你回答的我也做了!当我看到这个答案时,已经来删除这个问题了。谢谢您的帮助。将其标记为正确。
猜你喜欢
  • 1970-01-01
  • 2022-01-26
  • 2019-05-19
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
相关资源
最近更新 更多