os.path.isdir不起作用,如果我从C ++调用它

时间:2019-04-08 11:46:25

标签: python c++ qt

我正在尝试(copying)将python插件加载到我的C ++应用程序中的代码。

此代码本身运行良好。我刚刚添加了一个函数来从我的c ++调用它,并打印了一个调试用的图片:

if(c>5)
{
    b = (a/c)/2;
}
else
{
    b = 0;
}

我有一个像这样的树结构: import imp import os MainModule = "__init__" PluginFolder = "plugins" def getPlugins(ruta): plugins = [] possibleplugins = os.listdir(ruta + PluginFolder) for i in possibleplugins: location = os.path.join(PluginFolder, i) print (location) print (os.path.isdir(location)) if not os.path.isdir(location) or not MainModule + ".py" in os.listdir(location): continue info = imp.find_module(MainModule, [location]) plugins.append({"name": i, "info": info}) return plugins def loadPlugin(plugin): return imp.load_module(MainModule, *plugin["info"]) def iniciar(ruta): for i in getPlugins(ruta): print("Loading plugin " + i["name"]) plugin = loadPlugin(i) plugin.run() if __name__ == "__main__": for i in getPlugins("/myabsolutepath/python/"): print("Loading plugin " + i["name"]) plugin = loadPlugin(i) plugin.run()

我有任何dummy.py文件和各种文件夹用于探测。在其中一个文件夹中,我有一个 init .py文件

如果我运行脚本本身,我将得到正确的结果:

/myabsolutepath/python/plugins/

现在我尝试在C ++函数(Qt应用程序)下调用:

plugins/dummy.py
False
plugins/C1
True
plugins/C3
True
plugins/C2
True

我得到的都是假的:

void callplugins (const char* ruta, const char* nombremodulo, const char* nombrefuncion)
{
    qDebug()<<"ruta: "<<ruta;
    PyObject* modulo = nullptr;
    PyObject* pName = nullptr;
    PyObject* listado = nullptr;
    Py_Initialize();
    PyObject *sysPath = PySys_GetObject((char*)"path");
    PyObject *path = PyUnicode_FromString((char*)ruta);
    int result = PyList_Insert(sysPath, 0, path);
    if (result == 0 )
    {
        pName = PyUnicode_FromString(nombremodulo);
        modulo = PyImport_Import(pName);
        qDebug()<<"modulo "<<modulo;
        PyObject *args = PyTuple_New(1);
        PyObject* nombre = Py_BuildValue("s", ruta);
        PyTuple_SetItem(args, 0, nombre);
        Py_DECREF(path);
        if (modulo)
        {
            pFuncion = PyObject_GetAttrString(modulo, nombrefuncion);
            Py_DECREF(modulo);
            if (pFuncion && PyCallable_Check(pFuncion))
            {
                qDebug()<<"Funcion exportar: "<<" -- "<<pFuncion;
                listado = PyObject_CallObject(pFuncion,args);
                Py_DECREF(args);
            }
        }
    }
    if (PyList_Check(listado))
    {
        qDebug()<<"okay "<<PyList_Size(listado);
        // okay, it's a list
        for (Py_ssize_t i = 0; i < PyList_Size(listado); ++i)
        {
            PyObject* siguiente = PyList_GetItem(listado, i);
            qDebug()<<"Siguiente: "<<siguiente;
            // do something with next
        }
    }
    qDebug()<<"Cargados los modulos";
}

0 个答案:

没有答案