【发布时间】:2015-03-31 10:04:18
【问题描述】:
是否可以使用__init__.pyx(编译为__init__.so)创建Python 2.7 包?如果有怎么办?我没有任何运气让它工作。
这是我尝试过的:
-
setup.py:#!/usr/bin/env python from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext foo = Extension(name='foo.__init__', sources=['foo/__init__.pyx']) bar = Extension(name='foo.bar', sources=['foo/bar.pyx']) setup(name='foo', packages = ['foo'], cmdclass={'build_ext':build_ext}, ext_modules = [foo, bar]) -
foo/__init__.pyx:import foo.bar cpdef hello_world(): print "hello world" foo.bar.blah() -
foo/bar.pyx:cpdef blah(): print "blah"
上面有以下行为:
$ python -c 'import foo; foo.hello_world()'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named foo
我看到Python issue #15576 已被this Hg commit 修复。查看 Python Hg 存储库的 Git mirror 中的 equivalent Git commit,我发现可以从 Python v2.7.5 标记(以及所有后续的 v2.7.x 版本)访问提交。有回归吗?
【问题讨论】:
-
只是出于好奇:你为什么要这样做?
标签: python python-2.7 cython python-import python-c-extension