News for hackers. Stuff that matters.
Re:Self-contained Python scripts with uv by dharmab on Sunday March 30, 2025 @12:04AM (#43521234)
Has anyone gotten this to work on Windows? I wanted to use this trick for some tooling for a game mod I'm working on but couldn't get the shebang trick to work.
Re:Self-contained Python scripts with uv by seabrookmx on Sunday March 30, 2025 @12:11AM (#43521266)
I haven't done dev on windows in many years, but IIRC windows doesn't have shebang support.
Re:Self-contained Python scripts with uv by magicalhippo on Sunday March 30, 2025 @12:35AM (#43521382)
But it does support registering extension handlers[1], so if you name your scripts with say .pyuv and register "uv run --script %1", or whatever it would take to run uv, as the handler of .pyuv files, it should work. Unless uv does something funky that is.

You could do this during an installation step, for example.

[1]: https://learn.microsoft.com/en-us/windows/win32/shell/fa-fil...

Re:Self-contained Python scripts with uv by dlachausse on Sunday March 30, 2025 @12:20AM (#43521307)
You could always use something like PyInstaller...

https://pyinstaller.org

Re:Self-contained Python scripts with uv by quickslowdown on Sunday March 30, 2025 @12:47AM (#43521437)
I use this frequently on Windows and Linux. These are the steps I take:

$> uv init --script <script_name>.py

$> uv add --script <script_name>.py <pkg1> <pkg2> ...

$> uv add --script <script_name>.py --dev <dev_pkg1> <dev_pkg2> ...

$> uv run <script_name>.py

Hope this helps :)

Source: https://docs.astral.sh/uv/guides/scripts/

Re:Self-contained Python scripts with uv by networked on Sunday March 30, 2025 @09:56AM (#43524239)
Unfortunately, `uv add --dev` doesn't work with `--script`:

  > uv -V
  uv 0.6.10
  > uv add --script foo.py --dev ruff
  error: the argument '--script <SCRIPT>' cannot be used with '--dev'

  Usage: uv add --script <SCRIPT> --link-mode <LINK_MODE> <PACKAGES|--requirements <REQUIREMENTS>>

  For more information, try '--help'.
There is currently no mention of `uv add --script foo.py --dev ...` in https://docs.astral.sh/uv/guides/scripts/. Inline script metadata in Python doesn't standardize development dependencies.

I wrote a recent comment about how I develop scripts with `pyproject.toml` to have a regular development environment: https://news.ycombinator.com/item?id=43503171.

Re:Self-contained Python scripts with uv by quickslowdown on Sunday March 30, 2025 @11:20AM (#43524863)
Interesting about the --deb flag not working with scripts. I wrote this from memory and feel like I've installed dev dependencies for a script, but it must be a hallucination because I just tried and it doesn't work. Thanks for the correction.
Re:Self-contained Python scripts with uv by mixmastamyk on Sunday March 30, 2025 @12:50PM (#43525545)
Aside: angle brackets should be avoided in shell examples; one mistaken enter and work can be destroyed.
Re:Self-contained Python scripts with uv by sorenjan on Sunday March 30, 2025 @06:26AM (#43522991)
The regular CPython installer on Windows installs the py launcher and associates it with .py files. The py launcher supports shebang lines.

This was covered in a blog post about this same topic that was posted here a few days ago. According to that you have to omit the -S: https://thisdavej.com/share-python-scripts-like-a-pro-uv-and...

https://news.ycombinator.com/item?id=43500124

I haven't tried it myself, I simply changed the file association so all .py files are opened with uv run as standard.

https://docs.python.org/3/using/windows.html#python-launcher...

https://peps.python.org/pep-0397/

Re:Self-contained Python scripts with uv by dharmab on Sunday March 30, 2025 @01:06PM (#43525686)
Interesting. The workflow I've been using skips the CPython installer and only uses uv.
Re:Self-contained Python scripts with uv by sorenjan on Sunday March 30, 2025 @04:49PM (#43527496)
Windows doesn't support shebang lines as you probably know, but if you associate uv with .py files you'll get the same result.

I think it should be something like this:

  ftype Python.File=C:\Path\to\uv.exe run %L %*
If you don't use the CPython installer the Python.File file type might not be defined, so you might need to set that with `assoc` first:

  assoc .py=Python.File
Re:Self-contained Python scripts with uv by skeledrew on Sunday March 30, 2025 @10:08AM (#43524328)
Shebang should work fine from WSL.
Re:Self-contained Python scripts with uv by gus_massa on Sunday March 30, 2025 @11:03AM (#43524739)
I'm not sure it's useful, but someone posted how to do a similar thing for Racket with PoweScript https://onor.io/2025/01/more-scripting-with-racket.html