I have a small python application developed on Nix that I wish to distribute to some M$ Windows users. It seems that an executable Python zip archive is an excellent way to achieve this.
However, I would like to include dependencies that are not included with the standard Python installation, e.g. termcolor. Is this possible?
This is the app that I am testing
from termcolor import cprint
print('hello world')
cprint('hola mundo', 'red')
and termcolor is not included in the standard Python implementation. I do not expect the users to pip install.
I have built a .pyz using
python -m zipapp . -o my_app.pyz
and the app is created and it works in a virtualenv with termcolor installed. But of course fails if it is not.
I have also tried
python -m zipapps my_app -o my_app.pyz -r requirements.txt
This creates a .pyz which includes termcolor, but when I run it, it drops into the Python REPL
Is this possible?
You need to provide the
--mainoption. It is best done by creating a main function first.my_app.pyAnd then package with something like that:
Finally this can be run with:
And this
.pyzfile can be moved anywhere (on the same machine) and it will continue to work, as it includes the dependencies (no need for virtual environment). If the dependencies are pure Python only (as it seems to be the case for termcolor), then the.pyzfile can be used with other Python interpreter versions and on other operating systems.Optionally you can also set a shebang with the
--pythonoption.Anyway, if then later you aim for more serious usage, I recommend using pex or shiv for this. These tools will take care of handling many complex corner cases.