setuptoolssetup.pypython-packagingpyproject.toml

Distribute shell scripts using setuptools and pyproject.toml


I'm trying to distribute a shell script along with a Python package. Ideally, the shell script is installed when I run pip install my_package. I read from this SO that, my expected behavior is exactly what the scripts keyword of setuptools.setup provides. E.g. the script my_script will be installed with the following setup.py script:

setup(
    ...
    scripts=['my_script'],
    ... 
) 

However, I cannot use the above method for two reasons:

  1. the official doc did not mention this behavior. I don't know if I can continue to do this way.
  2. my whole project is built on pyproject.toml, without setup.py. Although pyproject.toml has provided a [project.scripts] table, as explained in the setuptools official doc, the scripts can only be python functions instead of shell scripts.

For completeness, in my case, the shell script reads git status and sets environment variables, which will be read from within my python project. The shell script and my python project are bonded so tightly that I would rather not split them into two projects.

I have also tried to use a python function to execute the shell script, e.g.

[project.scripts]
my_script = 'my_project:my_func'
def my_func():
    subprocess.run(...)

The problem with this solution is that every time I run my_script, my_project is loaded and the loading process is really slow.


Solution

  • I'm not exactly sure it will work for you case, but I solved this by creating a "shim" setup.py file (it has an added benefit of being able to install your project in edit mode).

    It usually just calls setup(), but it was possible to pass the scripts argument:

    """Shim setup file to allow for editable install."""
    
    from setuptools import setup
    
    if __name__ == "__main__":
        setup(scripts=["myscript"])
    

    Everything else was loaded from pyproject.toml.