Search code examples
pythonpython-c-apidlopen

Python 2.7 API PyImport_ImportModule() returns NULL if script includes dlopen()


I am using Python C API to load some scripts, however, on macOS X I noticed that if the script is including dylib such as import datetime then it would fail:

#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;
}

if success, both tests should display "OK", but this is the actual result:

$ 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

So the question is, how to use PyImport_ImportModule() to import a .dylib/.so library such as datetime in Python C API?

Anyway, the code runs well on Ubuntu 16.04 LTS:

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

Solution

  • Eventually, I figured it out it only happened in Python 2.7, Python 3.5+ doesn't have such an issue.