我想将python嵌入到C应用程序中。具体来说,我想将sympy用作python部分的一部分。为此,我创建了anaconda的全新安装。这是一个说明我的问题的最小工作示例。
int main()
{
putenv("PYTHONHOME=../anaconda3/bin/python3.7m");
Py_SetPath(L"../:../anaconda3/lib/python3.7:../anaconda3/lib/python3.7/lib-dynload:../anaconda3/lib/python3.7/site-packages:../anaconda3/lib/python3.7/site-packages/aeosa:../anaconda3/lib/python3.7/site-packages/IPython/extensions:../.ipython");
Py_Initialize();
//PyRun_SimpleString("import math\n");
PyRun_SimpleString("import sympy\n");
Py_FinalizeEx();
return (EXIT_SUCCESS)
}
此代码可以正常编译,但是当我尝试运行它时,出现以下错误。
Traceback (most recent call last):
File "../anaconda3/lib/python3.7/site-packages/sympy/__init__.py", line 19, in <module>
import mpmath
File "../anaconda3/lib/python3.7/site-packages/mpmath/__init__.py", line 5, in <module>
from .ctx_fp import FPContext
File "../anaconda3/lib/python3.7/site-packages/mpmath/ctx_fp.py", line 1, in <module>
from .ctx_base import StandardBaseContext
File "../anaconda3/lib/python3.7/site-packages/mpmath/ctx_base.py", line 3, in <module>
from .libmp.backend import xrange
File "../anaconda3/lib/python3.7/site-packages/mpmath/libmp/__init__.py", line 1, in <module>
from .libmpf import (prec_to_dps, dps_to_prec, repr_dps,
File "../anaconda3/lib/python3.7/site-packages/mpmath/libmp/libmpf.py", line 7, in <module>
import math
ImportError: dlopen(../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so, 2): Symbol not found: _PyExc_MemoryError
Referenced from: ../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so
Expected in: flat namespace
in ../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so
看着这个错误,看来“进口数学”上有错误。单独导入数学运算会出现以下错误,以确认这一点。
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: dlopen(../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so, 2): Symbol not found: _PyExc_MemoryError
Referenced from: ../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so
Expected in: flat namespace
in ../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so
我发现了一些遇到和讨论过类似问题的参考文献(例如this,this,this和this),但是解决方案要么无效,没有提供,或者我不完全了解它们可能与我的问题有何关系。 This解决方案建议在初始化之前添加以下行可能会有帮助
void*const libpython_handle = dlopen("libpython3.7m.dylib", RTLD_LAZY | RTLD_GLOBAL); // update to my python version
我也尝试过
void*const libpython_handle = dlopen("../anaconda3/lib/python3.7/lib-dynload/math.cpython-37m-darwin.so", RTLD_LAZY | RTLD_GLOBAL);
这是(显然)是导致问题的原因。两者都不起作用。看来该问题与lib-dynload / *文件的依赖性问题(已讨论here)有关,但到目前为止,我还没有找到解决方法。
我该如何解决此问题?
编辑:
我正在使用MacOS