使用 Python setuptools 的安装后脚本 [英] Post-install script with Python setuptools

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

问题描述

是否可以将安装后的 Python 脚本文件指定为 setuptools setup.py 文件的一部分,以便用户可以运行命令:

Is it possible to specify a post-install Python script file as part of the setuptools setup.py file so that a user can run the command:

python setup.py install

在本地项目文件存档上,或

on a local project file archive, or

pip install <name>

对于 PyPI 项目,脚本将在标准 setuptools 安装完成时运行?我希望执行可以在单个 Python 脚本文件中编码的安装后任务(例如,向用户提供自定义安装后消息,从不同的远程源存储库中提取其他数据文件).

for a PyPI project and the script will be run at the completion of the standard setuptools install? I am looking to perform post-install tasks that can be coded in a single Python script file (e.g. deliver a custom post-install message to the user, pull additional data files from a different remote source repository).

我遇到了这个SO几年前的答案 解决了该主题,听起来好像当时的共识是您需要创建一个安装子命令.如果情况仍然如此,是否有人可以提供一个示例来说明如何执行此操作,以便用户无需输入第二个命令即可运行脚本?

I came across this SO answer from several years ago that addresses the topic and it sounds as though the consensus at that time was that you need to create an install subcommand. If that is still the case, would it be possible for someone to provide an example of how to do this so that it is not necessary for the user to enter a second command to run the script?

推荐答案

注意:以下解决方案仅适用于安装源代码分发 zip 或 tarball,或以可编辑模式从源树安装时.从二进制轮 (.whl) 安装时,它不起作用

Note: The solution below only works when installing a source distribution zip or tarball, or installing in editable mode from a source tree. It will not work when installing from a binary wheel (.whl)

这个解决方案更透明:

您将对 setup.py 进行一些添加,并且不需要额外的文件.

You will make a few additions to setup.py and there is no need for an extra file.

您还需要考虑两种不同的后安装;一种用于开发/可编辑模式,另一种用于安装模式.

Also you need to consider two different post-installations; one for development/editable mode and the other one for install mode.

将这两个包含安装后脚本的类添加到setup.py:

Add these two classes that includes your post-install script to setup.py:

from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.install import install


class PostDevelopCommand(develop):
    """Post-installation for development mode."""
    def run(self):
        develop.run(self)
        # PUT YOUR POST-INSTALL SCRIPT HERE or CALL A FUNCTION

class PostInstallCommand(install):
    """Post-installation for installation mode."""
    def run(self):
        install.run(self)
        # PUT YOUR POST-INSTALL SCRIPT HERE or CALL A FUNCTION

并将cmdclass 参数插入到setup.py 中的setup() 函数中:

and insert cmdclass argument to setup() function in setup.py:

setup(
    ...

    cmdclass={
        'develop': PostDevelopCommand,
        'install': PostInstallCommand,
    },

    ...
)

你甚至可以在安装过程中调用 shell 命令,就像这个例子中的预安装准备:

You can even call shell commands during installation, like in this example which does pre-installation preparation:

from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.install import install
from subprocess import check_call


class PreDevelopCommand(develop):
    """Pre-installation for development mode."""
    def run(self):
        check_call("apt-get install this-package".split())
        develop.run(self)

class PreInstallCommand(install):
    """Pre-installation for installation mode."""
    def run(self):
        check_call("apt-get install this-package".split())
        install.run(self)


setup(
    ...

P.S.setuptools 上没有任何预安装入口点.如果您有疑问,请阅读此讨论为什么没有.

P.S. there are no any pre-install entry points available on setuptools. Read this discussion if you are wondering why there is none.

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

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