python-3.x - 如果使用 anaconda,导入 boost/c++ 时 Python 崩溃

标签 python-3.x c++11 anaconda boost-python

我正在尝试使用 boost/python 为 python 构建 C++ 引擎接口(interface)。 python 导入在 anaconda 环境中失败。我已将 c++ 剥离为以下内容 (simplepython.cpp),但仍然失败:

#include <boost/python/module.hpp>
#include <boost/python/class.hpp>

namespace R3 {
  class Coordinate {
  public:
    Coordinate() : x(0), y(0), z(0) {}
    Coordinate(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
    double getX() const { return x;}
    double getY() const { return y;}
    double getZ() const { return z;}
  private:
    double x, y, z;
  };
}

BOOST_PYTHON_MODULE(simplepython) {
  // An established convention for using boost.python.
  using namespace boost::python;
  Py_Initialize();

  class_<R3::Coordinate>("R3Coordinate", init<>())
    .def(init<int, int, int>())
    .def("getX", &R3::Coordinate::getX)
    .def("getY", &R3::Coordinate::getY)
    .def("getZ", &R3::Coordinate::getZ)
  ;
}

尝试一下:

$> python -c 'import sys;打印(sys.executable);导入简单的python

... SystemError: initialization of _heapq did not return an extension module

系统/构建细节:

Mac 操作系统 Mojave,10.14.6 Python 版本 3.7.4, conda 4.8.3 C++ 等:Apple clang 版本 11.0.0 (clang-1100.0.33.17)

conda 安装是:conda install -c anaconda boost

升压版本:boost-1.67.0

构建:

g++ -I/opt/anaconda3/include/python3.7m -I/opt/anaconda3/include/python3.7m -I/opt/anaconda3/include -O2 -fPIC -std=c++11 -Iinclude - c -o objs/simplepython.o src/simplepython.cpp

g++ -Wl,-rpath,/opt/anaconda3/lib -shared -o lib/simplepython.so objs/simplepython.o -L/opt/anaconda3/lib -Llib -lpython3.7m -ldl -framework CoreFoundation - lboost_python37 -lboost_numpy37

完整结果:

/opt/anaconda3/bin/python
Error processing line 1 of /opt/anaconda3/lib/python3.7/site-packages/matplotlib-3.1.1-py3.7-nspkg.pth:

Fatal Python error: initsite: Failed to import the site module
Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.7/site.py", line 168, in addpackage
    exec(line)
  File "<string>", line 1, in <module>
  File "/opt/anaconda3/lib/python3.7/importlib/util.py", line 14, in <module>
    from contextlib import contextmanager
  File "/opt/anaconda3/lib/python3.7/contextlib.py", line 5, in <module>
    from collections import deque
  File "/opt/anaconda3/lib/python3.7/collections/__init__.py", line 24, in <module>
    import heapq as _heapq
  File "/opt/anaconda3/lib/python3.7/heapq.py", line 587, in <module>
    from _heapq import *
SystemError: initialization of _heapq did not return an extension module

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.7/site.py", line 579, in <module>
    main()
  File "/opt/anaconda3/lib/python3.7/site.py", line 566, in main
    known_paths = addsitepackages(known_paths)
  File "/opt/anaconda3/lib/python3.7/site.py", line 349, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "/opt/anaconda3/lib/python3.7/site.py", line 207, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "/opt/anaconda3/lib/python3.7/site.py", line 178, in addpackage
    import traceback
  File "/opt/anaconda3/lib/python3.7/traceback.py", line 3, in <module>
    import collections
  File "/opt/anaconda3/lib/python3.7/collections/__init__.py", line 24, in <module>
    import heapq as _heapq
  File "/opt/anaconda3/lib/python3.7/heapq.py", line 587, in <module>
    from _heapq import *
SystemError: initialization of _heapq did not return an extension module

感谢您的帮助!

最佳答案

事实证明这个错误不是专门针对 Py_Initialize() 的,但在程序和 python 运行时之间有符号链接(symbolic link)。以下完全解决了它:

在您的链接行中,替换 -lpython<version>m-undefined dynamic_lookup.

CMake: 链接行在 /build/mybinary/CMakeFiles/myextension_py.dir/link.txt 中可用。 .现在看来,链接命令必须在 CMake 版本 3.17.2 中手动编辑.

致谢: Nehal J Wani 最初报告了此解决方案,请参阅他对 this question 的回答.

注意:我一直在使用 $(shell python3-config --libs)导出这些 -l 路径,不幸的是,其中包括 -lpython<version>m;从 3.8 版本开始就不会了。由于这种不一致,我暂时只使用我自己的扩展数据。

致谢:Nehal J Wani

关于python-3.x - 如果使用 anaconda,导入 boost/c++ 时 Python 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61345453/

相关文章:

python - 如果存在则写入文件,如果不存在则创建文件

c++ - 如何在 C 中公开 C++ 函数指针?

c++ - 如何调用一个变量(包含 rand() 函数)并始终得到不同的数字?

c++11 - 从包含其他字符的字符串中提取尾随 int

python - Atom : Error running Hydrogen, 找不到python.exe

Python,装饰函数中引用类实例/方法

python - PyCharm 中针对特定类型列表的类型提示自动补全

python-3.x - Networkx 问题 : need to install Graphviz/Pygraphviz (Anaconda 2021. 11,WIN10x64)

python - `pip list` 和 anaconda 包列表中的 Keras 但无法导入

python - 安装包 Beautiful Soup 失败。错误消息是 "SyntaxError: Missing parentheses in call to ' print'"