使用多个 C 模块编写 Python 包

发布于 2024-08-19 20:19:00 字数 1201 浏览 5 评论 0原文

我正在编写一个使用 pygame C API 的 Python C 扩展。到目前为止,一切都很好。现在我想知道如何组织我的源代码,以便我可以在包中拥有多个子模块。所有教程都重点关注一个 .c 文件扩展名。我尝试查看一些项目的 setup.py 文件,但它们的复杂性让我大吃一惊,而且我只见树木不见森林。

基本上,我有一个扩展,例如 MyExt。 MyExt有全局函数,有3种类型。我如何组织 PyMethodDef 列表?我必须将所有这些都放在一个列表中吗?另外,我注意到您传递给设置函数的扩展对象实际上是一个模块数组,那么我如何命名这些模块,以便它们都在一个包下并且可以互相看到?

我的 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'])

所以当我打电话时: setup(name = "Some Human Readable Name, Right?", ext_modules = [main_mod]) 我可以将其他模块添加到 ext_modules 列表中,但是我应该将什么作为第一个参数传递给“扩展”?我是否使用像“mypackage.submodule”这样的点分隔字符串?

更一般地说,如何组织具有多个子模块的 C 扩展?如果有人能给我指出一些易于阅读和理解的源代码,那就太好了。多谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡渣熟男 2024-08-26 20:19:00

我认为最简单的方法是在“pure python”中创建包;换句话说,创建 mypackage/,创建一个空的 mypackage/__init__.py ,然后将扩展模块放在 mypackage/module1.somypackage/module2.so 等等。

如果您想要 mypackage 中的内容而不是空的,您可以从 __init__.py 中的另一个扩展模块导入它们。

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.

ゃ人海孤独症 2024-08-26 20:19:00

我不确定我是否明白你想要什么。但是 Python 包有一种命名空间。您可以将模块放在不同的位置,但它们都共享相同的包名称。这里可以参考这个问题How do I create a namespace package在Python 中?

I'm not sure that did I understand what you want. But there is kind of namespace Python package. You can put module in different place, but all of them share same package name. Here you can reference to this question How do I create a namespace package in Python?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文