调用“pip install"时运行自定义任务 [英] Run custom task when call `pip install`

查看:37
本文介绍了调用“pip install"时运行自定义任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的 python 包pip installable".问题是该包具有必须在用户的 init shell 脚本中获取的 shell 脚本(例如 .bashrc).

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

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

setup.py

from distutils.command.install import install类自定义安装(安装):定义运行(自我):安装.运行(自我)# 自定义的东西在这里do_my_stuff()setup(..., cmdclass={'install': CustomInstall})

外壳

$ pip install dist/mypackage.tar.gz解压 ./dist/mypackage.tar.gz从 file:///path/to/mypackage/dist/mypackage.tar.gz 为包运行 setup.py egg_info安装收集的包:mypackage为 mypackage 运行 setup.py install用法:-c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]或:-c --help [cmd1 cmd2 ...]或:-c --help-commands或:-c cmd --help错误:无法识别选项 --single-version-externally-managed命令/path/to/.virtualenvs/myvirtualenv/bin/python -c "import setuptools;__file__='/tmp/pip-OFjrqU-build/setup.py';exec(compile(open(__file__).read) 的完整输出().replace('
', '
'), __file__, 'exec'))" install --record/tmp/pip-s4Yo4d-record/install-record.txt --single-version-外部管理的 --install-headers/path/to/.virtualenvs/myvirtualenv/include/site/python2.7:用法:-c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]或:-c --help [cmd1 cmd2 ...]或:-c --help-commands或:-c cmd --help错误:无法识别选项 --single-version-externally-managed----------------------------------------命令/path/to/.virtualenvs/myvirtualenv/bin/python -c "import setuptools;__file__='/tmp/pip-OFjrqU-build/setup.py';exec(compile(open(__file__).read().replace('
', '
'), __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 失败,错误代码 1 在/tmp/pip-OFjrqU-build在/path/to/myhome/.pip/pip.log 中存储完整的日志

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

解决方案

您可以尝试使用 from setuptools.command.install import install 而不是 distutils 吗?>

I want to make my python package "pip installable". The problem is that the package has shell script that must be sourced in the user's init shell script (e.g. .bashrc).

But after the installation, the user don't exactly know where the script went (presumably /usr/bin, but we can't garantee). Of course the user can runs which myscript.sh and manually edits his init script.

But I want to automate this step. I can create a new distutils command, but pip install doesn't call it. And I can extend distutils.command.install.install, but the installation breaks via pip (although works via 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('
', '
'), __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('
', '
'), __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

What is the best aproach to run a custom task after install a package via pip?

解决方案

Could you try with from setuptools.command.install import install instead of using distutils?

这篇关于调用“pip install"时运行自定义任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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