Python 如果使用 anaconda,导入 boost/c++ 时会崩溃

Python crash when boost/c++ is imported if anaconda was used

我正在尝试使用 boost/python 为 python 构建 C++ 引擎接口。 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

Sys/build 详情:

Mac OS 莫哈韦沙漠,10.14.6 Python 版本 3.7.4, 康达 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 运行时之间的符号 linkage。以下完全解决了它:

在您的 link 行中,将 -lpython<version>m 替换为 -undefined dynamic_lookup.

CMake: link 行在 /build/mybinary/CMakeFiles/myextension_py.dir/link.txt 中可用。目前看来 link 命令必须在 CMake 版本 3.17.2.

中手动编辑

学分: Nehal J Wani 最初报告了这个解决方案,请参阅他对 .

的回答

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

致谢Nehal J Wani