Setuptools entry_points: invoking OS command

I have a Python package that creates lots of log files. I can delete the log files using the below git command:

git clean -f -x "*MainProcess.log"

I would like to add this as an entry_point console “script” to the package’s I see that entry_point is designed to invoke a Python function inside a module, can it also be used to invoke an OS command? I guess this is sort of like adding a package-specific command line alias.

I am not interested in the workaround, which is to add a function to a Python module that does this:

import os

os.system("git clean -f -x '*MainProcess.log'")

Entry points are specifically for registering Python objects (modules, functions, classes, variables) for discovery by other systems. There’s no way to register an OS command other than your workaround, which is exactly what I would recommend.

1 Like

More specifically, you wouldn’t need to create a whole separate module; rather, you’d just need a function in any existing module that you’d then list under the console_scripts entry_points. Also, the subprocess module, specifically is greatly preferred to the legacy os.system() in modern code.

So, in your package (say spam), you’d add the following function to an appropriate module (say cli):

import subprocess

def clean():["git", "clean", "-f", "-x", "*MainProcess.log"])

Then, in e.g. your setup.cfg, you’d simply add

console_scripts =
    your-clean-command = spam.cli:clean

It might make more sense for your application, however, to make clean a subcommand of your main top-level entrypoint (e.g. spam clean), which you can do with argparse or a CLI library of your choice (e.g. Click).

1 Like