【问题标题】:Run custom task when call `pip install`调用“pip install”时运行自定义任务
【发布时间】:2013-03-28 23:56:44
【问题描述】:

我想让我的 python 包“可安装 pip”。问题是这个包有 shell 脚本,它必须来自用户的 init 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吗?
  • @Alok,它成功了!请回答,以便我接受。 :)
  • 完成。很高兴它成功了。

标签: python pip distutils


【解决方案1】:

我更新了代码以使用

点安装

也是。在这个例子中,我们在安装过程中创建了一个目录结构。所有这些都进入 setup.py 文件,然后是 $ pip install . 这个解决方案也回答了这个相关的帖子:Custom pip install commands not running

from setuptools import setup
from setuptools.command.install import install
import pip
import os, errno

my_dir_paths = ['1', '2', '3']

class CustomInstall(install):

    def __init__(self, dist):
        super(install, self).__init__(dist)
        self.__current  = "/home/..."
        self.__post_install(self.__current)
        
    def run(self):
        install.run(self)
        
    def __post_install(self, curr):
        try:
            os.makedirs(os.path.join(curr, "data2"))
        except FileExistsError:
            print("Directory %s already exists!"%("data2"))
            pass

        for path in my_dir_paths:
            try:
                os.makedirs(os.path.join(curr, "data2", path))
            except FileExistsError:
                print("Directory %s already exists!"%(path))
                pass



setup(
    name='some_name',
    version='0.1.0',
    author='me',
    author_email='...',
    url='http://...',
    packages=setuptools.find_packages(),
    license='...',
    description='....',
    long_description=open('README.txt').read(),
    install_requires=[
       "pandas >= 1.0.5",
    ],
    python_requires='>=3.6',
    
    cmdclass={'install': CustomInstall}
    
)

【讨论】:

  • 介意在这里解释一下dist 是什么?是否支持pypi中的pip
  • 用 *args 替换 dist 如果有帮助(不太具体)。没有它,setuptools.command.install 对我不起作用。
【解决方案2】:

你能用from setuptools.command.install import install代替distutils吗?

【讨论】:

  • 谢谢,这真的帮助了我。我关注了 (stackoverflow.com/questions/17806485/…) 并收到了同样的问题,将其记录在 (blog.diffbrent.com/…) 中并给了你道具
  • 我遇到了与 here 相同的问题 - 我在运行 python setup.py install 时得到了自定义行为,但在运行 pip install <path> 时却没有。有什么想法吗?
  • 我也有同样的问题。 pip install -e . 运行命令类,然后在我执行 bdist_wheel 时运行,但在 pip install 期间没有任何反应。
  • @scubbo 你有没有找到解决方案?
  • 我没有,对不起!这是不久前的事了,所以我需要重新理解这个问题 - 但希望其他评论者可以帮助你?
猜你喜欢
  • 2017-03-18
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
相关资源
最近更新 更多