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
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