Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a python module's C API in my own C module

This seems to be sort of what I want, except I want to make an instance of a C PyObject in another C module.

Create instance of a python class , declared in python, with C API

But I am not able to find the documentation for using a Python module from C. I can't seem to find the relevent docs online as everyone just talks about extending Python with C.

My issue is that I have a Pygame which I want to speed up. So I am making a C module which needs access to Pygame. How do I import pygame? I don't want to import two copies, right? The coding part I can figure out, I just don't know how to configure and link the code. I know I must be missing some docs, so if anyone can point me in the right direction, I'd be much obliged.


Update: Sorry I reread my post and realized my wording was terrible.

If you have pygame installed you can look in your Python/include directory and find pygame header files. What are they for? I thought that your C module could access the pygame C module directly, so that your python script AND your C module used the same pygame instance.

Clarification anyone?

like image 582
Dogman Avatar asked Dec 05 '25 13:12

Dogman


1 Answers

The trick is to write the normal Python routine, but in C. Use PyImport_ImportModule(), PyObject_GetAttr(), and PyObject_Call() as appropriate.

like image 190
Ignacio Vazquez-Abrams Avatar answered Dec 08 '25 01:12

Ignacio Vazquez-Abrams