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

bor*_*ges 13 python distutils pip

我想让我的python包"可以安装pip".问题是该软件包具有必须在用户的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

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

setup(..., cmdclass={'install': CustomInstall})
Run Code Online (Sandbox Code Playgroud)

贝壳

$ 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
Run Code Online (Sandbox Code Playgroud)

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

Alo*_*hal 12

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

  • 我遇到了与[here]相同的问题(http://stackoverflow.com/questions/19569557/pip-not-picking-up-a-custom-install-cmdclass?lq=1) - 我得到了自定义行为当运行`python setup.py install`时,但不是当我运行`pip install <path>`时.有什么想法吗? (5认同)
  • 我有同样的问题.```pip install -e .```运行命令类,然后当我执行bdist_wheel时它运行但是在pip安装期间没有任何反应. (5认同)