Python C API中的AttributeError? [英] AttributeError in Python C API?

查看:78
本文介绍了Python C API中的AttributeError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有两个项目:一个库和一个控制台应用程序。在库中,我有以下函数声明:



So, I have two projects: a library and a console app. In the library, I have the following function declaration:

std::string NewApp::Testing()
{
	char* test[] = {"test", 0};
	PyObject* obj = Python::Py_Arg("test", "test", test);
	std::string s = PyString_AsString(obj);
	return s;
}





然后,在应用程序中,我调用该函数:





Then, in the app, I call the function:

int _tmain(int argc, _TCHAR* argv[])
{
	std::cout << NewApp::Testing();
	return 0;
}





所有人都调用以下Python脚本(与静态库位于同一版本目录中) ):





Which all makes a call to the following Python script (which is in the same release directory as the static library):

def test(wrd):
    return wrd





然而,无论出于何种原因,每次运行程序时都会出现以下错误:





Yet, for whatever reason, I get the following error every time I run the program:

AttributeError: `module` object has no attribute `test`
Cannot find function "test"





这让我发疯了!它知道文件的位置,但无法找到该功能。有什么可能导致它?我已经在其他应用程序中使用嵌入式Python工作了,但我还是无法让这个工作!任何帮助表示赞赏。谢谢。



And it's driving me crazy! It knows where the file is but it can't find the function. What could be causing it?? I've gotten embedded Python to work before in another app, yet I still can't get this one to work! Any help is appreciated. Thanks.

推荐答案

我在这里可以看到一些东西:



I can see something here:

std::string NewApp::Testing()
{
    char* test[] = {"test", 0};//Here you have a char* array called 'test' set to the string of characters "test", ending with the null character '0'///
    PyObject* obj = Python::Py_Arg("test", "test", test);// Here you have 3 tests in your Py_Arg, one of which is the char* array.///
    std::string s = PyString_AsString(obj); // string s now = "test", "test", "test".
    return s;
}





这里你再次'定义'测试:





And here you 'define' "test" again:

def test(wrd):
    return wrd

你知道'wrd'是什么类型的吗?虽然我不懂Python,但这行代码并不像你想象的那样有效。你定义一个函数,一个变量,一个列表,???

Do you know what type 'wrd' is? While I'm not Python literate, this line of code is not working as you think. Are you defining a function, a variable, a list, ???


这篇关于Python C API中的AttributeError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆