调用“pip install”时运行自定义任务

2023-12-22

我想让我的 python 包“pip installable”。问题是该包具有必须源自用户的 init shell 脚本的 shell 脚本(例如.bashrc).

但安装后,用户并不确切知道脚本去了哪里(大概是/usr/bin,但我们不能保证)。当然用户可以运行which myscript.sh并手动编辑他的初始化脚本。

但我想自动化这一步。我可以创建一个新的 distutils 命令,但是pip install不调用它。我可以延长distutils.command.install.install,但是安装通过 pip 中断(尽管可以通过python setup.py install):

setup.py

from distutils.command.install import install

class CustomInstall(install):
    def run(self):
        install.run(self)
        # custom stuff here
        do_my_stuff()

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

shell

$ pip install dist/mypackage.tar.gz
Unpacking ./dist/mypackage.tar.gz
  Running setup.py egg_info for package from file:///path/to/mypackage/dist/mypackage.tar.gz

Installing collected packages: mypackage
  Running setup.py install for mypackage
    usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
       or: -c --help [cmd1 cmd2 ...]
       or: -c --help-commands
       or: -c cmd --help

    error: option --single-version-externally-managed not recognized
    Complete output from command /path/to/.virtualenvs/myvirtualenv/bin/python -c "import setuptools;__file__='/tmp/pip-OFjrqU-build/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-s4Yo4d-record/install-record.txt --single-version-externally-managed --install-headers /path/to/.virtualenvs/myvirtualenv/include/site/python2.7:
    usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
       or: -c --help [cmd1 cmd2 ...]
       or: -c --help-commands
       or: -c cmd --help

error: option --single-version-externally-managed not recognized

----------------------------------------
Command /path/to/.virtualenvs/myvirtualenv/bin/python -c "import setuptools;__file__='/tmp/pip-OFjrqU-build/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-s4Yo4d-record/install-record.txt --single-version-externally-managed --install-headers /path/to/.virtualenvs/myvirtualenv/include/site/python2.7 failed with error code 1 in /tmp/pip-OFjrqU-build
Storing complete log in /path/to/myhome/.pip/pip.log

通过安装包后运行自定义任务的最佳方法是什么pip?


你能尝试一下吗from setuptools.command.install import install而不是使用distutils?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

调用“pip install”时运行自定义任务 的相关文章

随机推荐