无法从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
试图从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 中是必需的,而不是使用大括号。 并将包作为标头包含在程序中,以便您使用包。
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 对可变参数使用声明.如何选择正确的功能
- 询问在设计我的手臂模拟器功能表示格式1
- 功能原型的目的
- 无法从Linux,C ++执行"my_script" python脚本"main(filename)"功能;pModule = PyImport_Import(pName);返回空值