Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #62928

PyImport_ImportModule(..) returns NULL

Date 2013-12-31 17:22 +0530
Subject PyImport_ImportModule(..) returns NULL
From python satya <pythonsatya@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4754.1388499484.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

PyImport_ImportModule(..) returns NULL python satya <pythonsatya@gmail.com> - 2013-12-31 17:22 +0530

csiph-web