7

When writing a Python package, I know how to specify other required Python packages in the setup.py file thanks to the field install_requires from setuptools.setup.

However, I do not know how to specify external system dependencies that are NOT Python packages, i.e. a commands such as git or cmake (examples) that my package could call via subprocess.call or subprocess.Popen?

Do I have to manually check the availability of the commands in my setup.py file, or is there a fancy way to specify system requirements?

Edit: I just want to be able to check if the external tools are available, and if not invite the user to install them (by themself). I do not want to manage the installation of external tools when installing the package.

Summary of contributions: it seems that setuptools has no support for this, and it would be safer to do the check at runtime (c.f. comments and answers).

Odin
  • 633
  • 4
  • 11
  • 2
    As far as I’m aware setuptools has no support for this (and making this general would be hard), you have to specify such dependencies manually; see for example https://stackoverflow.com/a/37481508/1968 – Konrad Rudolph Sep 22 '20 at 10:05
  • https://stackoverflow.com/a/53981373/7976758 – phd Sep 22 '20 at 10:53
  • 1
    That is not what _setuptools_ is good at. For these kinds of things you should package your code in the format of the Linux distribution (_apt/`.deb`, _yum_/`.rpm`, _pacman_, etc.). – sinoroc Sep 22 '20 at 12:07
  • 1
    Thank you for your answers. Actually, I would just like to check if the commands (e.g. git or cmake) are available on the system, and if not print a message inviting the user to install them. I do not want to completely manage the installation of the external tools. – Odin Sep 23 '20 at 11:08
  • 2
    My recommendation would be to check for those not at _install-time_ but at _run-time_. Either at the start of each run, or maybe at the first run. -- It's true that you could add this to your `setup.py`, but the `setup.py` is not always executed at _install-time_: for example if your project is packaged as a _wheel_ then it doesn't even contain the `setup.py` file at all. And even if you do not distribute your project as a _wheel_, if I am not mistaken _pip_ tends to build a _wheel_ locally anyway for the following installations. -- So I would say _run-time_ is safer. – sinoroc Sep 23 '20 at 11:38
  • @sinoroc ok I understand, thanks for the details. I think you could make an accepted answers out of your comments. – Odin Sep 23 '20 at 12:35

1 Answers1

4

My recommendation would be to check for the presence of those external dependencies not at install-time but at run-time. Either at the start of each run, or maybe at the first run.

It's true that you could add this to your setup.py, but the setup.py is not always executed at install-time: for example if your project is packaged as a wheel then it doesn't even contain the setup.py file at all. And even if you do not distribute your project as a wheel, if I am not mistaken pip tends to build a wheel locally anyway and reuse it for the subsequent installations.

So although it would be possible to do such checks as part of the setup script at install time (provided that you can guarantee the presence and execution of setup.py), I would say run-time is a safer bet.

sinoroc
  • 18,409
  • 2
  • 39
  • 70