Why do I need to explicitly add os.getcwd() to sys.path when importing Python scripts from a C application?

StackOverflow https://stackoverflow.com/questions/13078025

  •  14-07-2021
  •  | 
  •  

Domanda

I was going through the Python documentation regarding Extending Python with C/C++, and I found a very interesting scenario. Consider that I have the following, trivial Python script named test_script.py in my working directory:

import time
def My_Python_Func():
    print "My_Python_Func() Called: " + str(time.time())

In the same directory, I have a file called another_test_script.py, which has:

import test_script
test_script.My_Python_Func()

Which works fine, when called as python ./another_test_script.py. Now, I'm trying to call said function from a C environment linked with the Python libraries as follows:

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

PyObject *pName, *pModule, *pDict, *pFunc, *pVal;

int main()
{
    const char* script_name = "test_script";
    const char* func_name   = "My_Python_Func";

    Py_Initialize();

    pName = PyString_FromString(script_name);

    PyRun_SimpleString("import os");
    PyRun_SimpleString("import sys");
    /* * * * IF I COMMENT THE FOLLOWING LINE OUT, pModule IS ALWAYS SET TO NULL * * * */
    PyRun_SimpleString("sys.path.insert(0, os.getcwd())");
    pModule = PyImport_Import(pName);

    Py_DECREF(pName);
    if (pModule != NULL)
    {
        pFunc = PyObject_GetAttrString(pModule, func_name);
    }
    pDict = PyModule_GetDict(pModule);
    pFunc = PyDict_GetItemString(pDict, func_name);
    if (PyCallable_Check(pFunc))
    {
        PyObject_CallObject(pFunc, NULL);
        Py_DECREF(pModule);
        Py_DECREF(pName);
    }
    else
    {
        PyErr_Print();
    }
    Py_Finalize();
    return 0;
}

As noted in the comments in the above C program, commenting out the line containing PyRun_SimpleString("sys.path.insert(0, os.getcwd())") causes the call to PyImport_Import to fail (returning NULL). Furthermore, calling PyRun_SimpleString("import test_script") appears to behave the same way.

Why do I need to manually add the current working directory to Python's sys.path list of strings, when simply importing it from another Python script in the same working directory? Furthermore, why does Python not search the current working directory in the first place (as os.getcwd() returns the correct path)? Is this an appropriate solution, if I wish to import functions from scripts bundled with my C application?

È stato utile?

Soluzione

Adding the script directory to sys.path is a peculiarity of the python executable; it is not done by default when the interpreter is initialized, since it is not always appropriate to do so in embedded applications.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top