使用 distutils/setuptools 在安装后执行 Python 脚本 [英] Execute a Python script post install using distutils / setuptools

查看:16
本文介绍了使用 distutils/setuptools 在安装后执行 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向 Python distutils 添加安装后任务,如 如何使用简单的安装后脚本扩展 distutils?.该任务应该在安装的 lib 目录中执行一个 Python 脚本.此脚本生成安装包所需的其他 Python 模块.

I'm trying to add a post-install task to Python distutils as described in How to extend distutils with a simple post install script?. The task is supposed to execute a Python script in the installed lib directory. This script generates additional Python modules the installed package requires.

我的第一次尝试如下:

from distutils.core import setup
from distutils.command.install import install

class post_install(install):
    def run(self):
        install.run(self)
        from subprocess import call
        call(['python', 'scriptname.py'],
             cwd=self.install_lib + 'packagename')

setup(
 ...
 cmdclass={'install': post_install},
)

这种方法有效,但据我所知有两个不足:

This approach works, but as far as I can tell has two deficiencies:

  1. 如果用户使用的 Python 解释器不是从 PATH 中选择的,安装后脚本将使用 不同 解释器执行,这可能会导致问题.
  2. 它对空运行等不安全,我可以通过将其包装在一个函数中并使用 distutils.cmd.Command.execute 调用来补救.
  1. If the user has used a Python interpreter other than the one picked up from PATH, the post install script will be executed with a different interpreter which might cause a problem.
  2. It's not safe against dry-run etc. which I might be able to remedy by wrapping it in a function and calling it with distutils.cmd.Command.execute.

我该如何改进我的解决方案?是否有推荐的方法/最佳实践来执行此操作?如果可能,我想避免引入另一个依赖项.

How could I improve my solution? Is there a recommended way / best practice for doing this? I'd like to avoid pulling in another dependency if possible.

推荐答案

解决这些不足的方法是:

The way to address these deficiences is:

  1. sys.executable 获取执行 setup.py 的 Python 解释器的完整路径.
  2. 继承自distutils.cmd.Command的类(例如我们在这里使用的distutils.command.install.install)实现了execute 方法,它以安全方式"执行给定的函数,即尊重空运行标志.

  1. Get the full path to the Python interpreter executing setup.py from sys.executable.
  2. Classes inheriting from distutils.cmd.Command (such as distutils.command.install.install which we use here) implement the execute method, which executes a given function in a "safe way" i.e. respecting the dry-run flag.

但是请注意,--dry-run 选项当前已损坏并且无论如何都不能按预期工作.

Note however that the --dry-run option is currently broken and does not work as intended anyway.

我最终得到了以下解决方案:

I ended up with the following solution:

import os, sys
from distutils.core import setup
from distutils.command.install import install as _install


def _post_install(dir):
    from subprocess import call
    call([sys.executable, 'scriptname.py'],
         cwd=os.path.join(dir, 'packagename'))


class install(_install):
    def run(self):
        _install.run(self)
        self.execute(_post_install, (self.install_lib,),
                     msg="Running post install task")


setup(
    ...
    cmdclass={'install': install},
)

请注意,我对派生类使用了类名 install,因为这是 python setup.py --help-commands 将使用的.

Note that I use the class name install for my derived class because that is what python setup.py --help-commands will use.

这篇关于使用 distutils/setuptools 在安装后执行 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆