how to load a custom python module in c

安稳与你 提交于 2019-12-05 10:04:29
iabdalkader

I think it should be in the same directory or in sys.path as it loads a module by name, this should work:

./call multiply multiply 5 6

Update: if I add the current directory explicitly to sys.path it works:

Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");

And this prints:

./call multiply multiply 5 6
('Will compute', 5, 'times', 6)
Result of call: 30

Update: I've asked a relevant question and it seems that if you just add PySys_SetArgv instead it works:

Py_Initialize();
PySys_SetArgv(argc, argv);

The reason is mentioned here:

Otherwise (that is, if argc is 0 or argv[0] doesn’t point to an existing file name), an empty string is prepended to sys.path, which is the same as prepending the current working directory (".")

And this is the question you can check the answers there too:

Why does PyImport_Import fail to load a module from the current directory?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!