I keep writing these tiny utilities for myself in Python and while I love writing in Python, I definitely don’t enjoy the little war dance I have to do everytime I want to run it on a new machine. Keeping track of virtual environments, and then installing packages in them, quickly gets tiresome. I want to just run the program once I’m done with it. Like a C program. Or Rust. Or Go.
I could go learn one of those languages. Sure. That’s an option.
And I see no problem doing it for other needs (like if I’m working with a group that uses said language) or for my career’s sake or just for being a polyglot.
But when I write for myself, I’d rather stick to Python.
Its mental model fits my brain. I find writing Python joyful.
So what’s a boy to do?
Since I was writing CLI applications, I went and looked for how to write Python CLIs and I found
argparse and Typer and Click and I was confused. They didn’t seem to give me my executable. They had all sorts of other powerful features, but the one I wanted.
And I went looking some more. And the penny dropped.
What I wanted, wasn’t a way to build a CLI, (although that is what I was writing).
What I wanted, was a way to bundle my environment into an executable.
What I wanted, I learned, was a way to freeze my code.
More searching led to Nuitka, PyOxidizer, PyInstaller and their ilk.
I went with Nuitka, because that is what I found first and what I found was dead simple
python -m nuitka --static-libpython=no mastodon-to-moi.py
I got myself an executable
And then I got greedy and did a
mastodon-to-moi.bin --help and figured, that’s where the cli/tui frameworks like Typer and Textualize, come in handy. To scaffold and help these interfaces.
But when I ran it on my laptop, which has nothing other than system Python, it hiccoughed. I tried Pyinstaller next, and that too, did not do the trick. It choked on a loop, that worked when I ran it as with Python normally. PyOxidizer scared me off.
This needs looking further looking into, but atleast I know what I need to do now
Afterthought: The one con I do see, is losing platform independence. My binaries will be Linux on Intel only.1 I could just take my Python source anywhere, set up my environment and go. Well, I still have that option if I need it and in the meanwhile, having binaries affords me a lot of convenience.
Feedback on this post? Mail me at firstname.lastname@example.org
P.S. Subscribe to my mailing list!
Forward these posts and letters to your friends and get them to subscribe!
P.P.S. Feed my insatiable reading habit.
until I learn to cross compile that is. ↩︎