8

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

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

Veerendra K
  • 2,145
  • 7
  • 32
  • 61

2 Answers2

7

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.

phd
  • 82,685
  • 13
  • 120
  • 165
  • 3
    Even when uploading a sdist, `pip install funmotd` with a modern version of pip will call `setup.py bdist_wheel` and then install the wheel. This should still work since `bdist_wheel` invokes `install`, but it will be pre-install rather than post-install – Kyle Willmon Jun 07 '19 at 14:06
  • @phd "only release source distribution" means clone from repo and run? or any other way? didn't get it. – Veerendra K Jun 07 '19 at 14:12
  • 2
    @Veerendra It means running `python3 setup.py sdist` (and *not* `bdist_wheel`) followed by `twine upload dist/*` – Kyle Willmon Jun 07 '19 at 14:14
  • 3
    Note that you will first need to delete any `.whl` files from the `dist` folder, [remove the uploaded .whl files from PyPI](https://stackoverflow.com/questions/20403387/how-to-remove-a-package-from-pypi) and even use `pip install --no-cache-dir funmotd` whenever you try installing it again to avoid any wheels that may be cached on your system. (Or, better yet, just bump the version number and avoid all of this). – Kyle Willmon Jun 07 '19 at 14:16
  • thanks. I uploaded old files too with curiosity. I messed up a release. lol – Veerendra K Jun 07 '19 at 14:33
-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.

Nick H
  • 1,081
  • 8
  • 13