Python嵌入C++;:导入外部模块 我试图用C++运行Python模块。我正在使用中提供的说明 我的C++代码: #include <iostream> #include <cmath> #include <string> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cstring> #include <Python.h> using namespace std; int main(int argc, char **argv) { PyObject *pName, *pModule, *pDict, *pFunc; PyObject *pArgs, *pValue; int i; if (argc < 3) { fprintf(stderr,"Usage: call pythonfile funcname [args]\n"); return 1; } Py_Initialize(); pName = PyString_FromString(argv[1]); /* Error checking of pName left out */ pModule = PyImport_Import(pName); Py_DECREF(pName); if (pModule != NULL) { pFunc = PyObject_GetAttrString(pModule, argv[2]); /* pFunc is a new reference */ if (pFunc && PyCallable_Check(pFunc)) { pArgs = PyTuple_New(argc - 3); for (i = 0; i < argc - 3; ++i) { pValue = PyInt_FromLong(atoi(argv[i + 3])); if (!pValue) { Py_DECREF(pArgs); Py_DECREF(pModule); fprintf(stderr, "Cannot convert argument\n"); return 1; } /* pValue reference stolen here: */ PyTuple_SetItem(pArgs, i, pValue); } pValue = PyObject_CallObject(pFunc, pArgs); Py_DECREF(pArgs); if (pValue != NULL) { printf("Result of call: %ld\n", PyInt_AsLong(pValue)); Py_DECREF(pValue); } else { Py_DECREF(pFunc); Py_DECREF(pModule); PyErr_Print(); fprintf(stderr,"Call failed\n"); return 1; } } else { if (PyErr_Occurred()) PyErr_Print(); fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]); } Py_XDECREF(pFunc); Py_DECREF(pModule); } else { PyErr_Print(); fprintf(stderr, "Failed to load \"%s\"\n", argv[1]); return 1; } Py_Finalize(); return 0; }

Python嵌入C++;:导入外部模块 我试图用C++运行Python模块。我正在使用中提供的说明 我的C++代码: #include <iostream> #include <cmath> #include <string> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cstring> #include <Python.h> using namespace std; int main(int argc, char **argv) { PyObject *pName, *pModule, *pDict, *pFunc; PyObject *pArgs, *pValue; int i; if (argc < 3) { fprintf(stderr,"Usage: call pythonfile funcname [args]\n"); return 1; } Py_Initialize(); pName = PyString_FromString(argv[1]); /* Error checking of pName left out */ pModule = PyImport_Import(pName); Py_DECREF(pName); if (pModule != NULL) { pFunc = PyObject_GetAttrString(pModule, argv[2]); /* pFunc is a new reference */ if (pFunc && PyCallable_Check(pFunc)) { pArgs = PyTuple_New(argc - 3); for (i = 0; i < argc - 3; ++i) { pValue = PyInt_FromLong(atoi(argv[i + 3])); if (!pValue) { Py_DECREF(pArgs); Py_DECREF(pModule); fprintf(stderr, "Cannot convert argument\n"); return 1; } /* pValue reference stolen here: */ PyTuple_SetItem(pArgs, i, pValue); } pValue = PyObject_CallObject(pFunc, pArgs); Py_DECREF(pArgs); if (pValue != NULL) { printf("Result of call: %ld\n", PyInt_AsLong(pValue)); Py_DECREF(pValue); } else { Py_DECREF(pFunc); Py_DECREF(pModule); PyErr_Print(); fprintf(stderr,"Call failed\n"); return 1; } } else { if (PyErr_Occurred()) PyErr_Print(); fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]); } Py_XDECREF(pFunc); Py_DECREF(pModule); } else { PyErr_Print(); fprintf(stderr, "Failed to load \"%s\"\n", argv[1]); return 1; } Py_Finalize(); return 0; },python,c++,numpy,python-embedding,Python,C++,Numpy,Python Embedding,我使用 g++ -static -I/usr/include/python2.7 -L/usr/lib/python2.7/config/ -o embed embed.cc -lpython2.7 -ldl -lm -lutil -lz -pthread 并使用 PYTHONPATH=. ./embed multiply multiply 1 2 这对我来说很好。但是,如果我尝试导入numpy模块或任何外部模块,它不会在运行时崩溃 import numpy as np File "/u

我使用

g++ -static -I/usr/include/python2.7 -L/usr/lib/python2.7/config/ -o embed embed.cc -lpython2.7 -ldl -lm -lutil -lz -pthread
并使用

PYTHONPATH=. ./embed multiply multiply 1 2
这对我来说很好。但是,如果我尝试导入numpy模块或任何外部模块,它不会在运行时崩溃

import numpy as np
  File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 153, in <module>
    from . import add_newdocs
  File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 6, in <module>
    from . import multiarray
ImportError: /usr/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined symbol: PyExc_SystemError
Failed to load "multiply"
将numpy导入为np
文件“/usr/lib/python2.7/dist-packages/numpy/_-init__.py”,第153行,在
从…起导入添加新文档
文件“/usr/lib/python2.7/dist packages/numpy/add_newdocs.py”,第13行,在
从numpy.lib导入添加新文档
文件“/usr/lib/python2.7/dist packages/numpy/lib/_init__.py”,第8行,在
from.type\u检查导入*
文件“/usr/lib/python2.7/dist packages/numpy/lib/type_check.py”,第11行,在
将numpy.core.numeric作为_nx导入
文件“/usr/lib/python2.7/dist-packages/numpy/core/__-init__.py”,第6行,在
从…起导入多数组
ImportError:/usr/lib/python2.7/dist-packages/numpy/core/multiarray.so:未定义的符号:PyExc\u SystemError
未能加载“乘法”
请告诉我如何使它运行! 编辑:我试着包括
dlopen(“libpython2.7.so.1”,RTLD_LAZY | RTLD_GLOBAL)
如中所建议的线路


但是,pModule仍然没有初始化

我尝试使用C++代码的副本,在你所说的链接中,OP是没有错误的。但是,它仍然无法识别test_func()模块您是否尝试将Py_SetPath视为一个选项?
import numpy as np
  File "/usr/lib/python2.7/dist-packages/numpy/__init__.py", line 153, in <module>
    from . import add_newdocs
  File "/usr/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/lib/python2.7/dist-packages/numpy/core/__init__.py", line 6, in <module>
    from . import multiarray
ImportError: /usr/lib/python2.7/dist-packages/numpy/core/multiarray.so: undefined symbol: PyExc_SystemError
Failed to load "multiply"