【问题标题】:How to perform custom build steps in setup.py?如何在 setup.py 中执行自定义构建步骤?
【发布时间】:2013-01-04 16:52:58
【问题描述】:

distutils 模块允许将资源文件与 Python 模块一起包含和安装。如果在构建过程中需要生成资源文件,如何正确包含它们?

例如,该项目是一个 Web 应用程序,其中包含应编译为 JavaScript 并包含在 Python 包中的 CoffeeScript 源代码。有没有办法将其集成到正常的 sdist/bdist 进程中?

【问题讨论】:

    标签: python setuptools distutils


    【解决方案1】:

    我花了很长时间才弄清楚这一点,各种建议以各种方式被破坏 - 它们破坏了依赖项的安装,或者它们在 pip 中不起作用等。这是我的解决方案:

    在 setup.py 中:

    from setuptools import setup, find_packages
    from setuptools.command.install import install
    from distutils.command.install import install as _install
    
    class install_(install):
        # inject your own code into this func as you see fit
        def run(self):
            ret = None
            if self.old_and_unmanageable or self.single_version_externally_managed:
                ret = _install.run(self)
            else:
                caller = sys._getframe(2)
                caller_module = caller.f_globals.get('__name__','')
                caller_name = caller.f_code.co_name
    
                if caller_module != 'distutils.dist' or caller_name!='run_commands':
                    _install.run(self)
                else:
                    self.do_egg_install()
    
            # This is just an example, a post-install hook
            # It's a nice way to get at your installed module though
            import site
            site.addsitedir(self.install_lib)
            sys.path.insert(0, self.install_lib)
            from mymodule import install_hooks
            install_hooks.post_install()
            return ret
    

    然后,在调用 setup 函数时,传递 arg:

    cmdclass={'install': install_}
    

    您可以使用相同的想法进行构建而不是安装,为自己编写一个装饰器以使其更容易等。这已通过 pip 进行测试,并直接调用“python setup.py install”。

    【讨论】:

      【解决方案2】:

      最好的方法是编写一个自定义的 build_coffeescript 命令并使其成为 build 的子命令。其他类似/重复问题的回复中提供了更多详细信息,例如这个:

      https://stackoverflow.com/a/1321345/150999

      【讨论】:

      猜你喜欢
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多