views:

114

answers:

1

Hello,

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!

A: 

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.

mithrandi