4 ms·
For scripting... HIGHLY recommend putting your dependencies inline. E.g.: #!/usr/bin/env python3 # /// script # requires-python = ">=3.11" # dependenc
by TheIronYuppie 2y ago
For scripting... HIGHLY recommend putting your dependencies inline.
E.g.:
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "psycopg2-binary",
# "pyyaml",
# ]
# ///
Then -
uv run -s file.py
- maleldil 2y agoHow does this interact with your code editor or IDE? When you edit the file, where does the editor look for information about the imported third-party libraries?
- AlphaSite 2y agoUsually the VENV and import lines are enough
- maleldil 2y agoHow do you determine where the venv is? AFAIK, uv run in script mode creates the venv in some random temporary directory.
- JimDabell 2y agoI don’t know of a convenient way of doing it, but a clumsy way of doing it is to run this in your script: import os print(os.environ['VIRTUAL_ENV'] + '/bin/python') Then, e.g. in VS Code, you bring up the command palette, run Python: Select Interpreter, and enter the result.
- JimDabell 2y agouv v0.6.10 has just been released with a more convenient way of doing this: uv python find --script foo.py — https://github.com/astral-sh/uv/releases/tag/0.6.10 https://github.com/astral-sh/uv/releases/tag/0.6.10 — https://docs.astral.sh/uv/reference/cli/#uv-python-find--script https://docs.astral.sh/uv/reference/cli/#uv-python-find--scr...
- marcthe12 2y agoDo you need a wrapper script for scripts in the PATH or execve? I would usualy chmod+x the script but I am not sure here.
- tetha 2y agoNot at a laptop to try this right now, but shouldn't this be possible with the shebang? Something along the lines of: #!/home/tetha/Tools/uv run
- dfinninger 2y agoYes it is, I just converted my work scripts over this afternoon. #!/usr/bin/env uv run
- Manfred 2y agoIf you want to make it work regardless of where uv is installed, you can use the following shebang line: #!/usr/bin/env uv run --script
- JimDabell 2y agoDiscussed here: > Using uv as your shebang line — https://news.ycombinator.com/item?id=42855258 https://news.ycombinator.com/item?id=42855258 Since `env` doesn’t pass multiple arguments by default, the suggested line uses `-S`: #!/usr/bin/env -S uv run --script