I am porting some Python to C, exposing a few functions back to Python via CPython. However, some of the C still needs to call a function from a Python module.
Very specifically, what I need to do is import Pyglet.graphics.Batch so I can use it in my C code.
I know how to call Python functions using the CPython API, and how to coax out the data I need from any return, but I am failing to find out how to import a Python module to CPython (up to now, my experience has been defining callables via CPython and then calling them via CPython and/or Python).
I’m sure this is silly and easy, but for some reason the answer eludes me.
Thank you very much! That set me on the right track, and with a good example (actually almost the exact code I need is in the time_strptime function in timemodule.c.
I don’t know why it can be so difficult to google this stuff sometimes… It seems like a search phrase such as ‘“CPython” import module’ should have given me this as a top result, but instead the whole first page is filled with results about how to import modules in Python and it generally ignores the quotes I put around CPython…That, of course, wasn’t the only one I tried, but the results were similar no matter how I approached it.
It’s a very well documented API but google doesn’t seem to want to search it very well.
Indeed, it seems that where you say “CPython”, you want to say “Python/C API”. (Or on Python-specific forums like this one, just “C API”).
CPython is the whole interpreter, which includes the Python-language APIs as well.
Well, if it did, I clearly overlooked it. Also, google results tend to be tailored these days. Since I’m a veteran Python programmer, it probably gets confused now that I’m suddenly searching for C-related information. It also generally struggle to distinguish between C, C++, and C#…Understandable, but it seems to like to ignore what i put in hard quotes a lot of the time, too.
Very good tip. Thank you. This will likely eliminate my need to post any more super noob questions lol!
Edit: I did a few tests on questions I struggled with in the past, and the results are definitely more clear and accurate. Modifying my above search, the API documentation was the first result!