如果脚本包含dlopen(),则Python 2.7 API PyImport\u ImportModule()返回NULL

2024-10-03 02:41:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python C API加载一些脚本,但是在macOS X上,我注意到如果脚本包含dylib,例如import datetime,那么它将失败:

#include <stdio.h>
#ifdef __APPLE__
#include "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Python.framework/Headers/Python.h"
#else
#include "/usr/include/python2.7/Python.h"
#endif
#include <stdio.h>
#include <string.h>

void testingLoadModuleByPath(char * path, const char * name, const char * content)
{
  char fullpath[256];
  sprintf(fullpath, "%s/%s.py", path, name);
  FILE * file = fopen(fullpath, "w");
  fwrite(content, 1, strlen(content), file);
  fclose(file);
  Py_Initialize();
  PySys_SetPath(path);
  PyObject * h = PyImport_ImportModule(name);
  if (h == NULL) {
    PyErr_Print();
  } else {
    printf("%s: ok\n", name);
  }
  Py_Finalize();
}

int main(int argc, const char * argv[]) {
  testingLoadModuleByPath("/tmp", "mysys", "import sys\nclass Person:\n\thome = ''\n\tdef __init__(self):\n\t\thome = sys.path\n");
  testingLoadModuleByPath("/tmp", "mydat", "import datetime\nclass Person:\n\tbirthday = ''\n\tdef __init__(self):\n\t\tself.birthday = datetime.datetime.now().strftime(\"%A, %d. %B %Y %I:%M%p\")\n");
  return 0;
}

如果成功,两个测试都应显示“OK”,但这是实际结果:

$ clang -o pytst *.c -lpython2.7 && ./pytst
mysys: ok
Traceback (most recent call last):
  File "/tmp/mydat.py", line 1, in <module>
    import datetime
ImportError: No module named datetime
Program ended with exit code: 0

所以问题是,如何使用PyImport\u ImportModule()导入.dylib/.So库,比如pythoncapi中的datetime?你知道吗

总之,代码在Ubuntu 16.04 LTS上运行良好:

$ clang -o pytst *.c -lpython2.7 && ./pytst
mysys: ok
mydat: ok

Tags: pathnameimportdatetimeincludeokcontenttmp