1

I would like, when I install my python package with pip install ., that the command pre-commit install be run as well as everything else in the setup file.

Here is my setup.py file where I try to execute this:

from distutils.core import setup
from distutils.command.build import build as _build
from setuptools import find_packages, Command
import subprocess



class InstallPreCommit(Command):
    def run(self):
        subprocess.run(['pre-commit', 'install'])


# This class handles the pip install mechanism.
class build(_build):
    sub_commands = _build.sub_commands


setup(
    name="my-pkg",
    version="0.0.1",
    packages=find_packages(),
    install_requires=['pre-commit'],
    py_modules=["pkg"],
    cmdclass={
        'build': build,
        'install-pre-commit': InstallPreCommit
        }
)

However, when I run this, pre-commit install does not get run. I'm mostly taking inspiration from this SO post and this setup.py file in Apache Beam.

Does anyone have a sense of how to make sure I am invoking the setup of my package as well as calling my custom command, which runs the pre-commit install command?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Katya Willard
  • 2,152
  • 4
  • 22
  • 43
  • From https://stackoverflow.com/a/38422349/7976758 (found in https://stackoverflow.com/search?q=%5Bpip%5D+post-install+script): "*The solution below only works when installing a source distribution zip or tarball, or installing in editable mode from a source tree. It will not work when installing from a binary wheel…*" So either try `pip install -e .` (but learn what it means). or `python setup.py sdist && pip install dist/my-pkg-$VERSION.tar.gz` – phd Feb 11 '22 at 22:45

1 Answers1

3

you don't actually want to do this, and the packaging system tries very hard to prevent you from doing this.

the main hurdles:

  • pip usually only runs setup.py once and then caches the result as a wheel
  • pip will sometimes build your package in a directory unrelated to your git repository
  • sometimes the library or tool won't be available (ordering) at the time of installation

and last of all, something that does this subverts the expectations of the python community -- that installation should not have side-effects outside of making the package available

ignoring all of that, you can trudge onwards and hack around the bits that attempt to prevent you

the rough stages for that are:

  1. prevent wheeling of your package -- during the attempted wheeling make sure to exit, pip will usually fall back to just installing without wheeling
  2. attempt to find the original working directory (either $PWD or via assumptions of the running executable
  3. attempt to find the git repository you're running in
  4. and lastly, make sure the tool is available

I've done all the dirty work for you, and I would strongly recommend not doing this -- https://github.com/pre-commit/pre-commit-installed


disclaimer, I created pre-commit, I also created pre-commit-installed but purely as a joke / proof of concept

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • 1
    I appreciate your feedback, thank you. I won't do this. I have decided to add it as instruction in the README.md for the developers. – Katya Willard Feb 14 '22 at 14:40