PYTHON PACKAGING AND DISTRIBUTION USING HATCH 1.9 I struggled getting a simple Python CLI application properly packaged for distribution. Now that I've finally got it, I'm writing it down so I can remember the process for the entire month that it will be valid before becoming out-of-date. Yay for volatile programming language developments! Context: my objective was to create a file that I could shuttle along to another system, wherein it could be installed in an isolated, virtual environment, without having to manage dependencies. Initially I followed the guide on the Python website for packaging and distributing files.[1] The guide uses `Hatch'. It's a helpful guide, though it omits by CLI objectives. For that I had to sniff around, eventually finding more documentation on the Python and Hatch websites.[2] I must be some dummy though, because I couldn't get it without a lot of trial and error. Anyways, after a lot of fooling around, I finally got something working. Here are the steps. Note: this process assumes you are working in a virtual environment. If not, now could be a good time to start: ,---- | python -m venv env `---- Install build dependencies ---------------------------------------------------------------------- ,---- | source env/bin/activate | pip install build pyinstaller hatch `---- Create the proper project structure ---------------------------------------------------------------------- Follow the project structure below. Note that the module inside the `src' directory is for example purposes only. ,---- | ./ | ./pyproject.toml | ./src/meme_maker/image_macro.py | ./src/meme_maker/__init__.py `---- Inside of `pyproject.toml' should look at minimum like this: ,---- | [build-system] | requires = ["hatchling", "pyinstaller"] | build-backend = "hatchling.build" | [project] | name = "meme-maker-cli" | version = "0.1" | description = 'Make memes from the CLI' | requires-python = ">=3.11" | authors = [ | { name = "Some One" }, | ] | [project.scripts] | make-image-macro = "meme_maker.image_macro:main" | [tool.hatch.build.targets.wheel] | packages = ["src/meme_maker"] | [tool.hatch.build.targets.sdist] | exclude = [ | "/env", | "/dist" | ] `---- Build the project ---------------------------------------------------------------------- The project should be built from inside the virtual environment. Simply run `hatch build'. This will output `meme-maker-cli-0.1-py3-none-any.whl' inside of a new `dist' directory. Install the project ---------------------------------------------------------------------- For this part you should be outside of a Python virtual environment. Install `python3-pipx' from your distribution package manager; or, by using `pip' inside a virtual environment. Then run `pipx install meme-maker-cli-0.1-py3-none-any.whl'. Note: older versions of `pipx' require the wheel to be specified as a local path like `./meme-maker-cli-0.1-py3-none-any.whl'. You can now invoke the CLI using `make-image-macro'. Footnotes ---------------------------------------------------------------------- Footnotes _________ [1] [2]