尝试从Linux的main(),c++调用my_script.py的main(filename)函数。
For call pName不为NULL,但next pName = PyUnicode_DecodeFSDefault(argv[1]); 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 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 main download.jpeg
我从头开始写了一个新函数和两个脚本文件。两个脚本文件的内容相同。但它在1中运行,并在其他中给出错误。an.py->
#!/usr/bin/python
def fun(name):
return 4myscript.py->
#!/usr/bin/python
#import cv2
#import numpy as np
def fun(name):
return 4embedpy.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: pModule中没有名为'an‘的模块错误
在第二种情况下,调用没有错误结果=4
我真正的问题(实际)是我不能跑步
./embedpy my_script main download.jpegerror- ModuleNotFoundError: pModule中没有名为'my_script‘的模块错误
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: pModule中没有名为“script2”的模块错误
和
./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行('unknown') ^ TabError:缩进中的制表符和空格使用不一致
pModule中的错误
我尝试从"my_script.py“到"an.py”中包含元素1到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行,导入cv2 ModuleNotFoundError: pModule中没有名为'cv2‘的模块错误
我在控制台输入"conda update anaconda-navigator“conda: command not found
发布于 2019-07-19 14:39:46
你写的程序是用C++编写的,如果你把文件扩展名添加为my_script.py,它将无法工作。尝试将文件扩展名更改为my_script.cpp。错误本身是关于缩进和使用制表符的,这在python中是必需的,而不是使用大括号。并将这些包作为标题包含在程序中,以便您使用这些包。
https://stackoverflow.com/questions/57104384
复制相似问题