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

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

问题描述

我想使我的python包"pip可安装".问题在于该程序包具有必须来自用户的初始Shell脚本中的Shell脚本(例如.bashrc).

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).

但是在安装之后,用户并不完全知道脚本的去向(大概是/usr/bin,但是我们不能保证).当然,用户可以运行which myscript.sh并手动编辑其初始化脚本.

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.

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

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})

外壳

$ 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安装软件包后,运行自定义任务的最佳方法是什么?

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

推荐答案

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

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

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

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