【问题标题】:Execute post installation task with pip使用 pip 执行安装后任务
【发布时间】:2019-10-23 00:09:12
【问题描述】:

我的项目树结构

.
├── example.gif
├── funmotd
│   ├── config.json
│   ├── __init__.py
│   └── quotes_db.py
├── LICENSE
├── README.md
└── setup.py

setup.py(删除了一些代码以减少代码)

import sys
import os
import setuptools
from setuptools.command.install import install

class PostInstall(install):
    def run(self):
        mode = 0o666
        bashrc_file = os.path.join(os.path.expanduser('~'), ".bashrc")
        install.run(self)
        # Added CLI to .bashrc
        # Change "config.json" file permission


setuptools.setup(
      ...
      entry_points={'console_scripts': ['funmotd = funmotd:main']},
      package_dir={'funmotd': 'funmotd/'},
      package_data={'funmotd': ['config.json'], },
      include_package_data=True,
      python_requires=">=3.4",
      cmdclass={'install': PostInstall, },
      ...      
)

当我运行python3 setup.py install 时,PostInstall 运行良好。所以,像下面这样上传到Pypi(来自this doc

$ python3 setup.py bdist_wheel
# Created "dist", "funmotd.egg-info" and "build" dirs
$ twine upload dist/*

但是当我运行 pip install funmotd 时,PostInstallNOT 执行的,我看到 dist/* 就像静态编译的东西。 当我运行pip install funmotd 时,有什么技巧可以运行安装后任务。或者如何让setup.pypip 处执行。

我遵循以下问题,没有得到我需要的解决方案

PS:我不希望用户克隆 repo 并运行 python setup.py install。想让它变得简单pip install funmotd

UDPATE1

似乎已经有issue on github,这是长线程

【问题讨论】:

    标签: python-3.x pip setuptools pypi python-wheel


    【解决方案1】:

    pip 不会从轮子中运行 setup.py,因此您不能在轮子中从 setup.py 运行任何安装后代码。

    setup.py 用于构建轮子或在安装源分发 (sdist) 期间使用。因此,如果您希望安装后脚本停止将轮子上传到 PyPI,只需发布​​源分发 (python3 setup.py sdist)。然后pip install funmotd 将运行来自setup.py 的代码。

    【讨论】:

    • 即使在上传 sdist 时,带有现代版本 pip 的 pip install funmotd 也会调用 setup.py bdist_wheel 然后安装轮子。这应该仍然有效,因为 bdist_wheel 调用 install,但它将是预安装而不是安装后
    • @phd “仅发布源分发”是指从 repo 克隆并运行?或任何其他方式?没听懂。
    • @Veerendra 表示运行python3 setup.py sdist(和 bdist_wheel)后跟twine upload dist/*
    • 请注意,您首先需要从dist 文件夹remove the uploaded .whl files from PyPI 中删除所有.whl 文件,甚至在尝试再次安装时使用pip install --no-cache-dir funmotd 以避免任何可能的轮子缓存在您的系统上。 (或者,更好的是,只是增加版本号并避免所有这些)。
    • 谢谢。我也好奇地上传了旧文件。我搞砸了发布。大声笑
    【解决方案2】:

    您需要先调用父运行,然后再执行PostInstall 的其余部分,您可以试试吗:

    class PostInstall(install):
    def run(self):
        install.run(self)
        mode = 0o666
        bashrc_file = os.path.join(os.path.expanduser('~'), ".bashrc")
        # Added CLI to .bashrc
        # Change "config.json" file permission
    

    希望这能解决问题 - 我自己也遇到过非常相似的问题,在本地工作,但不是通过 pip。

    【讨论】:

    • 感谢您的回答。我刚检查过。没用。
    • 嗯,你有没有收到任何警告?
    • pip install -v ...
    猜你喜欢
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 2012-07-20
    相关资源
    最近更新 更多