Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:15:40+00:00 2026-06-13T10:15:40+00:00

I was going through the Python documentation regarding Extending Python with C/C++ , and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:15:41+00:00Added an answer on June 13, 2026 at 10:15 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Going through Javascript documentation, I found the following two functions on a Javascript object
Going through happstack-lite tutorial : we build functions that have return type of ServerPart
While going through SWig generated wrappers, I find that the PInvokes don't have any
I have recently been going through some of our windows python 2.4 code and
i was going through one of the python recipes in active state and found
Today is my first day at Python and have been going through problems. One
I'm new to python and have been going through some tutorials on log parsing
I have been trying to understand python metaclasses, and so have been going through
While I'm going through Python code and seeing functions called, I notice things like
I am studying Python and currently going through some more learning with dictionaries. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.