I was trying to search up how pip install black creates a black.exe on windows, and stumbled upon easy_install.py in setuptools, and exemaker that do similar stuff, but I’m still unable to figure out how I can do what the pip install command is doing in my own project.
If you do that, it’s simple to generate executable and .sh entry points automagickally, when your project is built for distribution using the [project.scripts] table, leaving the hard work to the build back end:
Setuptools does the job fantasticall too.
The details of how to produce a basic Windows .exe involve a bit of xml, and are inspectable in pip’s source code
I know, but I’m trying to do this dynamically. Essentially mimicking one of these build backends myself to create a console script from a python entrypoint.
You can adjust entries in pyproject.toml marked as dynamic with a build plugin script. It’s admittedly easier for me to say than for you to do, as I’ve already done it. But it was surprisingly straightforward.
I’m not trying to create a standalone executable. Simply a wrapper .exe that runs a Python command. Much like what a python file with a #!/usr/bin/env python shebang would do on UNIX.
You can imagine my usecase to be as follows: User supplies a python command, like python -m foo, and the program outputs a command.exe, which when ran, simply executes python -m foo.
So setuptools ships these compiled cli.exe, cli-64.exe and so on binaries. The way they work is by renaming the binary, and by providing a script alongside it, such as:
Rename cli.exe to myapp.exe
Create a myapp-script.py file, with a shebang:
#!/path/to/python
print("Hello world")
Now if you run myapp.exe (or just myapp if it is in PATH), you’ll get Hello world.
This is the source code for building cli.exe: launcher.c
Well done for solving it already, but all the above could be done in code that runs in a build environment, that builds a skeleton setup around the user’s foo script or command.
As I said, this is not being done as a build step. This is what I want to happen at runtime, as it’s the user that will provide the .py file (for example).
Thanks for the help though, ended up digging through the test suites of pip to stumble upon this.
You’re welcome. Maybe my suggestion wasn’t compatible with what you had in mind, but I’ve never got the impression any of the “all in one” exe bundlers work fantastically well (where as pipx say, does ), so that’s just how I’d prefer to approach it.
To be fair, the exe bundlers are generally doing almost the same thing that the Pip/Setuptools toolchain does - they just also provide a Python interpreter in their bundle (and some of them have a self-extracting zip alternative). Pipx is again similar, but the other way around: starting with a full (sort of) Python (virtual) environment, and then adding the project into that.
Aside: The documentaton for your executable project mentions python setup.py sdist bdist_wheel, but this is deprecated and should not be used, see page linked below: