PyImport_ImportModule(..) returns NULL

P

python satya

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top