使用 pip 执行安装后任务

Vee*_*dra 9 pip setuptools pypi python-3.x python-wheel

我的项目树结构

\n\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 example.gif\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 funmotd\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config.json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 quotes_db.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 LICENSE\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 setup.py\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

setup.py(为了减少代码量,删除了一些代码)

\n\n
import sys\nimport os\nimport setuptools\nfrom setuptools.command.install import install\n\nclass PostInstall(install):\n    def run(self):\n        mode = 0o666\n        bashrc_file = os.path.join(os.path.expanduser(\'~\'), ".bashrc")\n        install.run(self)\n        # Added CLI to .bashrc\n        # Change "config.json" file permission\n\n\nsetuptools.setup(\n      ...\n      entry_points={\'console_scripts\': [\'funmotd = funmotd:main\']},\n      package_dir={\'funmotd\': \'funmotd/\'},\n      package_data={\'funmotd\': [\'config.json\'], },\n      include_package_data=True,\n      python_requires=">=3.4",\n      cmdclass={\'install\': PostInstall, },\n      ...      \n)\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

PostInstall当我运行时,执行得很好python3 setup.py install。因此,上传到Pypi如下(来自此文档

\n\n
$ python3 setup.py bdist_wheel\n# Created "dist", "funmotd.egg-info" and "build" dirs\n$ twine upload dist/*\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是当我运行pip install funmotd,PostInstall执行时,我发现这dist/*就像静态编译的东西。\n当我运行pip install funmotd. 或者如何使setup.py执行 at pip.

\n\n

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

\n\n\n\n

PS:我不希望用户克隆存储库并运行python setup.py install. 想让事情变得简单pip install funmotd

\n\n

UDP日期1

\n\n

似乎github 上已经有一个很长的问题了

\n

phd*_*phd 7

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”,然后安装wheel。这应该仍然有效,因为“bdist_wheel”调用“install”,但它将是预安装而不是安装后 (3认同)
  • 请注意,您首先需要从“dist”文件夹中删除所有“.whl”文件,[从 PyPI 中删除上传的 .whl 文件](/sf/ask/1428237121/ a-package-from-pypi),甚至在您尝试再次安装时使用“pip install --no-cache-dir funmotd”,以避免任何可能缓存在系统上的轮子。(或者,更好的是,只需增加版本号即可避免所有这些)。 (3认同)
  • @Veerendra这意味着运行“python3 setup.py sdist”(并且*不是*“bdist_wheel”),然后运行“twine upload dist/*” (2认同)