# Calling Python from C

This topic is 3811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to include a Python module in my C program and thought I would write a very simple Hello World Python script to test it out. Unfortunately this code does not seem to work. There also seems to be very little in the way of documentation on the specifics. I have read the page regarding this subject on python.org but have not found any sufficient information regarding the problem. Here is the C code:
PyObject *module, *dict, *func, *value;
Py_Initialize();
module = PyImport_ImportModule("logic");
dict = PyModule_GetDict(module);
func = PyDict_GetItemString(dict, "logic");
value = PyObject_CallFunction(func, "HW", "");

Py_DECREF(module);
Py_DECREF(dict);
Py_DECREF(func);
Py_DECREF(value);

Py_Finalize();

And the Python code:
def HW():
print 'Hello World!'


##### Share on other sites
I think it should be:

func=PyDict_GetItemString(dict,"HW");//get the object(function) named "HW"
value=PyObject_CallFunction(func,"");//call the function, with 0 arguments

I suggest you use boost::python though, it makes things a lot more easier, and there's pretty good documentation for it.

##### Share on other sites
Quote:
 Original post by mikemanI suggest you use boost::python though, it makes things a lot more easier, and there's pretty good documentation for it.

The only problem with that is Boost is a C++ library and I am using C.

Thanks for the help although unfortunately after a little closer examination it seems to be crashing on the call to PyModule_GetDict. No idea why though.

##### Share on other sites
You're not checking any return values. Put proper error handling in, and see how that goes.

##### Share on other sites
Quote:
 Original post by KylotanYou're not checking any return values. Put proper error handling in, and see how that goes.

Sorry for the late reply. I've got it working when compiling on the command line but unfortunately it does not work from within Xcode. I guess I'll have to have another look at it to see what the IDE is doing.

Does anyone use Xcode and embed Python modules with C/C++ that could give some advice?

##### Share on other sites
Quote:
 Original post by CromulentSorry for the late reply. I've got it working when compiling on the command line but unfortunately it does not work from within Xcode. I guess I'll have to have another look at it to see what the IDE is doing.
Could be a path thing. If you put proper error handling in, you could see how that would go.

##### Share on other sites
Quote:
Original post by Sneftel
Quote:
 Original post by CromulentSorry for the late reply. I've got it working when compiling on the command line but unfortunately it does not work from within Xcode. I guess I'll have to have another look at it to see what the IDE is doing.
Could be a path thing. If you put proper error handling in, you could see how that would go.

Okay you were right, all working correctly now. I should have checked that first I guess.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013593
×