python - 调用 `pip install` 时运行自定义任务

标签 python pip distutils

我想让我的 python 包“pip 可安装”。问题是该包具有必须在用户的 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})

外壳

$ 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 吗?

关于python - 调用 `pip install` 时运行自定义任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15853058/

相关文章:

Python 3.6.4 - 是否捆绑了 PIP?

python - 在 64 位 linux 上编译 32 位 python 模块(例如 numpy)

python - pip 无法卸载 <package> : "It is a distutils installed project"

python - 使 collat​​z 程序自动化无聊的事情

python - 如何在 Python 3.8+ 和 Python 2.7+ 中使用 unittest.mock 包?

python - pip 未使用 pip.conf 中定义的额外索引 url

python - 如何打包分发使用共享库的 python 模块?

python - sqlalchemy 中的并发

python - 如何将 PyTorch 张量列表转换为 float 列表?

python - 收集pyHook 找不到满足pyHook要求的版本