1

Q: In creating a python distribution using setup.py, how can I define Python code that will be run by pip at installation time (NOT at build time!) and runs on the installation target machine (NOT on the build machine!)

I have spent the past seek searching the web for answers, reading contradictory documentation pages, and viewing multiple videos about setup.py. But in all this research can't find even one working example of how install time tasks can be specified.

Can someone point me at a complete working example?

Background: I am writing Python code for an application that is controlling a specialized USB peripheral my company is making, the processor where this will be installed is embedded/bundled with the peripheral and control software.

What's Needed: During the installation of the controlling application, I need the installing program (pip?) to write a configuration file on the install target machine. This file needs to include machine specific information about the target machine acquired using calls to functions imported from Lib/platform.py.

What I tried: Everything I've tried so far either runs at build time on the build machine (when setup.py runs and thus picks up the WRONG information for the target machine), or it merely installs the code I want to run on the target, but does not run it. It thus requires manual intervention by the user after the pip installation but prior to attempting to run the program they think they just installed, to run the auxiliary program that creates the installation config file. Only after this 2 step process can the user actually run the installed (and now properly configured) application.

Source code: Sorry. All my failed attempts to put functions in setup.py (which only run on the build machine, at build time) would only further confuse any readers and encourage more misleading wild goose chases down pointless rat holes.

mcgregor94086
  • 1,467
  • 3
  • 11
  • 22
  • 1
    Short answer: you can't and it's bad practice. For a longer answer I think there are some already on this site. But as always, there are ways to go around, one of them would be to force to install from source or source distribution (by disabling `bdist_wheel`). – sinoroc Aug 18 '20 at 19:03
  • 1
    https://stackoverflow.com/a/58290113/11138259 – sinoroc Aug 18 '20 at 19:04
  • https://stackoverflow.com/a/58322375/11138259 – sinoroc Aug 18 '20 at 19:05
  • If my users were sophisticated python developers who are comfortable with command line error messages, the link that @sinoroc has provided in the previous comment would have been an interesting solution. Given that they are barely comfortable installing packages from the App Store or Google Play store, that's probably not right for me. But given that install time functions are regarded as bad practice, my workaround is to have my program check for the presence of the necessary configuration file every time the program runs. – mcgregor94086 Aug 20 '20 at 04:17

1 Answers1

1

If my users were sophisticated python developers who are comfortable with command line error messages, the link that @sinoroc has provided in the previous comment would have been an interesting solution.

Given that my users are barely comfortable installing packages from the App Store or Google Play store, the referenced work around is probably not right for me.

But given that install time functions are regarded as bad practice, my workaround is to alter the installed program so that its first action is to check for the presence of the necessary configuration file every time the program runs.

While this checking is seemingly unnecessary after the first run, it consumes only minimal CPU resources and would be more robust if the configuration file is ever accidentally deleted.

mcgregor94086
  • 1,467
  • 3
  • 11
  • 22