Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing Python packages with multiple C modules

I am writing a Python C extension which uses the pygame C API. So far so good. Now I wonder how I organize my source code so that I can have multiple submodules in the package. All the tutorials out there focus on one .c file extensions. I tried looking at some projects setup.py files but they blew my mind with complexity, and I could not see the forest for the trees.

Basically, I have an extension, say MyExt. MyExt has global functions, and 3 types. How do I go about organizing the PyMethodDef lists? Do I have to put all of them in one list? Alternatively, I noticed that the Extension object you passed to the setup function is actaully an array of modules, so how do I name the modules so that they are all under one package and can see each other?

My setup.py:

main_mod = Extension('modname',
                include_dirs = ['C:\Libraries\Boost\include',
                                'C:\Libraries\SDL\include',
                                'C:\Libraries\SDL_image\include'],

                libraries = ['libSDL',
                             'SDL_image'],

                library_dirs = ['C:\Libraries\SDL\lib',
                                'C:\Libraries\SDL_image\lib'],

                sources = ['main.cpp',
                           'Object1.cpp',
                           'Object2.cpp',
                           'Etcetera.cpp'])

So when I call: setup(name = "Some Human Readable Name, Right?", ext_modules = [main_mod]) I can add other modules to ext_modules list but what do I pass as the first parameter to 'Extension'? Do I use a dot seperated string like 'mypackage.submodule'?

More generally, how do I organize a C extension with multiple submodules? If anyone can point me to some source code which is easy to read and understand, that would be great. Thanks a lot!

like image 942
Anonnobody Avatar asked Feb 02 '10 15:02

Anonnobody


People also ask

How do I use C modules in Python?

To write Python modules in C, you'll need to use the Python API, which defines the various functions, macros, and variables that allow the Python interpreter to call your C code. All of these tools and more are collectively bundled in the Python. h header file.

Can I mix Python with C?

Any code that you write using any compiled language like C, C++, or Java can be integrated or imported into another Python script. This code is considered as an "extension." A Python extension module is nothing more than a normal C library.

Are Python modules written in C?

Most of the Python Libraries are written in the C programming language.

What is C API in Python?

The Python/C API allows for compiled pieces of code to be called from Python programs or executed within the CPython interpreter. This process of producing compiled code for use by CPython is generally known as "extending" Python and the compiled pieces of code to be used are known as "extension modules".


1 Answers

I think the easiest way to do this would be to create the package in "pure python"; in other words, create mypackage/, create an empty mypackage/__init__.py , and then put your extension modules at mypackage/module1.so, mypackage/module2.so, and so on.

If you want things in mypackage instead of it being empty, you can import them from another extension module in your __init__.py.

like image 187
mithrandi Avatar answered Sep 24 '22 02:09

mithrandi