dharmab 3 days ago

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.

6
sorenjan 3 days ago

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/

dharmab 3 days ago

Interesting. The workflow I've been using skips the CPython installer and only uses uv.

sorenjan 3 days ago

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

quickslowdown 3 days ago

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/

networked 3 days ago

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.

quickslowdown 3 days ago

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.

mixmastamyk 3 days ago

Aside: angle brackets should be avoided in shell examples; one mistaken enter and work can be destroyed.

seabrookmx 3 days ago

I haven't done dev on windows in many years, but IIRC windows doesn't have shebang support.

magicalhippo 3 days ago

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...

dlachausse 3 days ago

You could always use something like PyInstaller...

https://pyinstaller.org

gus_massa 3 days ago

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

skeledrew 3 days ago

Shebang should work fine from WSL.