如何在 C++ 中访问 Python 模块?
How is it possible to access a Python module in C++?
我正在尝试导入 C++ 中的模块;该模块位于一个包中,应该像这样访问:
from x.y import class1,func1, const1, etc
我在 Python 3.6 上,对于这个版本,到目前为止我发现的是使用 PyRun_SimpleString
进行导入,然后使用 PyImport_AddModuleObject
来处理我的模块。即:
PyRun_SimpleString("from PacKage1 import Module1 as Module1");
auto module = PyImport_AddModuleObject(PyUnicode_DecodeFSDefault("Module1"));
这样我以后就可以访问它的不同属性,如下所示:
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;
当我尝试 运行 这段代码时,我得到了显示地图内容的输出:
size: 5
__doc__ : Module1
__loader__ : Module1
__name__ : Module1
__package__ : Module1
__spec__ : Module1
所以这是PyModule_GetDict(module);
的结果 但是,当涉及到从这个模块中提取class时,它失败了,即PyDict_GetItemString(pdict, "MyClass1");
returns null
.
在我看来,模块处理程序本身不正确,这可能是因为它可能没有指向实际模块,这意味着我完全无法导入和获取该模块的句柄。
因此我想不出任何其他方法可以让我导入模块并像这样使用它。
我在这里错过了什么?
正如评论中指出的那样,我最初尝试导入该模块是完全错误的,我应该这样做:
auto module = PyImport_ImportModule("Package1.Module1");
所以我这样做了:
auto module = PyImport_ImportModule("FV.F_V");
这样做之后,现在我得到了更有意义的输出,如您所见:
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++ 中的模块;该模块位于一个包中,应该像这样访问:
from x.y import class1,func1, const1, etc
我在 Python 3.6 上,对于这个版本,到目前为止我发现的是使用 PyRun_SimpleString
进行导入,然后使用 PyImport_AddModuleObject
来处理我的模块。即:
PyRun_SimpleString("from PacKage1 import Module1 as Module1");
auto module = PyImport_AddModuleObject(PyUnicode_DecodeFSDefault("Module1"));
这样我以后就可以访问它的不同属性,如下所示:
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;
当我尝试 运行 这段代码时,我得到了显示地图内容的输出:
size: 5
__doc__ : Module1
__loader__ : Module1
__name__ : Module1
__package__ : Module1
__spec__ : Module1
所以这是PyModule_GetDict(module);
的结果 但是,当涉及到从这个模块中提取class时,它失败了,即PyDict_GetItemString(pdict, "MyClass1");
returns null
.
在我看来,模块处理程序本身不正确,这可能是因为它可能没有指向实际模块,这意味着我完全无法导入和获取该模块的句柄。
因此我想不出任何其他方法可以让我导入模块并像这样使用它。
我在这里错过了什么?
正如评论中指出的那样,我最初尝试导入该模块是完全错误的,我应该这样做:
auto module = PyImport_ImportModule("Package1.Module1");
所以我这样做了:
auto module = PyImport_ImportModule("FV.F_V");
这样做之后,现在我得到了更有意义的输出,如您所见:
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