TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Using uv as your shebang line

479 点作者 Einenlum4 个月前

32 条评论

rav4 个月前
Oh wow, today I learned about env -S - when I saw the shebang line in the article, I immediately thought &quot;that doesn&#x27;t work on Linux, shebang lines can only pass a single argument&quot;. Basically, running foo.py starting with<p><pre><code> #!&#x2F;usr&#x2F;bin&#x2F;env -S uv run --script </code></pre> causes the OS run really run env with only two arguments, namely the shebang line as one argument and the script&#x27;s filename as the second argument, i.e.:<p><pre><code> &#x2F;usr&#x2F;bin&#x2F;env &#x27;-S uv run --script&#x27; foo.py </code></pre> However, the -S flag of env causes env to split everything back into separate arguments! Very cool, very useful.
评论 #42856704 未加载
评论 #42861614 未加载
评论 #42862036 未加载
评论 #42856742 未加载
评论 #42865875 未加载
评论 #42863984 未加载
评论 #42857888 未加载
anotherpaulg4 个月前
Not using shebang, but I&#x27;ve recently been using uv as an &quot;installer&quot;. It&#x27;s hard to package and distribute python CLI tools with complex dependencies. I used uv in a couple of novel ways:<p>1. I copied their `curl | sh` install script and added a `uv tool install --python python3.12 my-tool` line at the end. So users can now install my CLI tool with a nice curl-pipe-sh one-liner.<p>2. I made a tiny pypi &quot;installer&quot; package that has &quot;uv&quot; as its only dependency. All it does is `uv tool install` my CLI tool.<p>Both methods install the CLI tool, python3.12 and all python dependencies in their own isolated environment. Users don&#x27;t need to manage python virtual envs. They don&#x27;t need to even have python installed for the curl-pipe-sh method.<p>I now get far fewer GitHub issues from users who have somehow mangled the complex dependencies of my tool.<p>I wrote it up with more detail and links to code:<p><a href="https:&#x2F;&#x2F;aider.chat&#x2F;2025&#x2F;01&#x2F;15&#x2F;uv.html" rel="nofollow">https:&#x2F;&#x2F;aider.chat&#x2F;2025&#x2F;01&#x2F;15&#x2F;uv.html</a>
评论 #42862140 未加载
评论 #42863124 未加载
infogulch4 个月前
Speaking of funny shebangs, I came up with this to shell execute prolog .pl files, but it should work for any scripting language that has &#x2F;**&#x2F;-comments but doesn&#x27;t support #-comments or #! shebangs specifically:<p><pre><code> &#x2F;*usr&#x2F;bin&#x2F;env scryer-prolog &quot;$0&quot; &quot;$@&quot; ; exit #*&#x2F; </code></pre> From my original comment <a href="https:&#x2F;&#x2F;github.com&#x2F;mthom&#x2F;scryer-prolog&#x2F;issues&#x2F;2170#issuecomment-1821713993">https:&#x2F;&#x2F;github.com&#x2F;mthom&#x2F;scryer-prolog&#x2F;issues&#x2F;2170#issuecomm...</a> :<p>&gt; The way this works is that this test.pl is both a valid shell file and prolog file. When executing as shell the first line finds and executes &#x2F;usr&#x2F;bin&#x2F;env after searching for the glob pattern &#x2F;*usr&#x2F;bin&#x2F;env. env then executes scryer-prolog &quot;test.pl&quot; which runs the prolog file as a module and halts; of course prolog ignores the first line as a comment &#x2F;* ... *&#x2F;. Then the shell continues and executes the next command after ; which is exit which stops execution so the rest of the file (which is not valid shell) is ignored. The # makes the shell evaluator ignore prolog comment closer *&#x2F; to prevent it from printing an error.<p>This may be the best and worst thing I have ever created.
评论 #42859871 未加载
评论 #42859937 未加载
评论 #42859146 未加载
nikkindev4 个月前
“Lazy self-installing Python scripts with uv”[1] article from Trey Hunner has more details with examples<p>[1] <a href="https:&#x2F;&#x2F;treyhunner.com&#x2F;2024&#x2F;12&#x2F;lazy-self-installing-python-scripts-with-uv&#x2F;" rel="nofollow">https:&#x2F;&#x2F;treyhunner.com&#x2F;2024&#x2F;12&#x2F;lazy-self-installing-python-s...</a>
评论 #42858975 未加载
hv424 个月前
You can use this trick with mise (mise-en-place) for small tasks: <a href="https:&#x2F;&#x2F;mise.jdx.dev&#x2F;tasks&#x2F;toml-tasks.html#shell-shebang" rel="nofollow">https:&#x2F;&#x2F;mise.jdx.dev&#x2F;tasks&#x2F;toml-tasks.html#shell-shebang</a><p><pre><code> [tools] uv = &#x27;latest&#x27; [tasks.python_uv_task] run = &quot;&quot;&quot; #!&#x2F;usr&#x2F;bin&#x2F;env -S uv run --script # &#x2F;&#x2F;&#x2F; script # dependencies = [&quot;requests&lt;3&quot;, &quot;rich&quot;] # &#x2F;&#x2F;&#x2F; import requests # your code here &quot;&quot;&quot;</code></pre>
评论 #42856214 未加载
mkl4 个月前
Related article and discussion from 16 days ago: Uv&#x27;s killer feature is making ad-hoc environments easy <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=42676432">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=42676432</a> (502 points, 417 comments)
babel_4 个月前
Now that&#x27;s a trick I should remember!<p>I recently switched over my python aliases to `uv run python` and it&#x27;s been really quite pleasant, without needing to manage `.venv`s and the rest. No fuss about system installs for python or anything else either, which resolves the old global&#x2F;user install problem (and is a boon on Debian). Also means you can invoke the REPL within a project&#x2F;environment without any `activate`, which saves needing to think about it.<p>Only downside for calling a .py directly with uv is the cwd relative pathing to project&#x2F;environment files, rather than relative to the .py file itself. There is an explicit switch for it, `--project`, which at least is not much of an ask (`uv run --project &lt;path&gt; &lt;path&gt;&#x2F;script.py`), though a target relative project switch would be appreciated, to at least avoid the repetition.
threecheese4 个月前
I e experienced a few “gotchas” using uv (or uvx) as a command runner, but when it works (which is most of the time) it’s a big time saver. As a Python dev and curious person my homedir is littered with shallow clones and one-off directories for testing some package or another, and not having to manage this overhead is really useful.<p>OP, you have a great idea and I’m stealing it. Do you use some non-.py suffix, or is seeing the exec bit at on a .py file enough of a signal for you to know that you can just run the file as a script (and it won’t use the system Python)?
评论 #42856287 未加载
评论 #42856149 未加载
评论 #42860131 未加载
einpoklum4 个月前
The idea is as follows:<p>* A recently-published PEP specificies how Python scripts can declare dependencies in opening comments.<p>* uv is a Python script-runner&#x2F;package manager which scans for those dependencies, arranges for them to be met, and runs the script with those dependency modules available for importing<p>* If, in a Python script, you use the first line comment - the shebang line - to have the file invoked using uv rather than python itself, you&#x27;ll get that effect automagically when &quot;running&quot; the Python script<p>The result: Python scripts which require setup of dependencies will now &quot;just run&quot;.
评论 #42856263 未加载
评论 #42856284 未加载
dcre4 个月前
For the TypeScript enjoyers: you can do the same with Deno (and I&#x27;m sure Bun too, though I haven&#x27;t done it).<p><pre><code> #! &#x2F;usr&#x2F;bin&#x2F;env -S deno run </code></pre> You can add permission flags like this:<p><pre><code> #! &#x2F;usr&#x2F;bin&#x2F;env -S deno run --allow-env --allow-read --allow-net</code></pre>
评论 #42857909 未加载
krick4 个月前
Wow, this is super obvious (so obvious that it wouldn&#x27;t occur to me to write a post to brag about it), yet it somehow didn&#x27;t occur to me. Very cool.<p>Not so long ago I started pip-installing my scripts to keep things tidy. It seems now I can regress back to chaos again...
评论 #42858605 未加载
traverseda4 个月前
You can also do the same thing with the nix package manager and pretty much any language or dependency. Like if you wanted a portable bash-script with imagemagick<p><pre><code> #! &#x2F;usr&#x2F;bin&#x2F;env nix-shell #! nix-shell -i bash -p bash imagemagick </code></pre> Handy for passing quick scripts around. Nothing is quite as portable as a single file like this.
评论 #42857917 未加载
PhilippGille4 个月前
From 68 days ago:<p>&quot;&#x2F;usr&#x2F;bin&#x2F;env -S uv run&quot;: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=42198256">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=42198256</a><p>125 points, 45 comments
评论 #42858362 未加载
7thpower4 个月前
Does anyone have any intro to UV guides they found helpful?<p>I haven’t dove into uv as it just hasn’t been at the top of my list, but I will probably take the plunge soon.<p>I gave it an initial try thinking it might function as a drop in, but didn’t find any great guides, and just haven’t been back yet. But we’re ~8% into 2025 so it’s time.
评论 #42889471 未加载
TrueDuality4 个月前
One gotcha that annoys me when do this... The file still needs to end in .py which I generally don&#x27;t like for the bins and utilities in my path. Everyone using these shebangs either doesn&#x27;t hit this or never mentions it. Entirely a stylistic personal preference but still makes me a sad lady.
评论 #42858312 未加载
评论 #42857922 未加载
alin234 个月前
I recently learned about swift-sh [1] which is the same thing as “uv run —script” but for swift files.<p>I’ve been having a blast automating some macOS annoyances with small scripts that can use native macOS APIs directly, no more pyobjc.<p>I also wrote an app to bring the startup folder functionality from Windows to the Mac to help me run these scripts at startup more easily [2]<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;mxcl&#x2F;swift-sh">https:&#x2F;&#x2F;github.com&#x2F;mxcl&#x2F;swift-sh</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;FuzzyIdeas&#x2F;StartupFolder">https:&#x2F;&#x2F;github.com&#x2F;FuzzyIdeas&#x2F;StartupFolder</a>
recursive_fruit4 个月前
Oh nice, that&#x27;s really cool. I&#x27;m seeing uv pop up in more and more places. Python really needed this tooling upgrade. Also, I&#x27;ll make a mental note of -S, awesome.<p>If you want to do the same with javascript or typescript, you can just use bun, it will auto-install[1] the packages included in there.<p><pre><code> #!&#x2F;usr&#x2F;bin&#x2F;env bun </code></pre> [1]: <a href="https:&#x2F;&#x2F;bun.sh&#x2F;docs&#x2F;runtime&#x2F;autoimport" rel="nofollow">https:&#x2F;&#x2F;bun.sh&#x2F;docs&#x2F;runtime&#x2F;autoimport</a>
tiberriver2564 个月前
uv makes Python actually usable. Freaking love that tool.
picafrost4 个月前
Astral are making great tools. There are many points of friction in the Python world and Astral is making them seem obvious to see and obvious to solve. That is hard work.
AdieuToLogic4 个月前
This reminds me of an old Perl magic spell for invoking it within a Bourne&#x2F;POSIX shell wrapper along with allowing arbitrary command line arguments[0]:<p><pre><code> #!&#x2F;bin&#x2F;sh #! -*- perl -*- -p eval &#x27;exec perl -x -wS $0 ${1+&quot;$@&quot;}&#x27; if 0; # It&#x27;s Perl code from this point. </code></pre> 0 - <a href="https:&#x2F;&#x2F;perldoc.perl.org&#x2F;perlrun" rel="nofollow">https:&#x2F;&#x2F;perldoc.perl.org&#x2F;perlrun</a>
zahlman4 个月前
I really don&#x27;t understand how it is that other people frequently write one-off scripts that nevertheless need to run in an isolated environment (as opposed to a sandbox that contains a few common workhorse packages like NumPy or Requests).<p>Of course I have plenty of separate environments set up, but that&#x27;s because I&#x27;m <i>developing</i> things I hope to share with others, so I need to keep close track of their specific dependencies (so I can specify them in package metadata). But even this stuff would generally work with a wide range of versions for those dependencies. I&#x27;d imagine this is equally true of small, one-off, personal automation scripts.<p>But, sure, if you have this problem, that&#x27;s a great way to use the shebang line. (You can do similarly with Pipx, and I expect you&#x27;ll be able to with Paper as well, although I might push that sort of thing into an optional add-on.)
评论 #42856780 未加载
评论 #42856178 未加载
评论 #42856094 未加载
Levitating4 个月前
Ruby&#x27;s bundler has actually always been able to do inline dependencies<p><a href="https:&#x2F;&#x2F;bundler.io&#x2F;guides&#x2F;bundler_in_a_single_file_ruby_script.html" rel="nofollow">https:&#x2F;&#x2F;bundler.io&#x2F;guides&#x2F;bundler_in_a_single_file_ruby_scri...</a><p>Though I think the implementation could be improved upon
vunderba4 个月前
I&#x27;ve been meaning to look into uv. However, since I make pretty heavy use of conda for my pythons scripts, I&#x27;ll usually add a bash function that handles conda activation &#x2F; code execution to my .zshrc file.<p><pre><code> function transcribe() { # get the active conda environment active_env=$(conda info | grep &quot;active environment&quot; | awk &#x27;{print $4}&#x27;) if [[ &quot;$active_env&quot; != &quot;speechrec-env&quot; ]]; then conda activate speechrec-env fi python &#x2F;users&#x2F;vunderba&#x2F;dev&#x2F;transcriber&#x2F;transcribe.py &quot;$@&quot; } </code></pre> It&#x27;s handy because if it&#x27;ll always point to the most current version of my script.
评论 #42856232 未加载
mark_l_watson4 个月前
Nice ideas, especially with “-S” env option.<p>I love having little custom command line apps in my ~&#x2F;bin directory. I used to, a very long time ago, use Gambit Scheme for this, then moved on to Common Lisp command line apps. More recently I have been using Python for ~&#x2F;bin scripts but since I use venv for each individual project on my laptop, having a global Python setup with dependencies for ~&#x2F;bin is annoying. Using uv seems like a great idea, which I will try today: use uv for all command line apps and venv for all other Python coding projects.
Tyrubias4 个月前
I love using this technique, but developing these scripts has always bothered me because there’s no code completion without an actual `venv` to point my editor to. Has anyone found a way around this?
评论 #42871527 未加载
m3at4 个月前
Uv&#x27;s support of inline metadata is super handy. What if the person running the scripts doesn&#x27;t have uv yet though? For fun I wrote a double shebang script that will handle that too: <a href="https:&#x2F;&#x2F;paulw.tokyo&#x2F;standalone-python-script-with-uv&#x2F;" rel="nofollow">https:&#x2F;&#x2F;paulw.tokyo&#x2F;standalone-python-script-with-uv&#x2F;</a>
tiltowait4 个月前
I’ve seen uv a lot recently. Is there any major benefit over poetry?
评论 #42857072 未加载
评论 #42857315 未加载
评论 #42856991 未加载
评论 #42856770 未加载
评论 #42860188 未加载
thecodemonkey4 个月前
What is uv?
评论 #42855760 未加载
评论 #42855750 未加载
评论 #42855751 未加载
评论 #42855738 未加载
Eduard4 个月前
why not<p><pre><code> #!&#x2F;usr&#x2F;bin&#x2F;env python3 ?</code></pre>
评论 #42860179 未加载
kseistrup4 个月前
See also <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=42198256">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=42198256</a> from November 2024 (40 comments).
megamix4 个月前
if it has Rust in it, then...
zombot4 个月前
What is this `uv` thing? Is that something pythonic?
评论 #42865719 未加载