<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>uv &amp;mdash; Nat Knight</title>
    <link>http://natknight.xyz/tag:uv</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:37 -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>
    <item>
      <title>TIL: Time-stamp based dependency constraints with uv</title>
      <link>http://natknight.xyz/til-time-stamp-based-dependency-constraints-with-uv</link>
      <description>&lt;![CDATA[#til #python #uv&#xA;&#xA;(via epistasis on HN)&#xA;&#xA;Semantic versioning is difficult. Not everyone has the same idea of what &#34;breaking change&#34; means, and depending on your language and tooling &#34;breaking&#34; changes can sneak in despite your best efforts.&#xA;&#xA;One possible mitigation is to record when you resolved your dependencies and ignore anything published after that date. It&#39;s crude, and relies on package dependencies not monkeying with stuff that&#39;s already been published, but it&#39;s easy to understand and you can do it with uv.&#xA;&#xA;It&#39;s documented here.&#xA;&#xA;!---more---&#xA;&#xA;Put a date in your pyproject.toml file and subsequent invocations of uv will ignore anything published after the cutoff:&#xA;&#xA;[tool.uv]&#xA;exclude-newer = &#34;2023-10-16T00:00:00Z&#34;&#xA;&#xA;You can also use it in the &#34;inline metadata&#34; format for scripting! From the uv docs:&#xA;&#xA;/// script&#xA;dependencies = [&#xA;&#34;requests&#34;,&#xA;]&#xA;[tool.uv]&#xA;exclude-newer = &#34;2023-10-16T00:00:00Z&#34;&#xA;///&#xA;&#xA;import requests&#xA;&#xA;print(requests.version)&#xA;&#xA;This use case seems particularly valuable: if I write a quick script I probably care more about it not breaking than I care about it getting updated dependencies. It&#39;s nice that uv offers a way to keep code running rather than forcing me to update it or throw it out.]]&gt;</description>
      <content:encoded><![CDATA[<p><a href="http://natknight.xyz/tag:til" class="hashtag"><span>#</span><span class="p-category">til</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:uv" class="hashtag"><span>#</span><span class="p-category">uv</span></a></p>

<p>(via <a href="https://news.ycombinator.com/item?id=43097209">epistasis on HN</a>)</p>

<p>Semantic versioning is difficult. Not everyone has the same idea of what “breaking change” means, and depending on your language and tooling “breaking” changes can sneak in despite your best efforts.</p>

<p>One possible mitigation is to record <em>when</em> you resolved your dependencies and ignore anything published after that date. It&#39;s crude, and relies on package dependencies not monkeying with stuff that&#39;s already been published, but it&#39;s easy to understand and you can do it with <a href="https://docs.astral.sh/uv/"><code>uv</code></a>.</p>

<p>It&#39;s documented <a href="https://docs.astral.sh/uv/guides/scripts/#improving-reproducibility">here</a>.</p>



<p>Put a date in your <code>pyproject.toml</code> file and subsequent invocations of <code>uv</code> will ignore anything published after the cutoff:</p>

<pre><code class="language-toml">[tool.uv]
exclude-newer = &#34;2023-10-16T00:00:00Z&#34;
</code></pre>

<p>You can also use it in the <a href="https://docs.astral.sh/uv/guides/scripts/#declaring-script-dependencies">“inline metadata” format for scripting</a>! From the <code>uv</code> docs:</p>

<pre><code class="language-python"># /// script
# dependencies = [
#   &#34;requests&#34;,
# ]
# [tool.uv]
# exclude-newer = &#34;2023-10-16T00:00:00Z&#34;
# ///

import requests

print(requests.__version__)
</code></pre>

<p>This use case seems particularly valuable: if I write a quick script I probably care more about it not breaking than I care about it getting updated dependencies. It&#39;s nice that <code>uv</code> offers a way to keep code running rather than forcing me to update it or throw it out.</p>
]]></content:encoded>
      <guid>http://natknight.xyz/til-time-stamp-based-dependency-constraints-with-uv</guid>
      <pubDate>Thu, 20 Feb 2025 23:56:36 +0000</pubDate>
    </item>
  </channel>
</rss>