gpt4 book ai didi

python - 无法从 linux、c++ 执行 "main(filename)"python 脚本的 "my_script"函数; pModule = PyImport_Import(pName);返回空

转载 作者:太空宇宙 更新时间:2023-11-04 11:47:46 26 4
gpt4 key购买 nike

尝试从 Linux,c++ 的 main() 调用 my_script.pymain(filename) 函数。

对于pName = PyUnicode_DecodeFSDefault(argv[1]); pName不为NULL,但是下次调用pModule = PyImport_Import(pName); pModule为NULL。

my_script 的main 函数将图像的文件名作为参数,并返回一个包含蔬菜名称的字符串。这里 pModule = PyImport_Import(pName);pModule 返回 null。

我尝试了 pwd 得到了 /home/sandeep/Downloads/VegeTable-master/VegeTable/image_recognition

export PYTHONPATH=/home/sandeep/Downloads/VegeTable-master/VegeTable/image_recognitionpModule 仍然是 NULL

int main(int argc, char* argv[])
{

PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue, *syspath;
int i;
char * str = get_current_dir_name();
char* path, *eximpath = "/etc/exim";
cout<<"str="<<str<<endl;
if (argc < 3) {
fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
return 1;
}
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}

Py_SetProgramName(program);
Py_Initialize();


system("echo $PYTHONPATH");
pName = PyUnicode_DecodeFSDefault(argv[1]);
/* Error checking of pName left out */

if(pName)
cout<<"OK:"<<endl;

pModule = PyImport_Import(pName);
Py_DECREF(pName);

if(pModule) cout<<"success"<<endl;

if (pModule != NULL)
{
cout<<"success"<<endl;
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 = PyLong_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);
cout<<"success"<<endl;
Py_DECREF(pArgs);
if (pValue != NULL)
{
printf("Result of call: %ld\n", PyLong_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;
}
if (Py_FinalizeEx() < 0)
{
return 120;
}
}
output-
str=/home/sandeep/Downloads/VegeTable-master/VegeTable/image_recognition
/home/sandeep/Downloads/VegeTable-master/VegeTable/image_recognition
OK:
File "/home/sandeep/Downloads/VegeTable-master/VegeTable/image_recognition/my_script.py", line 135
return ('unknown')
^
TabError: inconsistent use of tabs and spaces in indentation
Failed to load "my_script"

用于运行我正在使用的代码./pyembed my_script 主下载.jpeg

我从头开始编写了一个新函数和 2 个脚本文件。两个脚本文件的内容相同。但它在 1 中运行并在其他中给出错误。一个.py->

#!/usr/bin/python

def fun(name):
return 4

myscript.py->

#!/usr/bin/python

#import cv2
#import numpy as np

def fun(name):
return 4

embedpy.c 的内容

int main(int argc, char* argv[])
{

wchar_t* program;
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue, *syspath;
int i;

if(argc<3)
{
cerr<<"Usage: call pythonfile funcname [args]\n"<<endl;
return 1;
}

program = Py_DecodeLocale(argv[0],NULL);

Py_SetProgramName(program);
Py_Initialize();


pName = PyUnicode_DecodeFSDefault(argv[1]);

if(!pName)
{
cerr<<"error in pName"<<endl;

return 1;
}


pModule = PyImport_Import(pName);
Py_DECREF(pName);

if(!pModule)
{
PyErr_Print();
cerr<<"error in pModule"<<endl;

return 1;
}

pFunc = PyObject_GetAttrString(pModule, argv[2]);

if(!pFunc || !PyCallable_Check(pFunc))
{
if (PyErr_Occurred())
PyErr_Print();
cerr<<"can not find function"<<argv[2]<<endl;

return 1;
}

pArgs = PyTuple_New(argc - 3);
for(int i = 0; i < argc-3; i++)
{
pValue = PyLong_FromLong(atoi(argv[i + 3]));

if(!pValue)
{
Py_DECREF(pArgs);
Py_DECREF(pModule);

cerr<<"can not convert argument"<<endl;

return 1;
}

PyTuple_SetItem(pArgs, i, pValue);
}


}

pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);

if(!pValue)
{
Py_DECREF(pFunc);
Py_DECREF(pModule);

PyErr_Print();
cerr<<"function call failed"<<endl;

return 1;
}

cout<<"Result of Call = "<<PyLong_AsLong(pValue)<<endl;
Py_DECREF(pValue);


if (Py_FinalizeEx() < 0)
{
return 120;
}

}

我正在运行

./embedpy an fun download.jpeg

./embedpy myscript fun download.jpeg

在第一种情况下错误 -ModuleNotFoundError:没有名为“an”的模块pModule 错误

在第二种情况下没有错误调用结果 = 4

我真正的问题(实际)是我无法运行

./embedpy my_script main download.jpeg

错误-ModuleNotFoundError:没有名为“my_script”的模块pModule 错误

ls -l gives
-rw-r--r-- 1 sandeep sandeep 47 Jul 19 14:31 an.py
-rwxrwxrwx 1 sandeep sandeep 3947 Jul 19 13:50 my_script.py
-rw-r--r-- 1 sandeep sandeep 79 Jul 19 14:31 myscript.py
-rwxr-xr-x 1 sandeep sandeep 13816 Jul 19 13:59 embedpy
-rw-r--r-- 1 sandeep sandeep 1804 Jul 19 13:54 embedpy.cpp
-rw-r--r-- 1 sandeep sandeep 79 Jul 19 14:53 script2.py

我复制使用

cp myscript.py script2.py

但是

./embedpy script2 fun download.jpeg 

ModuleNotFoundError: 没有名为“script2”的模块pModule 错误

./embedpy myscript fun download.jpeg

调用结果 = 4

奇怪->我做了

export PYTHONPATH=/home/sandeep/Downloads/VegeTable-master/VegeTable/image_recognition

./embedpy an fun download.jpeg
./embedpy myscript fun download.jpeg
./embedpy script2 fun download.jpeg

给予

调用结果 = 4

./embedpy my_script main download.jpeg

给予文件“/home/sandeep/Downloads/VegeTable-master/VegeTable/image_recognition/my_script.py”,第 136 行 返回('未知') ^TabError:缩进中制表符和空格的使用不一致

pModule 错误

我正在尝试从“my_script.py”到“an.py”逐个包含元素我包含了 2 个导入,但它给出了错误

an.py->

!/usr/bin/python

import cv2
import numpy as np

def fun(name):
return 4

./embedpy an fun download.jpeg

给予追溯(最近一次通话): 文件“/home/sandeep/Downloads/VegeTable-master/VegeTable/image_recognition/an.py”,第 3 行,位于 导入cv2ModuleNotFoundError:没有名为“cv2”的模块pModule 错误

任何猜测- 哪里出了问题,python 文件可以独立执行我在控制台中输入“conda update anaconda-navigator” conda :找不到命令

最佳答案

您编写的程序是用 C++ 编写的,如果将文件扩展名添加为 my_script.py,它将无法运行。尝试将文件扩展名更改为 my_script.cpp。错误本身与制表符的缩进和使用有关,这在 python 中是必需的,而不是使用大括号。并将这些包作为标题包含在程序中,以便您使用这些包。

关于python - 无法从 linux、c++ 执行 "main(filename)"python 脚本的 "my_script"函数; pModule = PyImport_Import(pName);返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57104384/

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