【发布时间】:2012-05-11 22:59:54
【问题描述】:
我已经定义了一个自定义 build_ext 来构建一个时髦的扩展,我正试图让它对 pip 友好。以下是我正在做的精简版。
foo_ext = Extension(
name='foo/_foo',
sources=foo_sources,
)
class MyBuildExt(build_ext):
def build_extension(self, ext):
# This standalone script builds the __init__.py file
# and some .h files for the extension
check_call(['python', 'build_init_file.py'])
# Now that we've created all the init and .h code
# build the C/C++ extension using setuptools/distutils
build_ext.build_extension(self, ext)
# Include the generated __init__.py in the build directory
# which is something like `build/lib.linux-x86/foo/`.
# How can I get setuptools/distutils to install the
# generated file automatically?!
generated_file = 'Source/foo/__init__.py'
output_path = '/'.join(self.get_outputs()[0].split('/')[:-1])
self.move_file(generated_file, output_path)
setup(
...,
ext_modules = [foo_ext],
cmdclass={'build_ext' : MyBuildExt},
)
打包这个模块并使用 pip 安装后,我的 virtualenv 的 site-packages 目录中有一个模块 foo。目录结构如下所示。
foo/
foo/__init__.py
foo/_foo.so
egg-info/SOURCES.txt 文件不包括我手动创建/移动的 __init__.py 文件。当我执行pip uninstall foo 时,该命令会将foo/__init__.py 留在我的virtualenv 的站点包中。我想 pip 删除整个包。如何将生成的 __init__.py 文件添加到已安装的输出文件列表中?
我知道这是令人作呕和骇人听闻的,所以我欢迎恶心和骇人听闻的答案!
尝试:
- 添加了
packages=['foo']-- 当我这样做时,pip 不会构建扩展。还尝试调整包名称的文件路径/命名空间版本——没有区别。
【问题讨论】:
标签: python pip setuptools distutils python-extensions