3 ms·
I wish an easy cross-platform PEX or shiv [1] were a thing. Binary dependencies are the biggest reason I prefer the new inline script metadata spec (https://pac
by networked 2y ago
I wish an easy cross-platform PEX or shiv [1] were a thing.
Binary dependencies are the biggest reason I prefer the new inline script metadata spec (https://packaging.python.org/en/latest/specifications/inline-script-metadata/ https://packaging.python.org/en/latest/specifications/inline...) and `pipx run`.
Luckily, they're pretty great.
They have changed how I write Python scripts.
The way inline script metadata works is that your script declares arbitrary dependencies in a structured top comment, and a compliant script runner must provide them.
Here is an example from a real script:
#! /usr/bin/env -S pipx run
# /// script
# dependencies = [
# "click==8.*",
# "Jinja2==3.*",
# "tomli==2.*",
# ]
# requires-python = ">=3.8"
# ///
pipx implements the spec with cached per-script virtual environments.
It will download the dependencies, create a venv for your script, and install the dependencies in the venv the first time you invoke the script.
The idea isn't new: you could do more or less the same with https://github.com/PyAr/fades https://github.com/PyAr/fades (2014) and https://github.com/jaraco/pip-run https://github.com/jaraco/pip-run (2015).
However, I only adopted it after I saw https://peps.python.org/pep-0722/ https://peps.python.org/pep-0722/, which PEP 723 replaced and became the current standard.
It is nice to have it standardized and part of pipx.
For really arbitrary hosts with no guarantee of recent pipx,
there is https://pip.wtf https://pip.wtf and my venv version https://github.com/dbohdan/pip-wtenv https://github.com/dbohdan/pip-wtenv.
Personally, I'd go with `pipx run` instead whenever possible.
[1] I recommend shiv over PEX for pure-Python dependencies because shiv builds faster. Have a look at https://shiv.readthedocs.io/en/stable/history.html https://shiv.readthedocs.io/en/stable/history.html.
- mikepurvis 2y agoAll else being equal I’d probably prefer poetry for the broader project structure, but that would definitely be compelling single script use cases.
- networked 2y agoYou can also combine the two. Something I have done is script dependencies in inline script metadata and dev dependencies (Pyright and Ruff) managed by Poetry.