如何在C ++中访问python模块? [英] How is it possible to access a python module in C++?

查看:115
本文介绍了如何在C ++中访问python模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导入C ++中的模块.该模块位于一个包中,应按以下方式访问:

I'm trying to import a module in C++. the module resides in a package and should be accessed like:

from x.y import class1,func1, const1, etc 

我使用的是Python3.6,到目前为止,对于该版本,我发现使用PyRun_SimpleString进行导入,然后使用PyImport_AddModuleObject来处理我的模块.即:

I'm on Python3.6 and for this version what I have found so far is to use PyRun_SimpleString to do the import and then use PyImport_AddModuleObject to have a handle to my module. that is:

PyRun_SimpleString("from PacKage1 import Module1 as Module1");
auto module = PyImport_AddModuleObject(PyUnicode_DecodeFSDefault("Module1"));

这样一来,我便可以访问其不同的属性,如下所示:

so that down the road I can access its different attributes, something like this:

auto args = Py_BuildValue("sOOOOONNiN", model_name, model_checkpoint_path, align_fn,
                          bank_folder_root, cache_folder, postfix, 
                          rebuild_cache, use_jit, threshold, device);

if (module != nullptr) {
    // dict is a borrowed reference.
    auto pdict = PyModule_GetDict(module);
    if (pdict == nullptr) {
        cout << "Fails to get the dictionary.\n";
        return 1;
    }
    //Py_DECREF(module);
    PyObject *pKeys = PyDict_Keys(pdict);
    PyObject *pValues = PyDict_Values(pdict);

    map<string, string> my_map;
    //cout << "size: " << PyDict_Size(pdict)<<endl;

    char* cstr_key = new char[100];
    char* cstr_value = new char[500];

    for (Py_ssize_t i = 0; i < PyDict_Size(pdict); ++i) {
        PyArg_Parse(PyList_GetItem(pKeys, i), "s", &cstr_key);
        PyArg_Parse(PyList_GetItem(pValues, i), "s", &cstr_value);
        //cout << cstr<< "  "<< cstr2 <<endl;
        my_map.emplace(cstr_key, cstr_value);
    }
    for (auto x : my_map)
    {
        cout << x.first << " : " << x.second << endl;
    }
    system("pause");

    // Builds the name of a callable class
    auto python_class = PyDict_GetItemString(pdict, "MyClass1");

    system("pause");
    if (python_class == nullptr) {
        cout << "Fails to get the Python class.\n";
        return 1;
    }
    //Py_DECREF(pdict);


    cout << python_class;
    PyObject* object;

    // Creates an instance of the class
    if (PyCallable_Check(python_class)) {
        object = PyObject_CallObject(python_class, args);
        Py_DECREF(python_class);
    }
    else {
        std::cout << "Cannot instantiate the Python class" << endl;
        Py_DECREF(python_class);
        return 1;
    }

    auto val = PyObject_CallMethod(object, "is_jit_model_available", NULL);
    if (!val)
        cout << "error!";

    cout << val;

当我尝试运行此代码时,我得到以下显示地图内容的输出:

When I try to run this code, I get this output which shows the maps contents:

size: 5
__doc__ : Module1
__loader__ : Module1
__name__ : Module1
__package__ : Module1
__spec__ : Module1

所以这是PyModule_GetDict(module);的结果,但是,当涉及到从该模块中提取类时,它会失败,即PyDict_GetItemString(pdict, "MyClass1");返回null.

So this is the result of PyModule_GetDict(module); However, when it comes to the class extraction from this module, it fails, that is, the PyDict_GetItemString(pdict, "MyClass1"); returns null.

在我看来,模块处理程序本身是不正确的,这可能是因为它未指向实际模块,这意味着我完全无法导入并获取该模块的句柄.

It seems to me the module handler itself is not right and that is probably because maybe it doesnt point to the actual module which means I completely failed at importing and getting a handle to that module.

因此,我想不出任何其他方式允许我导入模块并像这样使用它.

Therefore I cant think of any other way that allows me to import a module and use it like this.

我在这里想念什么?

推荐答案

正如注释中指出的,我最初导入模块的尝试是完全错误的,相反,我应该这样做:

As pointed in the comments, my initial attempt for importing the module is completely wrong, and instead I should have done this :

auto module = PyImport_ImportModule("Package1.Module1");

所以我这样做了:

auto module = PyImport_ImportModule("FV.F_V");

完成此操作后,您将看到更多有意义的输出:

After doing this, now I get much more meaningful output as you can see:

module: 0000021306FF9B38
size: 30
FaceVerification: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
Image: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
Path: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
__builtins__: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
__cached__: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
__doc__: FV.F_V
__file__: C:\Users\Master\Anaconda3\Lib\site-packages\FV\F_V.py
__loader__: FV
__name__: FV.F_V
__package__: FV
__spec__: FV
__warningregistry__ : C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
align_face: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
cv2: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
data_transforms: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
get_central_face_attributes: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
math: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
nn: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
np: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
os: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
pickle: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
plt: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
resnet101: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
resnet18: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
resnet50: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
scipy: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
time: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
torch: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
tqdm: C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc
transforms : C:\Users\Master\Anaconda3\Lib\site-packages\FV\__pycache__\F_V.cpython-36.pyc

这篇关于如何在C ++中访问python模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆