- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试一个非常基本的“hello world”程序,它应该在我的 C++ 控制台应用程序中嵌入一个 python 脚本,但它在 pModule = PyImport_Import(pName);
处失败并出现未指定的异常“访问违规阅读位置……”
我已经能够为没有定义和返回的 python 脚本运行 PyRun_SimpleFile()
,但是对于我 future 的应用程序,我需要一个有返回值的 python 方法,所以 PyRun_SimpleFile()
不是一个选项。
我的代码,基于 this Introduction是:
主要.cpp
#include "stdafx.h"
#include <stdlib.h>
#include <Python.h>
int main(int argc, char *argv[])
{
PyObject *pName, *pModule;
PyObject *pFunc, *pValue;
pName = PyUnicode_FromString("HelloWorld");
pModule = PyImport_Import(pName);
Py_XDECREF(pName);
if (pModule)
{
pFunc = PyObject_GetAttrString(pModule, "getInteger");
if (pFunc && PyCallable_Check(pFunc))
{
pValue = PyObject_CallObject(pFunc, NULL);
printf_s("C: getInteger() = %ld\n", PyLong_AsLong(pValue));
Py_XDECREF(pValue);
}
else
{
printf("ERROR: function getInteger()\n");
}
Py_XDECREF(pFunc);
}
else
{
printf_s("ERROR: Module not imported\n");
}
Py_XDECREF(pModule);
Py_Finalize();
return 0;
}
HelloWorld.py(在我的 VS2015 解决方案的调试位置):
def getInteger():
print('Python function getInteger() called')
c = 100*2
return c
最佳答案
好吧,我相信您的代码中缺少一些指令,例如 Py_Initialize
。我还会使用 PyImport_ImportModule
而不是 PyImport_Import
。看看你可能会尝试的这个序列:
int main(int argc, char *argv[])
{
Py_SetPythonHome(L"path/to/python/folder");
Py_Initialize();
//PySys_SetArgv(argc, argv); //optional, argv must be wchar_t
PyObject *pFunc, *pValue;
pModule = PyImport_ImportModule("HelloWorld");
if (pModule)
{
pFunc = PyObject_GetAttrString(pModule, "getInteger");
if (pFunc && PyCallable_Check(pFunc))
{
pValue = PyObject_CallObject(pFunc, NULL);
printf_s("C: getInteger() = %ld\n", PyLong_AsLong(pValue));
Py_XDECREF(pValue);
}
else
{
printf("ERROR: function getInteger()\n");
}
Py_XDECREF(pFunc);
}
else
{
printf_s("ERROR: Module not imported\n");
}
Py_XDECREF(pModule);
Py_Finalize();
return 0;
}
如果仍然无法正常工作,请尝试在 PyInitialize
之后添加:
PyRun_SimpleString(
"import os, sys \n"
"sys.path.append(os.getcwd()) \n"
);
同样在 PyInitialize
之后,您可以检查它是否使用 Py_IsInitialized
进行了初始化。
关于c++ - 如何修复 C++ 中的 PyImport_Import(python35.dll 中的异常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56396346/
我正在尝试在我的 c++ 中调用 python 函数 当我导入我的 python 模块时,它给出了这个错误:ModuleNotFoundError: No module named 'test'这是我
我试过替换 PyRun_SimpleString("import Pootle"); 与 PyObject *obj = PyString_FromString("Pootle"); PyImport
我正在尝试运行 embedding example并且我不能从当前工作目录加载模块,除非我明确地将它添加到 sys.path 然后它工作: PyRun_SimpleString("import sys
使用下一行 pModule = PyImport_Import(pName); 只从当前目录加载模块。 但是我想从其他地方加载什么?有没有一种巧妙的方法可以做到这一点? PyRun_SimpleStr
首先,是的,我看到了this和 this ,但是他们没有解决我的问题/错误。 因此,我试图从 C/C++ 调用 Python 函数,但是当调用 PyImport_Import() 时,它返回 NULL
我是 python 的新手,所以这可能是一个愚蠢的问题。我想用嵌入式 python 脚本编写简单的 c 程序。我有两个文件: 调用函数.c: #include int main(in
我有一个这样的cpp代码: void callPython() { Py_Initialize(); PyObject* sysPath = PySys_GetObject("p
我正在尝试一个非常基本的“hello world”程序,它应该在我的 C++ 控制台应用程序中嵌入一个 python 脚本,但它在 pModule = PyImport_Import(pName);
我正在尝试通过 c Api 从 c++ 调用 python,以获取 c++ 中两个 numpy 数组的值。第一次调用我的程序 callPython() 时,一切似乎都运行良好,但第二次调用导致 SIG
尝试从 Linux,c++ 的 main() 调用 my_script.py 的 main(filename) 函数。 对于pName = PyUnicode_DecodeFSDefault(argv
我是一名优秀的程序员,十分优秀!