Python 2.7 API PyImport_ImportModule()如果脚本包含dlopen()则返回NULL

时间:2018-06-10 17:45:16

标签: python python-c-api dlopen

我正在使用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_ImportModule()在Python C API中导入.dylib / .so库,例如datetime?

无论如何,代码在Ubuntu 16.04 LTS上运行良好:

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

1 个答案:

答案 0 :(得分:0)

最终,我发现它只发生在Python 2.7中,而Python 3.5+没有这个问题。