How to include an .exe in a package and run it from a py script?

I have a python script that calls an external .exe program and captures the output using subprocess.run. The script needs to run in an environment where all dependencies need to be covered in the script itself or in python packages. I’m able to build a package that includes the .exe using manifest.in, but how do I reference the .exe in the python script?
My manifest.in:
include src/kubectl.exe

1 Like

Hi Gary, I think you can use importlib.resources (or its backport) to locate the file inside the package (e.g. via as_file).

Then you can use subprocess.run to run this file.

(In theory, subprocess.run should share the same environment variables with the spawned process, so if you activated a virtualenv with the dependencies installed, this should be visible right?)

1 Like

Also, if you’re specifying it in MANFIEST.in, make sure you set include_package_data = True if using Setuptools (or the equivalent for other build backends), so the .exe gets bundled into the wheel.

And of course, make sure you have and follow the appropriate license to distribute the external binary.

2 Likes

Gary, there is some documentation about that in Data Files Support - setuptools 60.9.3.post20220314.post-20220314 documentation. Please feel free to post in the setuptools GitHub discussions if you have problems with that.

1 Like

This is all very helpful, thank you!