gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:10:52 26 4
gpt4 key购买 nike

我正在尝试使用 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.6Python 版本 3.7.4, conda 4.8.3C++ 等: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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com