Execute post installation task with pip

本小妞迷上赌 提交于 2021-02-19 08:19:16

问题


My Project Tree Structure

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

setup.py(Removed some code in order to have less code)

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

The PostInstall is executing fine when I run python3 setup.py install. So, uploaded to Pypi like below(From this doc)

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

But when I run pip install funmotd, PostInstall is NOT executing, I see that dist/* is like static compiled stuff. Is there any trick to run post installation tasks when I run pip install funmotd. Or how to make setup.py execute at pip.

I Followed below questions, didn't get solution what I need

  • Post-install script with Python setuptools

  • Run custom task when call `pip install`

PS: I don't want users to clone repo and run python setup.py install. Want to make it simple pip install funmotd

UDPATE1

Seems there is already issue on github which is long thread


回答1:


pip doesn't run setup.py from a wheel hence you cannot run any post-installation code from setup.py in a wheel.

setup.py is used to build wheels or used during installation of source distribution (sdist). So if you want post-installation script stop uploading wheels to PyPI, only release source distribution (python3 setup.py sdist). Then pip install funmotd will run code from setup.py.




回答2:


You need to invoke the parent run, first, before executing the rest of your PostInstall, could you try:

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

This hopefully will solve the problem - I've had problems with something very similar myself, works locally, but not through pip.



来源:https://stackoverflow.com/questions/56495087/execute-post-installation-task-with-pip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!