使用distutils分发预编译的python扩展模块

Toj*_*oji 16 python distutils python-3.x

今天快点:我正在学习Pythons distutils库的内外版本,我想在我的包中加入一个python扩展模块(.pyd).我当然知道推荐的方法是让distutils在创建包时编译扩展,但这是一个相当复杂的扩展,涵盖了许多源文件并引用了几个外部库,所以它需要一些重要的播放来获取所有内容工作正常.

与此同时,我有一个已知的工作版本的扩展来自Visual Studio,并希望在安装程序中使用它作为临时解决方案,让我专注于其他问题.但是,我无法将其指定为模块,因为那些显然必须具有明确的.py扩展名.我怎么能在setup.py中指出我想要包含一个预编译的扩展模块?

(Python 3.1,如果重要的话)

Kev*_*yth 6

我通过覆盖Extension.build_extension解决了这个问题:

setup_args = { ... }
if platform.system() == 'Windows':
    class my_build_ext(build_ext):
        def build_extension(self, ext):
            ''' Copies the already-compiled pyd
            '''
            import shutil
            import os.path
            try:
                os.makedirs(os.path.dirname(self.get_ext_fullpath(ext.name)))
            except WindowsError, e:
                if e.winerror != 183: # already exists
                    raise


            shutil.copyfile(os.path.join(this_dir, r'..\..\bin\Python%d%d\my.pyd' % sys.version_info[0:2]), self.get_ext_fullpath(ext.name))

    setup_args['cmdclass'] = {'build_ext': my_build_ext }

setup(**setup_args)
Run Code Online (Sandbox Code Playgroud)