<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>scripting &amp;mdash; Nat Knight</title>
    <link>http://natknight.xyz/tag:scripting</link>
    <description>Reflections, diversions, and opinions from a progressive ex-physicist programmer dad with a sore back.</description>
    <pubDate>Sat, 23 May 2026 14:44:55 -0700</pubDate>
    <item>
      <title>TIL: uv --script</title>
      <link>http://natknight.xyz/til-uv-script</link>
      <description>&lt;![CDATA[#uv #python #til #scripting&#xA;&#xA;I&#39;ve known for a while that uv can run Python scripts that declare inline dependencies.&#xA;&#xA;I learned today that you can also use uv to manage those dependencies.&#xA;&#xA;!--more--&#xA;&#xA;If you have a file foo.py:&#xA;&#xA;!/usr/bin/env -S uv run&#xA;&#xA;print(&#34;your code here&#34;)&#xA;&#xA;you can run uv add --script foo.py numpy and it becomes:&#xA;&#xA;!/usr/bin/env -S uv run&#xA;/// script&#xA;requires-python = &#34;  =3.12&#34;&#xA;dependencies = [&#xA;&#34;numpy&#34;,&#xA;]&#xA;///&#xA;&#xA;print(&#34;your code here&#34;)&#xA;&#xA;This is obviously nice if, like me, you have trouble remembering the exact syntax for declaring the inline dependencies, but it also means uv does things like dependency resolution so you don&#39;t have to manage your script&#39;s dependencies by hand.&#xA;&#xA;You can add --script to:&#xA;&#xA;uv add and uv remove to add and remove dependencies&#xA;uv lock and uv sync to create and synchronize with the script&#39;s lock file&#xA;uv tree to print the script&#39;s transitive dependencies&#xA;uv export to export the script&#39;s dependencies to another format, such as requirements.txt&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p><a href="http://natknight.xyz/tag:uv" class="hashtag"><span>#</span><span class="p-category">uv</span></a> <a href="http://natknight.xyz/tag:python" class="hashtag"><span>#</span><span class="p-category">python</span></a> <a href="http://natknight.xyz/tag:til" class="hashtag"><span>#</span><span class="p-category">til</span></a> <a href="http://natknight.xyz/tag:scripting" class="hashtag"><span>#</span><span class="p-category">scripting</span></a></p>

<p>I&#39;ve known for a while that <code>uv</code> can <a href="https://docs.astral.sh/uv/guides/scripts/#declaring-script-dependencies">run Python scripts</a> that declare <a href="https://packaging.python.org/en/latest/specifications/inline-script-metadata/#inline-script-metadata">inline dependencies</a>.</p>

<p>I learned today that you can also use <code>uv</code> to <em>manage</em> those dependencies.</p>



<p>If you have a file <code>foo.py</code>:</p>

<pre><code class="language-python">#!/usr/bin/env -S uv run

print(&#34;your code here&#34;)
</code></pre>

<p>you can run <code>uv add --script foo.py numpy</code> and it becomes:</p>

<pre><code class="language-python">#!/usr/bin/env -S uv run
# /// script
# requires-python = &#34;&gt;=3.12&#34;
# dependencies = [
#     &#34;numpy&#34;,
# ]
# ///

print(&#34;your code here&#34;)
</code></pre>

<p>This is obviously nice if, like me, you have trouble remembering the exact syntax for declaring the inline dependencies, but it also means <code>uv</code> does things like dependency resolution so you don&#39;t have to manage your script&#39;s dependencies by hand.</p>

<p>You can add <code>--script</code> to:</p>
<ul><li><code>uv add</code> and <code>uv remove</code> to add and remove dependencies</li>
<li><code>uv lock</code> and <code>uv sync</code> to create and synchronize with the script&#39;s lock file</li>
<li><code>uv tree</code> to print the script&#39;s transitive dependencies</li>
<li><code>uv export</code> to export the script&#39;s dependencies to another format, such as <code>requirements.txt</code></li></ul>
]]></content:encoded>
      <guid>http://natknight.xyz/til-uv-script</guid>
      <pubDate>Tue, 04 Mar 2025 17:59:28 +0000</pubDate>
    </item>
  </channel>
</rss>