将输出文件添加到 Python 扩展

sho*_*app 4 python distutils pip setuptools python-extensions

我已经定义了一个自定义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},
)
Run Code Online (Sandbox Code Playgroud)

将此模块打包并使用 pip 安装后foo,我的 virtualenv 的 site-packages 目录中有一个模块。目录结构如下所示。

foo/
foo/__init__.py
foo/_foo.so
Run Code Online (Sandbox Code Playgroud)

egg-info/SOURCES.txt文件不包括__init__.py我手动创建/移动的文件。当我执行pip uninstall foo命令时,命令会留foo/__init__.py在我的 virtualenv 的站点包中。我希望 pip 删除整个包。如何将__init__.py手动移动到构建目录中的生成文件添加到已安装的输出文件列表中?

我意识到这是令人作呕和骇人听闻的,所以我欢迎令人作呕和骇人听闻的答案!

尝试:

  1. 添加packages=['foo']- 当我这样做时,pip 不会构建扩展。还尝试调整包名称的文件路径/命名空间版本——没有区别。

Éri*_*ujo 5

为了让 distutils 安装一个 Python 包,你需要传递packages=['foo'],如果你把它放在不是项目根级别的地方(我的意思是 setup.py 脚本旁边的 foo 目录),就像你看起来要在这里做,您还必须通过package_dir={'foo': 'Source'}或使用更简单的布局。如果您的 setup.py 脚本包含此packages参数,则 build 命令将调用 build_py 命令将 Python 源文件(和目录)移动到 build 目录,稍后将通过 install 命令复制该目录。

这里的问题是您的foo/__init__.py文件是由 build_ext 命令构建的,该命令在 build_py 之后运行。您需要使用自定义构建命令覆盖它:

class MyBuild(build):
  sub_commands = [('build_clib', build.has_c_libraries),
                  ('build_ext', build.has_ext_modules),
                  ('build_py', build.has_pure_modules),
                  ('build_scripts', build.has_scripts),
                 ]

setup(..., cmdclass={'build': MyBuild, 'build_ext': MyBuildExt})
Run Code Online (Sandbox Code Playgroud)

sub_commands 属性中的元素是(命令名称,要调用的函数以决定是否运行命令)的元组;这在源代码中有记录,但我不记得文档中是否有解释。在标准构建类中,build_py 先于 build_clib。我可能会在 Python 2.7 的下一个版本中更改这一点,因为据报道它与 2to3 转换交互不佳。