PyImport_ImportModule(..) returns NULL

python satya pythonsatya at gmail.com
Tue Dec 31 06:52:12 EST 2013


Hi,
          I am new to python, am trying to use embedding python API's in
C,as below

I have my python file test.py in  "/var/tmp" path and trying to fetch
objects and functions from python script as below, but
PyImport_ImportModule("test") returning NULL always(instead adding /var/tmp
path to syspath)

Please let me know, how to solve this?

$% more /var/tmp/test.py
#!/usr/bin/python

import sys

input = "<dummy/>"

==================================================
#include <stdio.h>
#include <stdlib.h>
#include <Python.h>

int main(void)
{
    const char *scriptDirectoryName = "/var/tmp";
    Py_Initialize();
    PyObject *sysPath = PySys_GetObject("path");
    PyObject *path = PyString_FromString(scriptDirectoryName);
    PyList_Insert(sysPath, 0, path);
    PyObject *pModule = PyImport_ImportModule("test");
    if (pModule == NULL) {
        fp = fopen("/var/tmp/abc.txt", "w");
        fprintf(fp, "Fail to search in /var/tmp path");
        fclose(fp);
    } else {
       /* Read input object ... */
   }

    Py_Finalize();
    return 0;
}

====================================================

$% more /var/tmp/abc.txt
Fail to search in /var/tmp path

Thanks,
Satya
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131231/29c26da5/attachment.html>


More information about the Python-list mailing list