astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
25.64k stars 748 forks source link

Run a file without .py extension #7396

Closed sequoiayoav closed 4 weeks ago

sequoiayoav commented 1 month ago

if I make this file and name it example.py

#!/usr/bin/env uv run --quiet

# /// script
# dependencies = [
#     "requests",
# ]
# ///

from requests import get

resp = get("https://example.com")
print(resp)
chmod +x example.py

running example.py in my shell just works, But if I rename the file to just example and try to run it, uv doesn't recognize it as a python file. this isn't a bug, just want some help on this. (or recommendations) thanks!

charliermarsh commented 1 month ago

Does uv run python ./example work as expected?

kbernhagen commented 1 month ago

Possible workaround:

#!/usr/bin/env uv run -q --no-project --python 3.12 --with requests

It would be nice if uv run had option --script that did not need to have an explicit path to the script.

I am using an alias as a workaround in .zshrc:

alias fahctl='uv run -q --no-project --python 3.12 --with websocket-client fahctl'
kbernhagen commented 1 month ago

Does uv run python ./example work as expected?

Does not work for me

% uv run python /usr/local/bin/fahctl 
Missing python3-websocket library
kbernhagen commented 1 month ago

The best thing would probably be for python3 itself to understand PEP 723.

charliermarsh commented 1 month ago

We may want an explicit --script option to treat a path as PEP 723 script without sniffing the extension.

zanieb commented 1 month ago

Seems reasonable to me.

sequoiayoav commented 1 month ago

Sounds like the perfect solution

slavos1 commented 1 month ago

First off, thanks for uv.

Came across this issue too and on Ubuntu it not only "does not work", it calls itself recursively. 😨

Example:

#!/usr/bin/env -S uv run
import sys
print(f'sys.executable={sys.executable}')

When I name this file test-uv.py, make it chmod +x, then ./test-uv.py works normally.

However, when I name this file test-uv, make it chmod +x, then ./test-uv never finishes as it spawns itself recursively -- you can see it from this while true; do ps -e ...|grep uv -w; sleep .1; done:

1110162 1110156 uv run ./test-uv
1110168 1110162 uv run ./test-uv
1110174 1110168 uv run ./test-uv
1110180 1110174 uv run ./test-uv
1110186 1110180 uv run ./test-uv
1110192 1110186 uv run ./test-uv
1110198 1110192 uv run ./test-uv
1110204 1110198 uv run ./test-uv
1110210 1110204 uv run ./test-uv

I used uv version 0.4.15.

charliermarsh commented 4 weeks ago

--script now exists to power this use-case.