Jump to content
  • Advertisement
Sign in to follow this  
Cromulent

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.

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by mikeman
I 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 this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
You'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 this post


Link to post
Share on other sites
Quote:
Original post by Cromulent
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.
Could be a path thing. If you put proper error handling in, you could see how that would go.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by Cromulent
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.
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!