3 ms·
I love this, the biggest problem I have right now with python scripts is distributing my single file utility scripts (random ops scripts). I wish there was a w
by faizshah 2y ago
I love this, the biggest problem I have right now with python scripts is distributing my single file utility scripts (random ops scripts).
I wish there was a way to either shebang something like this or build a wheel that has the full venv inside.
- delusional 2y agoYou are in luck https://docs.astral.sh/uv/guides/scripts/#declaring-script-dependencies https://docs.astral.sh/uv/guides/scripts/#declaring-script-d...
- easton 2y agoThere’s a shebang now. as of PEP 722 you can declare dependencies in a comment at the top of a single file script that a package manager can choose to read and resolve. uv has support for it: https://docs.astral.sh/uv/guides/scripts/#running-a-script-with-dependencies https://docs.astral.sh/uv/guides/scripts/#running-a-script-w... (which only helps if your team is all in on uv, but maybe they are)
- eesmith 2y agoPEP 722 was rejected. You are thinking of PEP 723, which was very similar to 722 in goals. https://discuss.python.org/t/pep-722-723-decision/36763 https://discuss.python.org/t/pep-722-723-decision/36763 contains the reasoning for accepting 723 and rejecting 722.
- miohtama 2y agoDo other package managers support this yet?
- zahlman 2y agoPipx isn't in any meaningful sense a package manager (although it can manage environments to a limited extent), but the `pipx run` command supports this PEP as of version 1.4.2.
- amelius 2y agoHow does that work with the shebang?
- oulipo 2y agoCheck eg https://simonwillison.net/2024/Dec/19/one-shot-python-tools/ https://simonwillison.net/2024/Dec/19/one-shot-python-tools/
- wizzard0 2y ago#!uv run # /// script # requires-python = ">=3.10" # dependencies = [ # "click>8", # "rich", # ] # /// and that's it
- amelius 2y agoCool. I wasn't sure it would skip the first comment line.
- eesmith 2y agohttps://peps.python.org/pep-0723/ https://peps.python.org/pep-0723/ is at the very least related. It's a way of specifying the metadata in the script, allowing other tools to do the right thing. One of the use cases is: > A user facing CLI that is capable of executing scripts. If we take Hatch as an example, the interface would be simply hatch run /path/to/script.py [args] and Hatch will manage the environment for that script. Such tools could be used as shebang lines on non-Windows systems e.g. #!/usr/bin/env hatch run https://micro.webology.dev/2024/08/21/uv-updates-and.html https://micro.webology.dev/2024/08/21/uv-updates-and.html shows an example with uv: > With this new feature, I can now instruct users to run uv run main.py without explaining what a venv or virtualenv is, plus a long list of requirements that need to be passed to pip install. That ends: > PEP 723 also opens the door to turning a one-file Python script into a runnable Docker image that doesn’t even need Python on the machine or opens the door for Beeware and Briefcase to build standalone apps.
- mianos 2y agoYou mean like pyinstaller https://pyinstaller.org https://pyinstaller.org that takes your python and makes a standalone, self extracting or onedir archive to convert you ops script plus dependencies into something you can just distribute like a binary?
- zahlman 2y agoThis makes sense when you need to provision Python itself to the end user, not just third-party libraries.