[英]Is it possible to access C++ headers/libraries and run a C++ script within a python console?
[英]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);
的结果PyModule_GetDict(module);
但是,说到从这个模块中抽取类的时候,就失败了,也就是PyDict_GetItemString(pdict, "MyClass1");
返回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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.