编写python扩展模块_嵌套的Python C扩展/模块?

How do I compile a C-Python module such that it is local to another? E.g. if I have a module named "bar" and another module named "mymodule", how do I compile "bar" so that it imported via "import mymodule.bar"?

(Sorry if this is poorly phrased, I wasn't sure what the proper term for it was.)

I tried the following in setup.py, but it doesn't seem to work:

from distutils.core import setup, Extension

setup(name='mymodule',

version='1.0',

author='Me',

ext_modules=[Extension('mymodule', ['mymodule-module.c']),

Extension('bar', ['bar-module.c'])])

Edit

Thanks Alex. So this is what I ended up using:

from distutils.core import setup, Extension

PACKAGE_NAME = 'mymodule'

setup(name=PACKAGE_NAME,

version='1.0',

author='Me',

packages=[PACKAGE_NAME],

ext_package=PACKAGE_NAME

ext_modules=[Extension('foo', ['mymodule-foo-module.c']),

Extension('bar', ['mymodule-bar-module.c'])])

with of course a folder named "mymodule" containing __init__.py.

解决方案

The instructions are here:

Extension('foo', ['src/foo1.c',

'src/foo2.c'])

describes an extension that lives in

the root package, while

Extension('pkg.foo', ['src/foo1.c',

'src/foo2.c'])

describes the same extension in the

pkg package. The source files and

resulting object code are identical in

both cases; the only difference is

where in the filesystem (and therefore

where in Python’s namespace hierarchy)

the resulting extension lives.

Remember, a package is always a directory (or zipfile) containing a module __init__. To create a module that's a package body, that module will be called __init__ and live under the package's directory (or zipfile). I've never done that in C; if it doesn't work to do it directly, name the module e.g. _init instead, and in __init__.py do from _init import * (one of the very few legitimate uses of from ... import *;-).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值