Making python package scripts headless.

Simple

This is a quick one. I had to figure this out a few weeks back and it wasn't stated anywhere explicitly, so I guess I should write a blog post in case I forget it in the future.

I write a lot of Python at work, and it's pretty common knowledge that you can either call pythonw or change a Python file's extension to .pyw instead of .py in order to achieve headlessness (not having a console window open up).

However, I write a bunch of command-line applications using Click or argparse or optpase and then we have packaging that establishes these programs as command-line tools with the setup.py file.

setup(
    ...
    entry_points = {
        'console_scripts': ['entry-point=package_name.file_name:method_name'],
    }
    ...
)

However, when this creates an .exe in the Python package, it calls standard Python.

The way to get a packaged .exe to stay headless is actually rather simple. Just change console_scripts to gui_scripts, like this:

setup(
    ...
    entry_points = {
        'gui_scripts': ['entry-point=package_name.file_name:method_name'],
    }
    ...
)

And now you can call a packaged CLI entry point as a headless application for Python.