从C ++中的嵌入式python导入tensorflow:无法加载本机TensorFlow运行时

时间:2018-07-05 07:19:06

标签: python c++ tensorflow

我希望C ++代码中的嵌入式python导入tensorflow,但是“ import tensorflow”行在运行时给出了错误。导入其他python模块时,这没有发生。从我的python shell /脚本中使用tensorflow没问题,我还可以编译和使用C ++编写的自定义操作,从而证明安装工作没有问题。我不知道它是否相关,但是我的C ++库已经使用tensorflow作为第三方共享库。

在stackoverflow和GitHub上,我发现了具有类似错误的问题,但是它们发生在完全不同的环境中。 Importing tensorflow when embedding python in c++ returns null在类似的情况下存在另一个问题。我已经尝试了建议的解决方案,但是没有任何变化。 我认为许多程序员可能会对从c ++调用python函数以在运行时计算tensorflow图感兴趣,以便将这些图保存为protobuf并在c ++中运行它们。如果不是这种情况,我仍然希望从stickedoverflow中获得帮助。

这是我非常简单的调用python的C ++代码:

    PyObject* pInt;

    Py_Initialize();

    PyRun_SimpleString("import tensorflow \n"
                       "print('Hello TF!!!')");

    Py_Finalize();

这是我在运行时遇到的错误:

    Traceback (most recent call last):
      File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
      File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
      File "/usr/local/lib/python3.5/dist-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/lib/python3.5/imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
      File "/usr/lib/python3.5/imp.py", line 342, in load_dynamic
return _load(spec)
    ImportError: /usr/local/lib/python3.5/dist-packages/tensorflow/python/_pywrap_tensorflow_internal.so: undefined symbol: _ZNK6google8protobuf7Message11GetTypeNameEv

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/local/lib/python3.5/dist-packages/tensorflow/__init__.py", line 24, in <module>
from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
      File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
      File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 74, in <module>
raise ImportError(msg)
    ImportError: Traceback (most recent call last):
      File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
      File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
      File "/usr/local/lib/python3.5/dist-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/lib/python3.5/imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
      File "/usr/lib/python3.5/imp.py", line 342, in load_dynamic
return _load(spec)
    ImportError: /usr/local/lib/python3.5/dist-packages/tensorflow/python/_pywrap_tensorflow_internal.so: undefined symbol: _ZNK6google8protobuf7Message11GetTypeNameEv


    Failed to load the native TensorFlow runtime.

    See https://www.tensorflow.org/install/install_sources#common_installation_problems

    for some common reasons and solutions.  Include the entire stack trace

在寻求帮助时,出现此错误消息。

0 个答案:

没有答案