如何使用C api通过完整路径导入文件? [英] How to import a file by its full path using C api?
本文介绍了如何使用C api通过完整路径导入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
PyObject * PyImport_ImportModule(const char * name)
PyObject* PyImport_ImportModule( const char *name)
如何指定完整的文件路径和模块名称?
How to specify a full file path instead and a module name?
像PyImport_SomeFunction(const char *path_to_script, const char *name)
谢谢, 埃里亚斯
推荐答案
最终,我最终使用了来自imp模块的load_source:
Eventually, I ended up using the load_source from imp module:
s.sprintf(
"import imp\n"
"imp.load_source('%s', r'%s')", modname, script_path);
PyRun_SimpleString(s.c_str());
我认为这是最可行的解决方案.欢迎其他建议.
I think it is the most feasible solution. Other suggestions are welcome.
这篇关于如何使用C api通过完整路径导入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文