20

Using from setuptools.command.install import install, I can easily run a custom post-install script if I run python setup.py install. This is fairly trivial to do.

Currently, the script does nothing but print some text but I want it to deal with system changes that need to happen when a new package is installed -- for example, back up the database that the package is using.

I want to generate the a Python wheel for my package and then copy that and install it on a a set of deployment machines. However, my custom install script is no longer run on the deployment machine.

What am I doing wrong? Is that even possible?

Community
  • 1
  • 1
Sardathrion - against SE abuse
  • 17,269
  • 27
  • 101
  • 156

2 Answers2

9

PEP 427 which specifies the wheel package format does not leave any provisions for custom pre or post installation scripts.

Therefore running a custom script is not possible during wheel package installation.

You'll have to add the custom script to a place in your package where you expect the developer to execute first.

wonton
  • 7,568
  • 9
  • 56
  • 93
8

Do not mix package installation and system deployment

Installation of Python packages (using any sort of packaging tools or formats) shall be focused on making that package usable from Python code.

Deployment, what might include database modifications etc. is definitely out of scope and shall be handled by other tools like fab, salt-stack etc.

The fact, that something seems fairly trivial does not mean, one shall do it.

The risk is, you will make your package installation difficult to reuse, as it will be spoiled by others things, which are not related to pure package installation.

The option to hook into installation process and modify environment is by some people even considered flaw in design, causing big mess in Python packaging situation - see Armin Roacher in Python Packaging: Hate, Hate, Hate Everywhere, chapter "PTH: The failed Design that enabled it all"

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
  • 19
    Yes, but you do not offer any alternative! – ankostis Oct 29 '14 at 18:53
  • 1
    @ankostis One simple option would be: your package would offer two scripts: `configure` and `run`. `configure` would do what you expect from post-installation hook. `run` (or whatever else name you give it) would run the app. Your installation process would create virtualenv (recommended to prevent spoiling system Python), install into it the package and run the `configure` script. Feel free to use whatever application prefix for `configure` command you like. – Jan Vlcinsky Oct 30 '14 at 21:49
  • 1
    Thank you. Intensely annoying but at least its a real answer. I've spent days trying to find any information on this issue. – bjorns Sep 01 '15 at 00:43
  • FWIW after running into this very issue after switching to a wheel-based distribution I ended up writing a simple GUI-based configuration tool for my project that does some post-install stuff (in this case the creation of a configuration folder + file to the Windows user directory, and a desktop launcher). As it might be useful to people who ended up here I'll share the link: Note that actual post-install code is in the *post_install* function. Also note that some of the code there is Windows-specific. – johan Oct 25 '17 at 16:39
  • [setuptools entry_points](https://setuptools.readthedocs.io/en/latest/userguide/entry_point.html) has been great for me to implement a `configure` entrypoint within my module. After wheel installation, each entrypoint is created as a shell script in the user path. – Cory Aug 18 '21 at 14:28