无法从Linux,C ++执行"my_script" python脚本"main(filename)"功能;pModule = PyImport_Import(pName);返回空值

unable to execute "main(filename)" function of "my_script" python script from linux,c++; pModule = PyImport_Import(pName); returns null

本文关键字:功能 filename 返回 空值 pName Import PyImport pModule 脚本 Linux 执行      更新时间:2023-10-16

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

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

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

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

export PYTHONPATH=/home/sandeep/Downloads/VegeTable-master/VegeTable/image_recognition但仍然pModule为空

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 argumentn");
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: %ldn", PyLong_AsLong(pValue));
Py_DECREF(pValue);
}
else
{
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failedn");
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 中运行并在其他中给出错误。 an.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"包含 1 个元素 我包括 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 行,在 导入简历2 ModuleNotFoundError: 没有名为"cv2"的模块 pModule 中的错误

任何猜测 - 出了什么问题,Python 文件独立执行正常 我在控制台中输入"conda update anaconda-navigator" conda:找不到命令

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