<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Posts about Python</title><link>https://chriswarrick.com/</link><atom:link href="https://chriswarrick.com/blog/tags/python.xml" rel="self" type="application/rss+xml" /><description>A rarely updated blog, mostly about programming.</description><lastBuildDate>Mon, 16 Feb 2026 21:15:00 GMT</lastBuildDate><generator>https://github.com/Kwpolska/YetAnotherBlogGenerator</generator><item><title>I Wrote YetAnotherBlogGenerator</title><dc:creator>Chris Warrick</dc:creator><link>https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/</link><pubDate>Mon, 16 Feb 2026 21:15:00 GMT</pubDate><guid>https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/</guid><description>Writing a static site generator is a developer rite of passage. For the past 13 years, this blog was generated using Nikola. This week, I finished implementing my own generator, the unoriginally named YetAnotherBlogGenerator.
Why would I do that? Why would I use C# for it? And how fast is it? Continue reading to find out.
</description><content:encoded><![CDATA[<p>Writing a static site generator is a developer rite of passage. For the past 13 years, this blog was generated using <a href="https://getnikola.com/">Nikola</a>. This week, I finished implementing my own generator, the unoriginally named <a href="https://github.com/Kwpolska/YetAnotherBlogGenerator">YetAnotherBlogGenerator</a>.</p>
<p>Why would I do that? Why would I use C# for it? And how fast is it? Continue reading to find out.</p>



<h2 id="ok-but-why">OK, but why?</h2>
<p>You might have noticed I’m not happy with <a href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/">the Python</a> <a href="https://chriswarrick.com/blog/2024/01/15/python-packaging-one-year-later">packaging ecosystem</a>. But the language itself is no longer fun for me to code in either. It is especially not fun to maintain projects in. <a href="https://discuss.python.org/t/revisiting-pep-505-none-aware-operators/74568/">Elementary quality-of-life features</a> get bogged down in months of discussions and design-by-committee. At the same time, there’s a new release every year, full of removed and deprecated features. A lot of churn, without much benefit. I just don’t feel like doing it anymore.</p>
<p>Python is praised for being fast to develop in. That’s certainly true, but a good high-level statically-typed language can yield similar development speed with more correctness from day one. For example, I coded an entire table-of-contents-sidebar feature in one evening (and one more evening of CSS wrangling to make it look good). This feature extracts headers from either the Markdown AST or the HTML fragment. I could do it in Python, but I’d need to jump through hoops to get Python-Markdown to output headings with IDs. In C#, introspecting what a class can do is easier thanks to great IDE support and much less dynamic magic happening at runtime. There are also decompiler tools that make it easy to look under the hood and see what a library is doing.</p>
<p>Writing a static site generator is also a learning experience. A competent SSG needs to ingest content in various formats (as nobody wants to write blog posts in HTML by hand) and generate HTML (usually from templates) and XML (which you could, in theory, do from templates, but since XML parsers are not at all lenient, you don’t want to). Image processing to generate thumbnails is needed too. And to generate correct RSS feeds, you need to parse HTML to rewrite links. The list of small-but-useful things goes on.</p>
<h2 id="is-c.net-a-viable-technology-stack-for-a-static-site-generator">Is C#/.NET a viable technology stack for a static site generator?</h2>
<p>C#/.NET is certainly not the most popular technology stack for static site generators. <a href="https://jamstack.org/generators/">JamStack.org</a> have gathered a list of 377 SSGs. <a href="https://chriswarrick.com/listings/yabg-intro/jamstack-org-generators.js.html">Grouping by language</a>, there are 154 generators written in JavaScript or TypeScript, 55 generators written in Python, and 28 written in <em>PHP</em> of all languages. C#/.NET is in sixth place with 13 (not including YABG; I’m probably not submitting it).</p>
<p>However, it is a pretty good choice. Language-level support for concurrency with <code>async</code>/<code>await</code> (based on a thread pool) and JIT compilation help to make things fast. But it is still a high-level, object-oriented language where you don’t need to manually manage memory (hi Rustaceans!).</p>
<p>The library ecosystem is solid too. There are plenty of good libraries for working with data serialization formats: <a href="https://joshclose.github.io/CsvHelper/">CsvHelper</a>, <a href="https://github.com/aaubry/YamlDotNet">YamlDotNet</a>, <a href="https://www.nuget.org/packages/Microsoft.Data.Sqlite/">Microsoft.Data.Sqlite</a>, and the built-in <a href="https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/overview">System.Text.Json</a> and <a href="https://learn.microsoft.com/en-us/dotnet/standard/linq/linq-xml-overview">System.Xml.Linq</a>. <a href="https://github.com/xoofx/markdig">Markdig</a> handles turning Markdown into HTML. <a href="https://github.com/sebastienros/fluid">Fluid</a> is an excellent templating library that implements the Liquid templating language. <a href="https://html-agility-pack.net/">HtmlAgilityPack</a> is solid for manipulating HTML, and <a href="https://github.com/dlemstra/Magick.NET">Magick.NET</a> wraps the ImageMagick library.</p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_40b21992ea7c1038c9310b71679b5d10743805d9-1"><code data-line-number=" 1"></code></a></td><td class="code"><code><a id="code_40b21992ea7c1038c9310b71679b5d10743805d9-1" name="code_40b21992ea7c1038c9310b71679b5d10743805d9-1"></a><span class="nt">&lt;PackageReference</span><span class="w"> </span><span class="na">Include=</span><span class="s">&quot;CsvHelper&quot;</span><span class="w"> </span><span class="na">Version=</span><span class="s">&quot;33.1.0&quot;</span><span class="nt">/&gt;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_40b21992ea7c1038c9310b71679b5d10743805d9-2"><code data-line-number=" 2"></code></a></td><td class="code"><code><a id="code_40b21992ea7c1038c9310b71679b5d10743805d9-2" name="code_40b21992ea7c1038c9310b71679b5d10743805d9-2"></a><span class="nt">&lt;PackageReference</span><span class="w"> </span><span class="na">Include=</span><span class="s">&quot;Fluid.Core&quot;</span><span class="w"> </span><span class="na">Version=</span><span class="s">&quot;2.31.0&quot;</span><span class="nt">/&gt;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_40b21992ea7c1038c9310b71679b5d10743805d9-3"><code data-line-number=" 3"></code></a></td><td class="code"><code><a id="code_40b21992ea7c1038c9310b71679b5d10743805d9-3" name="code_40b21992ea7c1038c9310b71679b5d10743805d9-3"></a><span class="nt">&lt;PackageReference</span><span class="w"> </span><span class="na">Include=</span><span class="s">&quot;Fluid.ViewEngine&quot;</span><span class="w"> </span><span class="na">Version=</span><span class="s">&quot;2.31.0&quot;</span><span class="nt">/&gt;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_40b21992ea7c1038c9310b71679b5d10743805d9-4"><code data-line-number=" 4"></code></a></td><td class="code"><code><a id="code_40b21992ea7c1038c9310b71679b5d10743805d9-4" name="code_40b21992ea7c1038c9310b71679b5d10743805d9-4"></a><span class="nt">&lt;PackageReference</span><span class="w"> </span><span class="na">Include=</span><span class="s">&quot;HtmlAgilityPack&quot;</span><span class="w"> </span><span class="na">Version=</span><span class="s">&quot;1.12.4&quot;</span><span class="nt">/&gt;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_40b21992ea7c1038c9310b71679b5d10743805d9-5"><code data-line-number=" 5"></code></a></td><td class="code"><code><a id="code_40b21992ea7c1038c9310b71679b5d10743805d9-5" name="code_40b21992ea7c1038c9310b71679b5d10743805d9-5"></a><span class="nt">&lt;PackageReference</span><span class="w"> </span><span class="na">Include=</span><span class="s">&quot;Magick.NET-Q8-AnyCPU&quot;</span><span class="w"> </span><span class="na">Version=</span><span class="s">&quot;14.10.2&quot;</span><span class="nt">/&gt;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_40b21992ea7c1038c9310b71679b5d10743805d9-6"><code data-line-number=" 6"></code></a></td><td class="code"><code><a id="code_40b21992ea7c1038c9310b71679b5d10743805d9-6" name="code_40b21992ea7c1038c9310b71679b5d10743805d9-6"></a><span class="nt">&lt;PackageReference</span><span class="w"> </span><span class="na">Include=</span><span class="s">&quot;Markdig&quot;</span><span class="w"> </span><span class="na">Version=</span><span class="s">&quot;0.45.0&quot;</span><span class="nt">/&gt;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_40b21992ea7c1038c9310b71679b5d10743805d9-7"><code data-line-number=" 7"></code></a></td><td class="code"><code><a id="code_40b21992ea7c1038c9310b71679b5d10743805d9-7" name="code_40b21992ea7c1038c9310b71679b5d10743805d9-7"></a><span class="nt">&lt;PackageReference</span><span class="w"> </span><span class="na">Include=</span><span class="s">&quot;Microsoft.Data.Sqlite&quot;</span><span class="w"> </span><span class="na">Version=</span><span class="s">&quot;10.0.3&quot;</span><span class="nt">/&gt;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_40b21992ea7c1038c9310b71679b5d10743805d9-8"><code data-line-number=" 8"></code></a></td><td class="code"><code><a id="code_40b21992ea7c1038c9310b71679b5d10743805d9-8" name="code_40b21992ea7c1038c9310b71679b5d10743805d9-8"></a><span class="nt">&lt;PackageReference</span><span class="w"> </span><span class="na">Include=</span><span class="s">&quot;Microsoft.Extensions.FileProviders.Physical&quot;</span><span class="w"> </span><span class="na">Version=</span><span class="s">&quot;10.0.3&quot;</span><span class="nt">/&gt;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_40b21992ea7c1038c9310b71679b5d10743805d9-9"><code data-line-number=" 9"></code></a></td><td class="code"><code><a id="code_40b21992ea7c1038c9310b71679b5d10743805d9-9" name="code_40b21992ea7c1038c9310b71679b5d10743805d9-9"></a><span class="nt">&lt;PackageReference</span><span class="w"> </span><span class="na">Include=</span><span class="s">&quot;Microsoft.Extensions.Logging.Console&quot;</span><span class="w"> </span><span class="na">Version=</span><span class="s">&quot;10.0.3&quot;</span><span class="nt">/&gt;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_40b21992ea7c1038c9310b71679b5d10743805d9-10"><code data-line-number="10"></code></a></td><td class="code"><code><a id="code_40b21992ea7c1038c9310b71679b5d10743805d9-10" name="code_40b21992ea7c1038c9310b71679b5d10743805d9-10"></a><span class="nt">&lt;PackageReference</span><span class="w"> </span><span class="na">Include=</span><span class="s">&quot;YamlDotNet&quot;</span><span class="w"> </span><span class="na">Version=</span><span class="s">&quot;16.3.0&quot;</span><span class="nt">/&gt;</span>
</code></td></tr></table></div>
<p>There’s one major thing missing from the above list: code highlighting. <a href="https://www.nuget.org/packages?q=highlight">There are a few highlighting libraries on NuGet</a>, but I decided to stick with <a href="https://pygments.org/">Pygments</a>. I still need the Pygments stylesheets around since I’m not converting old reStructuredText posts to Markdown (I’m copying them as HTML directly from Nikola’s <code>cache</code>), so using Pygments for new content keeps things consistent. Staying with Pygments means I still maintain a bit of Python code, but much less: 230 LoC in <code>pygments_better_html</code> and 89 in <code>yabg_pygments_adapter</code>, with just one third-party dependency. Calling a subprocess while rendering listings is slow, but it’s a price worth paying.</p>
<h3 id="paid-libraries-in-the.net-ecosystem">Paid libraries in the .NET ecosystem</h3>
<p>All the above libraries are open source (MIT, Apache 2.0, BSD-2-Clause). However, one well-known issue of the .NET ecosystem is the number of packages that suddenly become commercial. This trend was started by <a href="https://dotnetfoundation.org/news-events/detail/update-on-imagesharp">ImageSharp</a>, a popular 2D image manipulation library. I could probably use it, since it’s licensed to open-source projects under Apache 2.0, but I’d rather not. I initially tried <a href="https://www.nuget.org/packages/SkiaSharp/">SkiaSharp</a>, but it has terrible image scaling algorithms, so I settled on <a href="https://www.nuget.org/packages/SkiaSharp">Magick.NET</a>.</p>
<p>Open-source sustainability is hard, maybe impossible. But I don’t think transitioning from open-source to pay-for-commercial-use is the answer. In practice, many businesses just use the last free version or switch to a different library. I’d rather support open-source projects developed by volunteers in their spare time. They might not be perfect or always do exactly what I want, but I’m happy to contribute fixes and improve things for everyone. I will avoid proprietary or dual-licensed libraries, even for code that never leaves my computer. Some people complain when Microsoft creates a library that competes with a third-party open-source library (e.g. <a href="https://www.nuget.org/packages/Microsoft.AspNetCore.OpenApi">Microsoft.AspNetCore.OpenApi</a>, which was built to replace <a href="https://www.nuget.org/packages/Swashbuckle.AspNetCore">Swashbuckle.AspNetCore</a>), but I am okay with that, since libraries built or backed by large corporations (like Microsoft) tend to be better maintained.</p>
<p>But at least sometimes <a href="https://www.jimmybogard.com/automapper-and-mediatr-commercial-editions-launch-today/">trash libraries take themselves out</a>.</p>
<h2 id="is-it-fast">Is it fast?</h2>
<p>One of the things that set Nikola apart from other Python static site generators is that it only rebuilds files that need to be rebuild. This does make Nikola fast when rebuilding things, but it comes at a cost: Nikola needs to track all dependencies very closely. Also, some features that are present in other SSGs are not easy to achieve in Nikola, because they would cause many pages to be rebuilt.</p>
<p>YetAnotherBlogGenerator has almost no caching. The only thing currently cached is code listings, since they’re rendered using Pygments in a subprocess. Additionally, the image scaling service checks the file modification date to skip regenerating thumbnails if the source image hasn’t changed. And yet, even if it rewrites everything, YABG finishes faster than Nikola when the site is fully up-to-date (there is nothing to do).</p>
<p>I ran some quick benchmarks comparing the performance of rendering the final Nikola version of this blog against the first YABG version (before the Bootstrap 5 redesign).</p>
<h3 id="testing-methodology">Testing methodology</h3>
<p>Here’s the testing setup:</p>
<ul>
<li>AWS EC2 instances
<ul>
<li>c7a.xlarge (4 vCPU, 8 GB RAM)</li>
<li>30 GB io2 SSD (30000 IOPS)</li>
<li>Total cost: $2.95 + tax for about an hour’s usage ($2.66 of which were storage costs)</li>
</ul>
</li>
<li>Fedora 43 from official Fedora AMI
<ul>
<li>Python 3.14.2 (latest available in the repos)</li>
<li>.NET SDK 10.0.102 / .NET 10.0.2 (latest available in the repos)</li>
<li>setenforce 0, SELINUX=disabled</li>
</ul>
</li>
<li>Windows Server 2025
<ul>
<li>Python 3.14.3 (latest available in winget)</li>
<li>.NET SDK 10.0.103 / .NET 10.0.3 (latest available in winget)</li>
<li>Windows Defender disabled</li>
</ul>
</li>
</ul>
<p>I ran three tests. Each test was run 11 times. The first attempt was discarded (as a warmup and to let me verify the log). The other ten attempts were averaged as the final result. I used PowerShell’s <code>Measure-Command</code> cmdlet for measurements.</p>
<p>The tests were as follows:</p>
<ol>
<li><strong>Clean build (no cache, no output)</strong>
<ul>
<li>Removing <code>.doit.db</code>, <code>cache</code>, and <code>output</code> from the Nikola site, so that everything has to be rebuilt from scratch.</li>
<li>Removing <code>.yabg_cache.sqlite3</code> and <code>output</code> from the YABG site, so that everything has to be reuilt from scratch, most notably the Pygments code listings have to be regenerated via a subprocess.</li>
</ul>
</li>
<li><strong>Build with cache, but no output</strong>
<ul>
<li>Removing <code>output</code> from the Nikola site, so that posts rendered to HTML by docutils/Python-Markdown are cached, but the final HTML still need to be built.</li>
<li>Removing <code>output</code> from the YABG site, so that the code listings rendered to HTML by Pygments are cached, but everything else needs to be built.</li>
</ul>
</li>
<li><strong>Rebuild (cache and output intact)</strong>
<ul>
<li>Not removing anything from the Nikola site, so that there is nothing to do.</li>
<li>Not removing anything from the YABG site. Things are still rebuilt, except for Pygments code listings and thumbnails.</li>
</ul>
</li>
</ol>
<p>For YetAnotherBlogGenerator, I tested two builds: one in Release mode (standard), and another in <a href="https://learn.microsoft.com/en-us/dotnet/core/deploying/ready-to-run">ReadyToRun mode</a>, trading build time and executable size for faster execution.</p>
<p>All the scripts I used for setup and testing can be found in <a href="https://chriswarrick.com/listings/yabg-intro/speedtest/">listings</a>.</p>
<h3 id="test-results">Test results</h3>
<div class="table-wrapper">
<table>
<thead>
<tr>
<th>Platform</th>
<th>Build type</th>
<th style="text-align: right;">Nikola</th>
<th style="text-align: right;">YABG (ReadyToRun)</th>
<th style="text-align: right;">YABG (Release)</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Linux</strong></td>
<td>Clean build (no cache, no output)</td>
<td style="text-align: right;">6.438</td>
<td style="text-align: right;">1.901</td>
<td style="text-align: right;">2.178</td>
</tr>
<tr>
<td><strong>Linux</strong></td>
<td>Build with cache, but no output</td>
<td style="text-align: right;">5.418</td>
<td style="text-align: right;">0.980</td>
<td style="text-align: right;">1.249</td>
</tr>
<tr>
<td><strong>Linux</strong></td>
<td>Rebuild (cache and output intact)</td>
<td style="text-align: right;">0.997</td>
<td style="text-align: right;">0.969</td>
<td style="text-align: right;">1.248</td>
</tr>
<tr>
<td><strong>Windows</strong></td>
<td>Clean build (no cache, no output)</td>
<td style="text-align: right;">9.103</td>
<td style="text-align: right;">2.666</td>
<td style="text-align: right;">2.941</td>
</tr>
<tr>
<td><strong>Windows</strong></td>
<td>Build with cache, but no output</td>
<td style="text-align: right;">7.758</td>
<td style="text-align: right;">1.051</td>
<td style="text-align: right;">1.333</td>
</tr>
<tr>
<td><strong>Windows</strong></td>
<td>Rebuild (cache and output intact)</td>
<td style="text-align: right;">1.562</td>
<td style="text-align: right;">1.020</td>
<td style="text-align: right;">1.297</td>
</tr>
</tbody>
</table>
</div><h2 id="design-details-and-highlights">Design details and highlights</h2>
<p>Here are some fun tidbits from development.</p>
<h3 id="everything-is-an-item">Everything is an item</h3>
<p>In Nikola, there are several different entities that can generate HTML files. Posts and Pages are both <code>Post</code> objects. Listings and galleries each have their own task generators. There’s no <code>Listing</code> class, everything is handled within the listing plugin. Galleries can optionally have a <code>Post</code> object attached (though that <code>Post</code> is not picked up by the file scanner, and it is not part of the timeline). The listings and galleries task generators both have ways to build directory trees.</p>
<p>In YABG, all of the above are <code>Item</code>s. Specifically, they start as <code>SourceItem</code>s and become <code>Item</code>s when rendered. For listings, the source is just the code and the rendered content is Pygments-generated HTML. For galleries, the source is a <a href="https://en.wikipedia.org/wiki/Tab-separated_values">TSV file</a> with a list of included gallery images (order, filenames, and descriptions), and the generated content comes from a meta field named <code>galleryIntroHtml</code>. Gallery objects have a <code>GalleryData</code> object attached to their <code>Item</code> object as <code>RichItemData</code>.</p>
<p>This simplifies the final rendering pipeline design. Only four classes (actual classes, not temporary structures in some plugin) can render to HTML: <code>Item</code>, <code>ItemGroup</code> (tags, categories, yearly archives, gallery indexes), <code>DirectoryTreeGroup</code> (listings), and <code>LinkGroup</code> (archive and tag indexes). Each has a corresponding template model. Nikola’s sitemap generator recurses through the <code>output</code> directory to find files, but YABG can just use the lists of items and groups. The sitemap won’t include HTML files from the files folder, but I don’t need them there (though I could add them if needed).</p>
<h3 id="windows-first-linux-in-zero-time">Windows first, Linux in zero time</h3>
<p>I developed YABG entirely on Windows. This forced me to think about paths and URLs as separate concepts. I couldn’t use most <code>System.IO.Path</code> facilities for URLs, since they would produce backslashes. As a result, there are zero bugs where backslashes leak into output on Windows. Nikola has such bugs pop up occasionally; indeed, <a href="https://github.com/getnikola/nikola/commit/d8d94c047cdc1718700f0b5d00627722241be68d">I fixed one yesterday</a>.</p>
<p>But when YABG was nearly complete, I ran it on Linux. And it just worked. No code changes needed. No output differences. (I had to add <code>SkiaSharp.NativeAssets.Linux</code> and <code>apt install libfontconfig1</code> since I was stilll using SkiaSharp at that point, but that’s no longer needed with Magick.NET.)</p>
<p>Not everything is perfect, though. I added a <code>--watch</code> mode based on <code>FileSystemWatcher</code>, but it doesn’t work on Linux. I don’t <em>need</em> it there; I’d have to switch to polling to make it work.</p>
<h3 id="dependency-injection-everywhere">Dependency injection everywhere</h3>
<p>A good principle used in object-oriented development (though not very often in Python) is <strong>dependency injection</strong>.  I have several grouping services, all implementing either <code>IPostGrouper</code> or <code>IItemGrouper</code>. They’re registered in the DI container as implementations of those interfaces. The <code>GroupEngine</code> doesn’t need to know about specific group types, it just gets them from the container and passes the post and item arrays.</p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_c29b1ed1e30a3b34f36f210df79414927ba24273-1"><code data-line-number="1"></code></a></td><td class="code"><code><a id="code_c29b1ed1e30a3b34f36f210df79414927ba24273-1" name="code_c29b1ed1e30a3b34f36f210df79414927ba24273-1"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">AddScoped</span><span class="o">&lt;</span><span class="n">IPostGrouper</span><span class="p">,</span><span class="w"> </span><span class="n">ArchiveGrouper</span><span class="o">&gt;</span><span class="p">()</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_c29b1ed1e30a3b34f36f210df79414927ba24273-2"><code data-line-number="2"></code></a></td><td class="code"><code><a id="code_c29b1ed1e30a3b34f36f210df79414927ba24273-2" name="code_c29b1ed1e30a3b34f36f210df79414927ba24273-2"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">AddScoped</span><span class="o">&lt;</span><span class="n">IPostGrouper</span><span class="p">,</span><span class="w"> </span><span class="n">GuideGrouper</span><span class="o">&gt;</span><span class="p">()</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_c29b1ed1e30a3b34f36f210df79414927ba24273-3"><code data-line-number="3"></code></a></td><td class="code"><code><a id="code_c29b1ed1e30a3b34f36f210df79414927ba24273-3" name="code_c29b1ed1e30a3b34f36f210df79414927ba24273-3"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">AddScoped</span><span class="o">&lt;</span><span class="n">IPostGrouper</span><span class="p">,</span><span class="w"> </span><span class="n">IndexGrouper</span><span class="o">&gt;</span><span class="p">()</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_c29b1ed1e30a3b34f36f210df79414927ba24273-4"><code data-line-number="4"></code></a></td><td class="code"><code><a id="code_c29b1ed1e30a3b34f36f210df79414927ba24273-4" name="code_c29b1ed1e30a3b34f36f210df79414927ba24273-4"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">AddScoped</span><span class="o">&lt;</span><span class="n">IPostGrouper</span><span class="p">,</span><span class="w"> </span><span class="n">NavigationGrouper</span><span class="o">&gt;</span><span class="p">()</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_c29b1ed1e30a3b34f36f210df79414927ba24273-5"><code data-line-number="5"></code></a></td><td class="code"><code><a id="code_c29b1ed1e30a3b34f36f210df79414927ba24273-5" name="code_c29b1ed1e30a3b34f36f210df79414927ba24273-5"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">AddScoped</span><span class="o">&lt;</span><span class="n">IPostGrouper</span><span class="p">,</span><span class="w"> </span><span class="n">TagCategoryGrouper</span><span class="o">&gt;</span><span class="p">()</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_c29b1ed1e30a3b34f36f210df79414927ba24273-6"><code data-line-number="6"></code></a></td><td class="code"><code><a id="code_c29b1ed1e30a3b34f36f210df79414927ba24273-6" name="code_c29b1ed1e30a3b34f36f210df79414927ba24273-6"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">AddScoped</span><span class="o">&lt;</span><span class="n">IItemGrouper</span><span class="p">,</span><span class="w"> </span><span class="n">GalleryIndexGrouper</span><span class="o">&gt;</span><span class="p">()</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_c29b1ed1e30a3b34f36f210df79414927ba24273-7"><code data-line-number="7"></code></a></td><td class="code"><code><a id="code_c29b1ed1e30a3b34f36f210df79414927ba24273-7" name="code_c29b1ed1e30a3b34f36f210df79414927ba24273-7"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">AddScoped</span><span class="o">&lt;</span><span class="n">IItemGrouper</span><span class="p">,</span><span class="w"> </span><span class="n">ListingIndexGrouper</span><span class="o">&gt;</span><span class="p">()</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_c29b1ed1e30a3b34f36f210df79414927ba24273-8"><code data-line-number="8"></code></a></td><td class="code"><code><a id="code_c29b1ed1e30a3b34f36f210df79414927ba24273-8" name="code_c29b1ed1e30a3b34f36f210df79414927ba24273-8"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">AddScoped</span><span class="o">&lt;</span><span class="n">IItemGrouper</span><span class="p">,</span><span class="w"> </span><span class="n">ProjectGrouper</span><span class="o">&gt;</span><span class="p">()</span>
</code></td></tr></table></div>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-1"><code data-line-number=" 1"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-1" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-1"></a><span class="k">internal</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nf">GroupEngine</span><span class="p">(</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-2"><code data-line-number=" 2"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-2" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-2"></a><span class="w">&nbsp;&nbsp;</span><span class="n">IEnumerable</span><span class="o">&lt;</span><span class="n">IItemGrouper</span><span class="o">&gt;</span><span class="w"> </span><span class="n">itemGroupers</span><span class="p">,</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-3"><code data-line-number=" 3"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-3" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-3"></a><span class="w">&nbsp;&nbsp;</span><span class="n">IEnumerable</span><span class="o">&lt;</span><span class="n">IPostGrouper</span><span class="o">&gt;</span><span class="w"> </span><span class="n">postGroupers</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-4"><code data-line-number=" 4"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-4" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-4"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">:</span><span class="w"> </span><span class="n">IGroupEngine</span><span class="w"> </span><span class="p">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-5"><code data-line-number=" 5"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-5" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-5"></a><span class="w">&nbsp;&nbsp;</span><span class="k">public</span><span class="w"> </span><span class="n">IEnumerable</span><span class="o">&lt;</span><span class="n">IGroup</span><span class="o">&gt;</span><span class="w"> </span><span class="n">GenerateGroups</span><span class="p">(</span><span class="n">Item</span><span class="p">[]</span><span class="w"> </span><span class="n">items</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-6"><code data-line-number=" 6"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-6" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-6"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kt">var</span><span class="w"> </span><span class="n">sortedItems</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">items</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-7"><code data-line-number=" 7"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-7" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-7"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">OrderByDescending</span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">i</span><span class="p">.</span><span class="n">Published</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-8"><code data-line-number=" 8"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-8" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-8"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">ThenBy</span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">i</span><span class="p">.</span><span class="n">SourcePath</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-9"><code data-line-number=" 9"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-9" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-9"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">ToArray</span><span class="p">();</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-10"><code data-line-number="10"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-10" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-10"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-11"><code data-line-number="11"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-11" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-11"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kt">var</span><span class="w"> </span><span class="n">sortedPosts</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sortedItems</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-12"><code data-line-number="12"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-12" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-12"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">Where</span><span class="p">(</span><span class="n">item</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">item</span><span class="p">.</span><span class="n">Type</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">ItemType</span><span class="p">.</span><span class="n">Post</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-13"><code data-line-number="13"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-13" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-13"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">ToArray</span><span class="p">();</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-14"><code data-line-number="14"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-14" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-14"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-15"><code data-line-number="15"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-15" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-15"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kt">var</span><span class="w"> </span><span class="n">itemGroups</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">itemGroupers</span><span class="p">.</span><span class="n">SelectMany</span><span class="p">(</span><span class="n">g</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="p">.</span><span class="n">GroupItems</span><span class="p">(</span><span class="n">sortedItems</span><span class="p">));</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-16"><code data-line-number="16"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-16" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-16"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kt">var</span><span class="w"> </span><span class="n">postGroups</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">postGroupers</span><span class="p">.</span><span class="n">SelectMany</span><span class="p">(</span><span class="n">g</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="p">.</span><span class="n">GroupPosts</span><span class="p">(</span><span class="n">sortedPosts</span><span class="p">));</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-17"><code data-line-number="17"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-17" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-17"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="k">return</span><span class="w"> </span><span class="n">itemGroups</span><span class="p">.</span><span class="n">Concat</span><span class="p">(</span><span class="n">postGroups</span><span class="p">);</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-18"><code data-line-number="18"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-18" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-18"></a><span class="w">&nbsp;&nbsp;</span><span class="p">}</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-19"><code data-line-number="19"></code></a></td><td class="code"><code><a id="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-19" name="code_badc33d3269d7f0a3c6b6177683f5988ea9b2ab3-19"></a><span class="p">}</span>
</code></td></tr></table></div>
<p>The <code>ItemRenderEngine</code> has a slightly different challenge: it needs to pick the correct renderer for the post (Gallery, HTML, Listing, Markdown). The renderers are registered as keyed services. The render engine does not need to know anything about the specific renderer types, it just gets the renderer name from the <code>SourceItem</code>’s <code>ScanPattern</code> (so ultimately from the configuration file) and asks the DI container to provide it with the right implementation.</p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_b40867aeb78b11d9d5f25c39bec3682c19216014-1"><code data-line-number="1"></code></a></td><td class="code"><code><a id="code_b40867aeb78b11d9d5f25c39bec3682c19216014-1" name="code_b40867aeb78b11d9d5f25c39bec3682c19216014-1"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">AddKeyedScoped</span><span class="o">&lt;</span><span class="n">IItemRenderer</span><span class="p">,</span><span class="w"> </span><span class="n">GalleryItemRenderer</span><span class="o">&gt;</span><span class="p">(</span><span class="n">GalleryItemRenderer</span><span class="p">.</span><span class="n">Name</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_b40867aeb78b11d9d5f25c39bec3682c19216014-2"><code data-line-number="2"></code></a></td><td class="code"><code><a id="code_b40867aeb78b11d9d5f25c39bec3682c19216014-2" name="code_b40867aeb78b11d9d5f25c39bec3682c19216014-2"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">AddKeyedScoped</span><span class="o">&lt;</span><span class="n">IItemRenderer</span><span class="p">,</span><span class="w"> </span><span class="n">HtmlItemRenderer</span><span class="o">&gt;</span><span class="p">(</span><span class="n">HtmlItemRenderer</span><span class="p">.</span><span class="n">Name</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_b40867aeb78b11d9d5f25c39bec3682c19216014-3"><code data-line-number="3"></code></a></td><td class="code"><code><a id="code_b40867aeb78b11d9d5f25c39bec3682c19216014-3" name="code_b40867aeb78b11d9d5f25c39bec3682c19216014-3"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">AddKeyedScoped</span><span class="o">&lt;</span><span class="n">IItemRenderer</span><span class="p">,</span><span class="w"> </span><span class="n">ListingItemRenderer</span><span class="o">&gt;</span><span class="p">(</span><span class="n">ListingItemRenderer</span><span class="p">.</span><span class="n">Name</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_b40867aeb78b11d9d5f25c39bec3682c19216014-4"><code data-line-number="4"></code></a></td><td class="code"><code><a id="code_b40867aeb78b11d9d5f25c39bec3682c19216014-4" name="code_b40867aeb78b11d9d5f25c39bec3682c19216014-4"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">AddKeyedScoped</span><span class="o">&lt;</span><span class="n">IItemRenderer</span><span class="p">,</span><span class="w"> </span><span class="n">MarkdownItemRenderer</span><span class="o">&gt;</span><span class="p">(</span><span class="n">MarkdownItemRenderer</span><span class="p">.</span><span class="n">Name</span><span class="p">)</span>
</code></td></tr></table></div>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-1"><code data-line-number=" 1"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-1" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-1"></a><span class="w">&nbsp;&nbsp;</span><span class="k">public</span><span class="w"> </span><span class="k">async</span><span class="w"> </span><span class="n">Task</span><span class="o">&lt;</span><span class="n">IEnumerable</span><span class="o">&lt;</span><span class="n">Item</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">Render</span><span class="p">(</span><span class="n">IEnumerable</span><span class="o">&lt;</span><span class="n">SourceItem</span><span class="o">&gt;</span><span class="w"> </span><span class="n">sourceItems</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-2"><code data-line-number=" 2"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-2" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-2"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kt">var</span><span class="w"> </span><span class="n">renderTasks</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">sourceItems</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-3"><code data-line-number=" 3"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-3" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-3"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">GroupBy</span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">i</span><span class="p">.</span><span class="n">ScanPattern</span><span class="p">.</span><span class="n">RendererName</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-4"><code data-line-number=" 4"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-4" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-4"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">Select</span><span class="p">(</span><span class="k">group</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-5"><code data-line-number=" 5"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-5" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-5"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kt">var</span><span class="w"> </span><span class="n">renderer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_keyedServiceProvider</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-6"><code data-line-number=" 6"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-6" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-6"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">.</span><span class="n">GetRequiredKeyedService</span><span class="o">&lt;</span><span class="n">IItemRenderer</span><span class="o">&gt;</span><span class="p">(</span><span class="k">group</span><span class="p">.</span><span class="n">Key</span><span class="p">);</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-7"><code data-line-number=" 7"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-7" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-7"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="k">return</span><span class="w"> </span><span class="n">renderer</span><span class="w"> </span><span class="k">switch</span><span class="w"> </span><span class="p">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-8"><code data-line-number=" 8"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-8" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-8"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="n">IBulkItemRenderer</span><span class="w"> </span><span class="n">bulkRenderer</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">bulkRenderer</span><span class="p">.</span><span class="n">RenderItems</span><span class="p">(</span><span class="k">group</span><span class="p">),</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-9"><code data-line-number=" 9"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-9" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-9"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="n">ISingleItemRenderer</span><span class="w"> </span><span class="n">singleRenderer</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">Task</span><span class="p">.</span><span class="n">WhenAll</span><span class="p">(</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-10"><code data-line-number="10"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-10" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-10"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="k">group</span><span class="p">.</span><span class="n">Select</span><span class="p">(</span><span class="n">singleRenderer</span><span class="p">.</span><span class="n">RenderItem</span><span class="p">)),</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-11"><code data-line-number="11"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-11" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-11"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="k">throw</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">InvalidOperationException</span><span class="p">(</span><span class="s">&quot;Unexpected renderer type&quot;</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-12"><code data-line-number="12"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-12" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-12"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">};</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-13"><code data-line-number="13"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-13" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-13"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">});</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/16/i-wrote-yet-another-blog-generator/#code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-14"><code data-line-number="14"></code></a></td><td class="code"><code><a id="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-14" name="code_10e51bbfc1002bc3932ecb96e587f1b3dad5958b-14"></a><span class="w">&nbsp;&nbsp;</span><span class="p">}</span>
</code></td></tr></table></div>
<p>In total, there are <strong>37</strong> specific service implementations registered (plus system services like <code>TimeProvider</code> and logging). Beyond these two examples, the main benefit is <strong>testability</strong>. I can write unit tests without dependencies on unrelated services, and without monkey-patching random names. (In Python, <code>unittest.mock</code> does both monkey-patching <em>and</em> mocking.)</p>
<p>Okay, I haven’t written very many tests, but I could easily ask an LLM to do it.</p>
<h3 id="immutable-data-structures-and-no-global-state">Immutable data structures and no global state</h3>
<p>All classes are immutable. This helps in several ways. It’s easier to reason about state when <code>SourceItem</code> becomes <code>Item</code> during rendering, compared to a single class with a nullable <code>Content</code> property. Immutability also makes concurrency safer. But the biggest win is how easy it was to develop the <code>--watch</code> mode. Every service has <code>Scoped</code> lifetime, and main logic lives in <code>IMainEngine</code>. I can just create a new scope, get the engine, and run it without state leaking between executions. No subprocess launching, no state resetting — everything disappears when the scope is disposed.</p>
<h2 id="can-anyone-use-it">Can anyone use it?</h2>
<p>On one hand, it’s open source under the 3-clause BSD license and <a href="https://github.com/Kwpolska/YetAnotherBlogGenerator">available on GitHub</a>.</p>
<p>On the other hand, it’s more of a source-available project. There are no docs, and it was designed specifically for this site (so some things are probably too hardcoded for your needs). In fact, this blog’s configuration and templates were directly hardcoded in the codebase until the day before launch. But I’m happy to answer questions and review pull requests!</p>
]]></content:encoded><category>C#/.NET</category><category>.NET</category><category>C#</category><category>Nikola</category><category>Python</category><category>static site generators</category><category>web development</category><category>YetAnotherBlogGenerator</category></item><item><title>Deploying Python Web Applications with Docker</title><dc:creator>Chris Warrick</dc:creator><link>https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/</link><pubDate>Fri, 06 Feb 2026 19:45:00 GMT</pubDate><guid>https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/</guid><description>Ten years ago, almost to the day, I wrote a very long blog post titled Deploying Python Web Applications with nginx and uWSGI Emperor. This week, I’ve migrated the Python web applications hosted on my VPS to Docker containers. Here are some reasons why, and all my Docker files to help you do this on your server.
</description><content:encoded><![CDATA[<p>Ten years ago, almost to the day, I wrote a very long blog post titled <a href="https://chriswarrick.com/blog/2016/02/10/deploying-python-web-apps-with-nginx-and-uwsgi-emperor/">Deploying Python Web Applications with nginx and uWSGI Emperor</a>. This week, I’ve migrated the Python web applications hosted on my VPS to Docker containers. Here are some reasons why, and all my Docker files to help you do this on your server.</p>



<p>You can jump to the <a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#scripts-and-configuration-files">scripts and configuration files</a> if you don’t care about the theory and just want to see the end result.</p>
<h2 id="why-docker">Why Docker?</h2>
<p>Docker is a technology that has taken the software engineering world by storm. The main promise is isolation: a Docker container that works on an x86_64 Linux machine will work on any x86_64 Linux machine in the same way. Want to quickly set up PostgreSQL for testing? Just run <code>docker run --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d --restart=unless-stopped postgres</code> and wait a few seconds. Docker is great for deployment as well as production deployments, and it even supports Windows Server containers these days (whether or not this is a pleasant experience is a different question).</p>
<p>Of course, there is a trade-off: running something in a Docker container requires more disk space than running the same software outside of Docker would. This is because Docker containers have their own copies of <em>everything</em>: the C library, the shell, core commands, and the runtime of your favorite language. But this is not a bug, it’s a feature.</p>
<p>If you read <a href="https://chriswarrick.com/blog/2016/02/10/deploying-python-web-apps-with-nginx-and-uwsgi-emperor/">the nginx/uWSGI Emperor guide</a> I wrote ten years ago, you might notice there are many cases where the configuration differs depending on the Linux distribution in use. Some distributions made logging an optional feature, others have it built in. Each distribution has a slightly different directory structure, and different users and groups for Web services. Some distros did not ship systemd service files. Red Hat is still pushing SELinux.</p>
<p>But uWSGI is not the only pain point. There’s also the system Python. Every distribution treats it differently and applies different customizations. Arch maps <code>python</code> to <code>python3</code>, but other distributions do not. Arch and Fedora ship a single Python package, while Debian/Ubuntu have many split packages. Taking a dependency on the system Python also makes distro upgrades harder: if the system Python is upgraded, the virtual environment needs to be recreated. (Hope you have an up-to-date <code>requirements.txt</code>!)</p>
<h2 id="should-everything-be-in-docker">Should everything be in Docker?</h2>
<p>It depends on your definition of <em>everything</em>. I ended up with only one non-dockerized application (which requires access to resources that cannot easily be provided to it when it is in a container): an <a href="https://dotnet.microsoft.com/en-us/apps/aspnet">ASP.NET Core</a> Minimal API compiled with <a href="https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/">Native AOT</a>, so its maintenance burden is essentially zero. Except for that app, all web applications on my VPS, whether written in PHP, Python, or .NET, run in Docker. I don't need to figure out how to make PHP happy; I use a container image in which someone else had done the hard parts for me. I don't need to maintain Python virtual environments; they can just live inside containers. The maintenance burden for .NET is smaller, but containers still prevent issues that could be caused by removing an old version of .NET, for example. (Those apps are ASP.NET Core MVC, so they are not compatible with Native AOT.)</p>
<p>But I am not running the Web-facing nginx instance in Docker. Similarly, I’ve kept PostgreSQL outside of Docker, and I’ve just un-dockerized a MariaDB instance. The main difference here is that I can <code>apt install nginx</code> and get a supported and secure build, with default configuration that might be more reasonable than the default. (But then again, <code>apt install python3</code> gets you a fairly mediocre build.)</p>
<p>For the Python dockerization project, my requirements are fairly simple. The Docker container only needs Python, a venv with dependencies installed, and all data files of the app. The database exists outside of Docker, and I’ve already configured PostgreSQL to listen on an address accessible from within Docker (but of course, not from the public Internet). Because Django and Python have abysmal performance, static files must be served by nginx. Since I don’t want a dedicated nginx in a Docker container, the easiest solution is to mount a folder as a volume: Django runs <code>collectstatic</code> inside the container to write static files there, and the host nginx serves them.</p>
<h2 id="what-should-be-in-docker">What should be in Docker?</h2>
<p>There are four things that we need to put in our Docker container: Linux, Python, a venv, and a WSGI server.</p>
<h3 id="linux-and-python-base-image">Linux and Python (base image)</h3>
<p>The choice of a base image can affect a few things about the Docker experience. For Python, the most commonly used image is the one prepared by Docker, Inc. simply named <code>python</code>. That image has three versions, or tags in Docker parlance:</p>
<ul>
<li><code>python:3.14</code> (the default), which is based on Debian (1.12GB)</li>
<li><code>python:3.14-slim</code>, which is based on Debian but with less cruft (119MB)</li>
<li><code>python:3.14-alpine</code>, which is bvased on Alpine Linux (47.4MB)</li>
</ul>
<p>Alpine Linux images are really small, but with the caveat that they are based on the <code>musl</code> libc instead of GNU <code>glibc</code>, which means most binaries, including <code>manylinux</code> binary wheels, do not work, and special builds (<code>musllinux</code> binary wheels in this case) are required. I started with the Debian-based images (the default one for build, the slim one for production), but I switched to <code>python:3.14-alpine</code>, as the only binary dependency I have is <code>psycopg2</code>, which I build from source, but there are <code>musllinux</code> wheels of <code>psycopg2-binary</code> available.</p>
<h3 id="virtual-environments-and-dependency-management">Virtual environments and dependency management</h3>
<p>Python packaging is still a mess. I don’t feel like using the VC-backed <code>uv</code>, and the other big tools (like <code>poetry</code> or <code>pipenv</code>) introduce too much magic and bloat. All I need is a <code>requirements.txt</code> file I can install with pip into a venv, but without having to manually track version numbers. For that, <a href="https://github.com/jazzband/pip-tools"><code>pip-compile</code> from <code>pip-tools</code></a> is a great option. It takes a <code>requirements.in</code> file with package names and optional version ranges, and produces a <code>requirements.txt</code> with all dependencies (including transitive dependencies) pinned to exact versions. It doesn’t get much simpler than that.</p>
<p>There is one issue: the <code>requirements.txt</code> files produced by <code>pip-compile</code> are specific to the environment in which it was executed. So if you want a <code>requirements.txt</code> for Linux and Python 3.14, you must run <code>pip-compile</code> on Linux with Python 3.14. While this does not matter much for this project (it has very simple dependencies), I wanted to ensure the tool runs with the same Python version the container will use, and to allow building the image without having <code>pip-compile</code> installed on the development machine.</p>
<p>So, I quickly hacked together a tool unoriginally named <a href="https://github.com/Kwpolska/docker-pip-compile"><code>docker-pip-compile</code></a>. It’s a five-line <code>Dockerfile</code> and a slightly longer shell script to help run it. That way, dependencies can be updated and the entire project can be built even without a functional system Python. The only catch here (and the reason for the hackiest line in the Dockerfile) is the fact that the package must be buildable in the environment where <code>pip-compile</code> runs, so I had to install <code>libpq</code> (the PostgreSQL client library) there.</p>
<h3 id="wsgi-server">WSGI server</h3>
<p>The old post used uWSGI (it’s even mentioned in the title). Sadly, <a href="https://github.com/unbit/uwsgi/commit/5838086dd4490b8a55ff58fc0bf0f108caa4e079">uWSGI has been in maintenance mode since 2022</a>. On the other hand, <a href="https://github.com/benoitc/gunicorn">gunicorn</a> is doing pretty well, so I used that. I also decided to add <a href="https://uvicorn.dev/">uvicorn</a> to the mix, because why not.</p>
<h2 id="scripts-and-configuration-files">Scripts and configuration files</h2>
<p>You might be able to find a more up-to-date version of those scripts in the <a href="https://github.com/getnikola/nikola-users/tree/master/docker">nikola-users</a> repository.</p>
<h3 id="dockerfile">Dockerfile</h3>
<p>The Dockerfile is pretty straightforward. To save a little disk space, I use a multi-stage build. The build stage sets up a virtual environment, installs packages into it, and copies files into <code>/app</code>. The final stage installs <code>libpq</code> (the PostgreSQL client library, needed for <code>psycopg2</code>) and copies <code>/venv</code> and <code>/app</code> from the build stage. (The disk space savings come from not having <code>build-base</code> and <code>libpq-dev</code> in the final image.)</p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-1"><code data-line-number=" 1"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-1" name="code_381600d5da99646e75aaa66100a72b972eb5088c-1"></a><span class="k">FROM</span><span class="w"> </span><span class="s">python:3.14-alpine</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="s">build</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-2"><code data-line-number=" 2"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-2" name="code_381600d5da99646e75aaa66100a72b972eb5088c-2"></a><span class="k">WORKDIR</span><span class="w"> </span><span class="s">/app</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-3"><code data-line-number=" 3"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-3" name="code_381600d5da99646e75aaa66100a72b972eb5088c-3"></a><span class="k">RUN</span><span class="w"> </span>apk<span class="w"> </span>add<span class="w"> </span>build-base<span class="w"> </span>libpq<span class="w"> </span>libpq-dev
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-4"><code data-line-number=" 4"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-4" name="code_381600d5da99646e75aaa66100a72b972eb5088c-4"></a><span class="k">RUN</span><span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>venv<span class="w"> </span>/venv<span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span>/venv/bin/python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>--no-cache-dir<span class="w"> </span>-U<span class="w"> </span>pip
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-5"><code data-line-number=" 5"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-5" name="code_381600d5da99646e75aaa66100a72b972eb5088c-5"></a><span class="k">COPY</span><span class="w"> </span>requirements.txt<span class="w"> </span>/app
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-6"><code data-line-number=" 6"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-6" name="code_381600d5da99646e75aaa66100a72b972eb5088c-6"></a><span class="k">RUN</span><span class="w"> </span>/venv/bin/pip<span class="w"> </span>install<span class="w"> </span>--no-cache-dir<span class="w"> </span>-r<span class="w"> </span>requirements.txt
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-7"><code data-line-number=" 7"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-7" name="code_381600d5da99646e75aaa66100a72b972eb5088c-7"></a><span class="k">COPY</span><span class="w"> </span>nikolausers<span class="w"> </span>/app/nikolausers
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-8"><code data-line-number=" 8"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-8" name="code_381600d5da99646e75aaa66100a72b972eb5088c-8"></a><span class="k">COPY</span><span class="w"> </span>sites<span class="w"> </span>/app/sites
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-9"><code data-line-number=" 9"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-9" name="code_381600d5da99646e75aaa66100a72b972eb5088c-9"></a><span class="k">COPY</span><span class="w"> </span>templates<span class="w"> </span>/app/templates
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-10"><code data-line-number="10"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-10" name="code_381600d5da99646e75aaa66100a72b972eb5088c-10"></a><span class="k">COPY</span><span class="w"> </span>manage.py<span class="w"> </span>docker/docker-entrypoint.sh<span class="w"> </span>/app/
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-11"><code data-line-number="11"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-11" name="code_381600d5da99646e75aaa66100a72b972eb5088c-11"></a><span class="k">RUN</span><span class="w"> </span>chmod<span class="w"> </span>+x<span class="w"> </span>/app/docker-entrypoint.sh
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-12"><code data-line-number="12"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-12" name="code_381600d5da99646e75aaa66100a72b972eb5088c-12"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-13"><code data-line-number="13"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-13" name="code_381600d5da99646e75aaa66100a72b972eb5088c-13"></a><span class="k">FROM</span><span class="w"> </span><span class="s">python:3.14-alpine</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-14"><code data-line-number="14"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-14" name="code_381600d5da99646e75aaa66100a72b972eb5088c-14"></a><span class="k">WORKDIR</span><span class="w"> </span><span class="s">/app</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-15"><code data-line-number="15"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-15" name="code_381600d5da99646e75aaa66100a72b972eb5088c-15"></a><span class="k">RUN</span><span class="w"> </span>apk<span class="w"> </span>add<span class="w"> </span>libpq
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-16"><code data-line-number="16"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-16" name="code_381600d5da99646e75aaa66100a72b972eb5088c-16"></a><span class="k">COPY</span><span class="w"> </span>--from<span class="o">=</span>build<span class="w"> </span>/venv<span class="w"> </span>/venv
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-17"><code data-line-number="17"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-17" name="code_381600d5da99646e75aaa66100a72b972eb5088c-17"></a><span class="k">COPY</span><span class="w"> </span>--from<span class="o">=</span>build<span class="w"> </span>/app<span class="w"> </span>/app
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_381600d5da99646e75aaa66100a72b972eb5088c-18"><code data-line-number="18"></code></a></td><td class="code"><code><a id="code_381600d5da99646e75aaa66100a72b972eb5088c-18" name="code_381600d5da99646e75aaa66100a72b972eb5088c-18"></a><span class="k">ENTRYPOINT</span><span class="w"> </span><span class="p">[</span><span class="s2">&quot;/app/docker-entrypoint.sh&quot;</span><span class="p">]</span>
</code></td></tr></table></div>
<h3 id="docker-entrypoint.sh">docker-entrypoint.sh</h3>
<p>The entrypoint is fairly simple as well. Before starting gunicorn, we need to run migrations and collect static files. To avoid repeating this on container restarts, we create a marker file inside the container to indicate whether setup has already completed. I use three workers, which should be enough for those sites.</p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_77ec481338d658dd68a97d1b6cf252576c12e963-1"><code data-line-number="1"></code></a></td><td class="code"><code><a id="code_77ec481338d658dd68a97d1b6cf252576c12e963-1" name="code_77ec481338d658dd68a97d1b6cf252576c12e963-1"></a><span class="ch">#!/bin/sh</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_77ec481338d658dd68a97d1b6cf252576c12e963-2"><code data-line-number="2"></code></a></td><td class="code"><code><a id="code_77ec481338d658dd68a97d1b6cf252576c12e963-2" name="code_77ec481338d658dd68a97d1b6cf252576c12e963-2"></a><span class="nv">statefile</span><span class="o">=</span>/tmp/migrated
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_77ec481338d658dd68a97d1b6cf252576c12e963-3"><code data-line-number="3"></code></a></td><td class="code"><code><a id="code_77ec481338d658dd68a97d1b6cf252576c12e963-3" name="code_77ec481338d658dd68a97d1b6cf252576c12e963-3"></a><span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span>!<span class="w"> </span>-f<span class="w"> </span><span class="s2">&quot;</span><span class="nv">$statefile</span><span class="s2">&quot;</span><span class="w"> </span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_77ec481338d658dd68a97d1b6cf252576c12e963-4"><code data-line-number="4"></code></a></td><td class="code"><code><a id="code_77ec481338d658dd68a97d1b6cf252576c12e963-4" name="code_77ec481338d658dd68a97d1b6cf252576c12e963-4"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span>/venv/bin/python<span class="w"> </span>manage.py<span class="w"> </span>collectstatic<span class="w"> </span>--noinput
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_77ec481338d658dd68a97d1b6cf252576c12e963-5"><code data-line-number="5"></code></a></td><td class="code"><code><a id="code_77ec481338d658dd68a97d1b6cf252576c12e963-5" name="code_77ec481338d658dd68a97d1b6cf252576c12e963-5"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span>/venv/bin/python<span class="w"> </span>manage.py<span class="w"> </span>migrate<span class="w"> </span>--noinput
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_77ec481338d658dd68a97d1b6cf252576c12e963-6"><code data-line-number="6"></code></a></td><td class="code"><code><a id="code_77ec481338d658dd68a97d1b6cf252576c12e963-6" name="code_77ec481338d658dd68a97d1b6cf252576c12e963-6"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span>touch<span class="w"> </span><span class="s2">&quot;</span><span class="nv">$statefile</span><span class="s2">&quot;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_77ec481338d658dd68a97d1b6cf252576c12e963-7"><code data-line-number="7"></code></a></td><td class="code"><code><a id="code_77ec481338d658dd68a97d1b6cf252576c12e963-7" name="code_77ec481338d658dd68a97d1b6cf252576c12e963-7"></a><span class="k">fi</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_77ec481338d658dd68a97d1b6cf252576c12e963-8"><code data-line-number="8"></code></a></td><td class="code"><code><a id="code_77ec481338d658dd68a97d1b6cf252576c12e963-8" name="code_77ec481338d658dd68a97d1b6cf252576c12e963-8"></a><span class="nb">exec</span><span class="w"> </span>/venv/bin/python<span class="w"> </span>-m<span class="w"> </span>gunicorn<span class="w"> </span>--bind<span class="w"> </span><span class="m">0</span>.0.0.0:6868<span class="w"> </span>nikolausers.asgi:application<span class="w"> </span>-k<span class="w"> </span>uvicorn_worker.UvicornWorker<span class="w"> </span>-w<span class="w"> </span><span class="m">3</span>
</code></td></tr></table></div>
<h3 id="docker-compose.yml">docker-compose.yml</h3>
<p>I mentioned that only one service runs inside Docker, since nginx and PostgreSQL live outside. Docker Compose may feel unnecessary with just one service, but it is still a convenient way to keep the required configuration in a file.</p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-1"><code data-line-number=" 1"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-1" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-1"></a>services:
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-2"><code data-line-number=" 2"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-2" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-2"></a><span class="w">&nbsp;&nbsp;</span>nikolausers:
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-3"><code data-line-number=" 3"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-3" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-3"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span>restart:<span class="w"> </span>unless-stopped
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-4"><code data-line-number=" 4"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-4" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-4"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span>image:<span class="w"> </span>nikolausers:latest
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-5"><code data-line-number=" 5"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-5" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-5"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span>ports:
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-6"><code data-line-number=" 6"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-6" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-6"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>-<span class="w"> </span><span class="m">127</span>.0.0.1:6868:6868
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-7"><code data-line-number=" 7"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-7" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-7"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span>user:<span class="w"> </span><span class="s2">&quot;33:33&quot;</span><span class="w"> </span><span class="c1"># www-data on Debian</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-8"><code data-line-number=" 8"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-8" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-8"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span>environment:
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-9"><code data-line-number=" 9"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-9" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-9"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c1">#SECRET_KEY: &quot;&quot;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-10"><code data-line-number="10"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-10" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-10"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c1">#DB_NAME: &quot;&quot;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-11"><code data-line-number="11"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-11" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-11"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c1">#DB_USER: &quot;&quot;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-12"><code data-line-number="12"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-12" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-12"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c1">#DB_PASSWORD: &quot;&quot;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-13"><code data-line-number="13"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-13" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-13"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c1">#DB_HOST: &quot;&quot;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-14"><code data-line-number="14"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-14" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-14"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c1">#EMAIL_HOST: &quot;&quot;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-15"><code data-line-number="15"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-15" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-15"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span>volumes:
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_0df510911c34864d8036ab1906f2ab9cb403c19f-16"><code data-line-number="16"></code></a></td><td class="code"><code><a id="code_0df510911c34864d8036ab1906f2ab9cb403c19f-16" name="code_0df510911c34864d8036ab1906f2ab9cb403c19f-16"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>-<span class="w"> </span>./static:/app/static
</code></td></tr></table></div>
<h3 id="nginx">nginx</h3>
<p>The nginx configuration is as simple as it gets:</p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_8c7aabea1826b4da5011effecea7431edf34cb4e-1"><code data-line-number=" 1"></code></a></td><td class="code"><code><a id="code_8c7aabea1826b4da5011effecea7431edf34cb4e-1" name="code_8c7aabea1826b4da5011effecea7431edf34cb4e-1"></a><span class="k">server</span><span class="w"> </span><span class="p">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_8c7aabea1826b4da5011effecea7431edf34cb4e-2"><code data-line-number=" 2"></code></a></td><td class="code"><code><a id="code_8c7aabea1826b4da5011effecea7431edf34cb4e-2" name="code_8c7aabea1826b4da5011effecea7431edf34cb4e-2"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="c1"># skip standard host config...</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_8c7aabea1826b4da5011effecea7431edf34cb4e-3"><code data-line-number=" 3"></code></a></td><td class="code"><code><a id="code_8c7aabea1826b4da5011effecea7431edf34cb4e-3" name="code_8c7aabea1826b4da5011effecea7431edf34cb4e-3"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_8c7aabea1826b4da5011effecea7431edf34cb4e-4"><code data-line-number=" 4"></code></a></td><td class="code"><code><a id="code_8c7aabea1826b4da5011effecea7431edf34cb4e-4" name="code_8c7aabea1826b4da5011effecea7431edf34cb4e-4"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kn">location</span><span class="w"> </span><span class="s">/</span><span class="w"> </span><span class="p">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_8c7aabea1826b4da5011effecea7431edf34cb4e-5"><code data-line-number=" 5"></code></a></td><td class="code"><code><a id="code_8c7aabea1826b4da5011effecea7431edf34cb4e-5" name="code_8c7aabea1826b4da5011effecea7431edf34cb4e-5"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kn">proxy_pass</span><span class="w"> </span><span class="s">http://127.0.0.1:6868/</span><span class="p">;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_8c7aabea1826b4da5011effecea7431edf34cb4e-6"><code data-line-number=" 6"></code></a></td><td class="code"><code><a id="code_8c7aabea1826b4da5011effecea7431edf34cb4e-6" name="code_8c7aabea1826b4da5011effecea7431edf34cb4e-6"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kn">include</span><span class="w"> </span><span class="s">proxy_params</span><span class="p">;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_8c7aabea1826b4da5011effecea7431edf34cb4e-7"><code data-line-number=" 7"></code></a></td><td class="code"><code><a id="code_8c7aabea1826b4da5011effecea7431edf34cb4e-7" name="code_8c7aabea1826b4da5011effecea7431edf34cb4e-7"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">}</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_8c7aabea1826b4da5011effecea7431edf34cb4e-8"><code data-line-number=" 8"></code></a></td><td class="code"><code><a id="code_8c7aabea1826b4da5011effecea7431edf34cb4e-8" name="code_8c7aabea1826b4da5011effecea7431edf34cb4e-8"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_8c7aabea1826b4da5011effecea7431edf34cb4e-9"><code data-line-number=" 9"></code></a></td><td class="code"><code><a id="code_8c7aabea1826b4da5011effecea7431edf34cb4e-9" name="code_8c7aabea1826b4da5011effecea7431edf34cb4e-9"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kn">location</span><span class="w"> </span><span class="s">/static</span><span class="w"> </span><span class="p">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_8c7aabea1826b4da5011effecea7431edf34cb4e-10"><code data-line-number="10"></code></a></td><td class="code"><code><a id="code_8c7aabea1826b4da5011effecea7431edf34cb4e-10" name="code_8c7aabea1826b4da5011effecea7431edf34cb4e-10"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kn">alias</span><span class="w"> </span><span class="s">/srv/users.getnikola.com/static</span><span class="p">;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_8c7aabea1826b4da5011effecea7431edf34cb4e-11"><code data-line-number="11"></code></a></td><td class="code"><code><a id="code_8c7aabea1826b4da5011effecea7431edf34cb4e-11" name="code_8c7aabea1826b4da5011effecea7431edf34cb4e-11"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="p">}</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/#code_8c7aabea1826b4da5011effecea7431edf34cb4e-12"><code data-line-number="12"></code></a></td><td class="code"><code><a id="code_8c7aabea1826b4da5011effecea7431edf34cb4e-12" name="code_8c7aabea1826b4da5011effecea7431edf34cb4e-12"></a><span class="p">}</span>
</code></td></tr></table></div>
<h2 id="conclusion">Conclusion</h2>
<p>In just two evenings, I got rid of the venv and system Python maintenance burden. When I upgrade to Ubuntu 26.04 in a few months, my Python apps will just work with no extra steps needed. Docker is a lot of fun.</p>
]]></content:encoded><category>Python</category><category>Django</category><category>Docker</category><category>gunicorn</category><category>Internet</category><category>Linux</category><category>nginx</category><category>Python</category></item><item><title>Distro Hopping, Server Edition</title><dc:creator>Chris Warrick</dc:creator><link>https://chriswarrick.com/blog/2025/11/09/distro-hopping-server-edition/</link><pubDate>Sun, 09 Nov 2025 18:00:00 GMT</pubDate><guid>https://chriswarrick.com/blog/2025/11/09/distro-hopping-server-edition/</guid><description>I’ve recently migrated my VPS from Fedora to Ubuntu. Here’s a list of things that might be useful to keep in mind before, during, and after a migration of a server that hosts publicly accessible Web sites and applications, as well as other personal services, and how to get rid of the most annoying parts of Ubuntu.
</description><content:encoded><![CDATA[<p>I’ve recently migrated my VPS from Fedora to Ubuntu. Here’s a list of things that might be useful to keep in mind before, during, and after a migration of a server that hosts publicly accessible Web sites and applications, as well as other personal services, and how to get rid of the most annoying parts of Ubuntu.</p>



<h2 id="why-switch">Why switch?</h2>
<p>Fedora is a relatively popular distro, so it’s well supported by software vendors. Its packagers adopt a no-nonsense approach, making very little changes that deviate from the upstream.</p>
<p>Ubuntu is not my favorite distro, far from it. While it is perhaps the most popular distro out there, its packages contain many more patches compared to Fedora, and Canonical (the company behind Ubuntu) are famous for betting on the wrong horse (Unity, upstart, Mir…). But one thing Ubuntu does well is stability. Fedora makes releases every 6 months, and those releases are supported for just 13 months, which means upgrading at least every year. Every upgrade may introduce incompatibilities, almost every upgrade requires recreating Python venvs. That gets boring fast, and it does not necessarily bring benefits. Granted, the Fedora system upgrade works quite well, and I upgraded through at least eight releases without a re-install, but I would still prefer to avoid it. That’s why I went with Ubuntu LTS, which is supported for five years, with a new release every two years, but which still comes with reasonably new software (and with many third-party repositories if something is missing or outdated).</p>
<h2 id="test-your-backups">Test your backups</h2>
<p>I have a backup “system” that’s a bunch of Bash scripts. After upgrading one of the services that is being backed up, the responsible script started crashing, and thus backups stopped working. Another thing that broke was e-mails from cron, so I didn’t know anything was wrong.</p>
<p>While I do have full disk backups enabled at <a href="https://hetzner.cloud/?ref=Qy1lehF8PwzP">Hetzner</a> <em>(disclaimer: referral link)</em>, my custom backups are more fine-grained (e.g. important configuration files, database dumps, package lists), so they are quite useful in migrating between OSes.</p>
<p>So, here’s a reminder not only to test your backups regularly, but also to make sure they are being created at all, and to make sure cron can send you logs somewhere you can see them.</p>
<p>Bonus cron tip: set <code>MAILFROM=</code> and <code>MAILTO=</code> in your crontab if your SMTP server does not like the values cron uses by default.</p>
<h2 id="think-about-ip-address-reassignment-or-pray-to-the-dns-gods">Think about IP address reassignment (or pray to the DNS gods)</h2>
<p>A new VPS or cloud server probably means a new IP address. But if you get a new IP address, that might complicate the migration of your publicly accessible applications. If you’re proxying all your Web properties through Cloudflare or something similar, that’s probably not an issue. But if you have a raw A record somewhere, things can get complicated. DNS servers and operating systems do a lot of caching. The conventional wisdom is to wait 24 or even 48 hours after changing DNS values. This might be true if your TTL is set to a long value, but if your TTL is short, the only worry are DNS servers that ignore TTL values and cache records for longer. If you plan a migration, it’s good to check your TTL well in advance, and not worry too much about broken DNS servers.</p>
<p>But you might not need a new IP. Carefully review your cloud provider’s IP management options before making any changes. Hetzner is more flexible than other hosts in this regard, as it is possible to <a href="https://docs.hetzner.com/cloud/servers/primary-ips/faq">move primary public IP addresses (not “floating” or “elastic” IPs) between servers</a>, as long as you’re okay with a few minutes’ downtime (you will need to shut down the source and destination servers).</p>
<p>If you’re not okay with any downtime, you would probably want to leverage the floating/elastic IP feature, or hope DNS propagates quickly enough.</p>
<h2 id="trim-the-fat">Trim the fat</h2>
<p>My VPS ran a lot of services I don’t need anymore, but never really got around to decommissioning. For example, I had a full Xfce install with VNC access (the VNC server was only running when needed). I haven’t actually used the desktop for ages, so I just dropped it.</p>
<p>I also had an OpenVPN setup. It was useful years ago, when mobile data allowances were much smaller and speeds much worse. These days, I don’t use public WiFi networks at all, unless I’m going abroad, and I just buy one month of <a href="https://mullvad.net/">Mullvad VPN</a> for €5 whenever that happens. So, add another service to the “do not migrate” list.</p>
<p>One thing that I could not just remove was the e-mail server. Many years ago, I ran a reasonably functional e-mail server on my VPS. I’ve since then migrated to <a href="https://www.zoho.com/mail/">Zoho Mail</a> (which costs €10.80/year), in part due to IP reputation issues after changing hosting providers, and also to avoid having to fight spam. When I did that, I kept Postfix around, but as a local server for things like cron or Django to send e-mail with, and I configured it to send all e-mails via Zoho. But I did not really want to move over all the configuration, hoping that Ubuntu’s Postfix packages can work with my hacked together config from Fedora. So I replaced the server with <a href="https://www.opensmtpd.org/">OpenSMTPD</a> (from the OpenBSD project), and all the Postfix configuration files with just one short configuration file:</p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2025/11/09/distro-hopping-server-edition/#code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-1"><code data-line-number="1"></code></a></td><td class="code"><code><a id="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-1" name="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-1"></a>table aliases file:/etc/aliases
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2025/11/09/distro-hopping-server-edition/#code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-2"><code data-line-number="2"></code></a></td><td class="code"><code><a id="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-2" name="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-2"></a>table secrets file:/etc/mail-secrets
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2025/11/09/distro-hopping-server-edition/#code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-3"><code data-line-number="3"></code></a></td><td class="code"><code><a id="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-3" name="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-3"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2025/11/09/distro-hopping-server-edition/#code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-4"><code data-line-number="4"></code></a></td><td class="code"><code><a id="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-4" name="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-4"></a>listen on localhost
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2025/11/09/distro-hopping-server-edition/#code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-5"><code data-line-number="5"></code></a></td><td class="code"><code><a id="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-5" name="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-5"></a>listen on 172.17.0.1 # Docker
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2025/11/09/distro-hopping-server-edition/#code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-6"><code data-line-number="6"></code></a></td><td class="code"><code><a id="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-6" name="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-6"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2025/11/09/distro-hopping-server-edition/#code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-7"><code data-line-number="7"></code></a></td><td class="code"><code><a id="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-7" name="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-7"></a>action &quot;relay&quot; relay host smtp+tls://smtp@smtp.example.net:587 auth &lt;secrets&gt; mail-from &quot;@example.com&quot;
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2025/11/09/distro-hopping-server-edition/#code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-8"><code data-line-number="8"></code></a></td><td class="code"><code><a id="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-8" name="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-8"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2025/11/09/distro-hopping-server-edition/#code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-9"><code data-line-number="9"></code></a></td><td class="code"><code><a id="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-9" name="code_2c8287178270f9e6d85ad33ec63ce4e8cfae3d0b-9"></a>match from any for any action &quot;relay&quot;
</code></td></tr></table></div>
<h2 id="dockerize-everything">Dockerize everything…</h2>
<p>My server runs a few different apps, some of which are exposed on the public Internet, while some do useful work in the background. The services I have set up most recently are containerized with the help of Docker. The only Docker-based service that was stateful (and did not just use folders mounted as volumes) was a MariaDB database. Migrating that is straightforward with a simple dump-and-restore.</p>
<p>Of course, not everything on my server is in Docker. The public-facing nginx install isn’t, and neither is PostgreSQL (but that was also a quick dump-and-restore migration with some extra steps).</p>
<h2 id="especially-python">…especially Python</h2>
<p>But then, there are the Python apps. Python the language is cool (if a little slow), but the packaging story is a <a href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/">total</a> dumpster <a href="https://chriswarrick.com/blog/2024/01/15/python-packaging-one-year-later/">fire</a>.</p>
<p>By the way, here’s a quick recap of 2024/2025 in Python packaging: the most hyped Python package manager (<code>uv</code>) is written in Rust, which screams “Python is a toy language in which you can’t even write a tool as simple as a package manager”. (I know, dependency resolution is computationally expensive, so doing <em>that</em> in Rust makes sense, but everything else could easily be done in pure Python. And no, the package manager should not manage Python installs.) Of course, almost all the other contenders listed in my 2023 post are still being developed. On the standards front, the community finally produced a lockfile standard after years of discussions.</p>
<p>Anyway, I have three Python apps. One of them is <a href="https://isso-comments.de/">Isso</a>, which is providing the comments box below this post. I used to run a modified version of Isso a long time ago, but I don’t need to anymore. I looked at the docs, and they offer <a href="https://isso-comments.de/docs/reference/installation/#using-docker">a pre-built Docker image</a>, which means I could just quickly deploy it on my server with Docker and skip the pain of managing Python environments.</p>
<p>The other two apps are Django projects built by yours truly. They are not containerized, they exist in venvs created using the system Python. Moving venvs between machines is generally impossible, so I had to re-create them. Of course, I hit a deprecation, because the Python maintainers (especially in the packaging world) does not understand their responsibility as maintainers of the most popular programming language. This time, it was caused by <a href="https://github.com/pypa/pip/issues/11457">an old editable install with setuptools (using setup.py develop, not PEP 660)</a>, and installs with more recent pip/setuptools versions would not have this error… although <a href="https://discuss.python.org/t/do-we-want-to-keep-the-build-system-default-for-pyproject-toml/104759">some people want to remove the fallback to setuptools if there is no pyproject.toml</a>, so you need to stay up to date with the whims of the Python packaging industry if you want to use Python software.</p>
<p><strong>Update 2026-02-06:</strong> <a href="https://chriswarrick.com/blog/2026/02/06/deploying-python-web-applications-with-docker/">I migrated the two Django apps to Docker and wrote a post about it.</a></p>
<h2 id="dont-bother-with-ufw">Don’t bother with ufw</h2>
<p>Ubuntu ships with <code>ufw</code>, the “uncomplicated firewall”, in the default install. I was previously using <code>firewalld</code>, a Red Hat-adjacent project, but I decided to give ufw a try. Since if it’s part of the default install, it might be supported better by the system.</p>
<p>It turns out that Docker and ufw <a href="https://docs.docker.com/engine/network/packet-filtering-firewalls/#docker-and-ufw">don’t play together</a>. <a href="https://github.com/chaifeng/ufw-docker?tab=readme-ov-file#solving-ufw-and-docker-issues">Someone has built a set of rules that are supposed to fix it</a>, but that did not work for me.</p>
<p>Docker <a href="https://docs.docker.com/engine/network/packet-filtering-firewalls/#integration-with-firewalld">does integrate with firewalld</a>, and Ubuntu has packages for it, so I just installed it, enabled the services that need to be publicly available and things were working again.</p>
<p><em>Update (2025-11-23):</em> The iptables integration was not very stable on my Ubuntu system, so I disabled the iptables integration and switched to <a href="https://dev.to/soerenmetje/how-to-secure-a-docker-host-using-firewalld-2joo">a simpler config in firewalld only</a>.</p>
<h2 id="kill-the-ads-and-other-nonsense-too">Kill the ads (and other nonsense too)</h2>
<p>Red Hat makes money by selling a stable OS with at least 10 years of support to enterprises, and their free offering is Fedora, with just 13 months of support; RHEL releases are branched off from Fedora. SUSE also sells SUSE Linux Enterprise and has openSUSE as the free offering (but the relationship between the paid and free version is more complicated).</p>
<p>Ubuntu chose a different monetization strategy: the enterprise offering is the same OS as the free offering, but it gets extra packages and extra updates. The free OS advertises the paid services. It is fairly simple to get rid of them all:</p>
<div class="highlight"><pre><span></span>sudo apt autoremove ubuntu-pro-client
sudo chmod -x /etc/update-motd.d/*
</pre></div>

<p>Also, Ubuntu installs snap by default. Snap is a terrible idea. Luckily, there are no snaps installed by default on a Server install, so we can just remove <code>snapd</code>. We’ll also remove <code>lxd-installer</code> to save ~25 kB of disk space, since the installer requires snap, and lxd is another unsuccessful Canonical project.</p>
<div class="highlight"><pre><span></span>sudo apt autoremove snapd lxd-installer
</pre></div>

<h2 id="the-cost-of-downgrading">The cost of downgrading</h2>
<p>Going from Fedora 42 (April 2025) to Ubuntu 24.04 (April 2024) means some software will be downgraded in the process. In general, this does not matter, as most software does not mind downgrades as much. One notable exception is WeeChat, the IRC client, whose config files are versioned, and Ubuntu’s version is not compatible with the one in Fedora. But here’s where Ubuntu’s popularity shines: <a href="https://weechat.org/download/debian/">WeeChat has its own repositories for Debian and Ubuntu</a>, so I could just get the latest version without building it myself or trying to steal packages from a newer version.</p>
<p>Other than WeeChat, I haven’t experienced any other issues with software due to a downgrade. Some of it is luck (or not using new/advanced features), some of it is software caring about backwards compatibility.</p>
<h2 id="conclusion">Conclusion</h2>
<p>Was it worth it? Time will tell. Upgrading Fedora itself was not that hard, and I expect Ubuntu upgrades to be OK too — the annoying part was cleaning up and getting things to work after the upgrade, and the switch means I will have to do it only every 2-4 years instead of every 6-12 months.</p>
<p>The switchover took a few hours, especially since I didn’t have much up-to-date documentation of what is actually installed and running, and there are always the minor details where distros differ that may require adjusting to. I think a migration like this is worth trying if rolling-release or frequently-released distros are too unstable for your needs.</p>
]]></content:encoded><category>Linux</category><category>Django</category><category>Docker</category><category>Fedora</category><category>Linux</category><category>Python</category><category>Ubuntu</category></item><item><title>Python Packaging, One Year Later: A Look Back at 2023 in Python Packaging</title><dc:creator>Chris Warrick</dc:creator><link>https://chriswarrick.com/blog/2024/01/15/python-packaging-one-year-later/</link><pubDate>Mon, 15 Jan 2024 18:50:00 GMT</pubDate><guid>https://chriswarrick.com/blog/2024/01/15/python-packaging-one-year-later/</guid><description>
A year ago, I wrote about the sad state of Python packaging. The large number of tools in the space, the emphasis on writing vague standards instead of rallying around the One True Tool, and the complicated venv-based ecosystem instead of a solution similar to node_modules. What has changed in the past year? Has anything improved, is everything the same, or are things worse than they were before?
</description><content:encoded><![CDATA[
<p>A year ago, I wrote about the sad state of Python packaging. The large number of tools in the space, the emphasis on writing vague standards instead of rallying around the One True Tool, and the complicated <code class="docutils literal">venv</code>-based ecosystem instead of a solution similar to <code class="docutils literal">node_modules</code>. What has changed in the past year? Has anything improved, is everything the same, or are things worse than they were before?</p>



<section id="the-tools">
<h1>The tools</h1>
<p><a class="reference external" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/">The original post</a> listed a bunch of packaging tools, calling <em>fourteen tools at least twelve too many</em>. My idea with that was that most people would be happy with one tool that does everything, but the scientific-Python folks might have special requirements that would work best as a second tool.</p>
<p>Out of the tools named in last year’s post, all of them still seem to be maintained. Except for Flit (zero new commits in the past 30 days) and virtualenv (only automated and semi-automated version bumps), the tools have recent commits, pull requests, and issues.</p>
<p>All of those tools are still in use. <a class="reference external" href="https://framapiaf.org/&#64;fcodvpt/111540079686191842">Françoise Conil analysed all PyPI packages</a> and checked their PEP 517 build backends: setuptools is the most popular (at 50k packages), Poetry is second at 41k, Hatchling is third at 8.1k. Other tools to cross 500 users include Flit (4.4k), PDM (1.3k), Maturin (1.3k, build backend for Rust-based packages).</p>
<p>There are some new tools, of course. Those that crossed my radar are <a class="reference external" href="https://github.com/njsmith/posy">Posy</a> and <a class="reference external" href="https://github.com/mitsuhiko/rye">Rye</a>. Posy is a project of Nathaniel J. Smith (of trio fame), Rye is a project of Armin Ronacher (of Flask fame). The vision for both of them is to manage Python interpreters and projects, but not have a custom build backend (instead using something like hatchling). Posy is built on top of PyBI (a format for distributing binaries of Python interpreters, proposed by Smith in draft <a class="reference external" href="https://peps.python.org/pep-0711/">PEP 711</a>), Rye uses Gregory Szorc’s pre-built Pythons. Rye seems to be fairly complete and usable, Posy is right now a PoC of the PyBI format, and only offers a REPL with pre-installed packages.</p>
<p>Both Posy and Rye are written in Rust. On the one hand, it makes sense that the part that manages Python interpreters is not written in Python, because that would require a separate Python, not managed by Posy/Rye, to run those tools. But Rye also has its own pyproject.toml parser in Rust, and many of its commands are implemented mostly or largely using Rust (sometimes also calling one-off Python scripts; although the main tasks of creating venvs, installing packages, and working with lockfiles are handed off to <code class="docutils literal">venv</code>, <code class="docutils literal">pip</code>, and <code class="docutils literal"><span class="pre">pip-tools</span></code> respectively).</p>
<p>Speaking of Rust and Python, there’s been another project in that vein that has grown a lot <a class="reference external" href="https://astral.sh/blog/announcing-astral-the-company-behind-ruff">(and gathered a lot of funding)</a> in the past year. That project is <a class="reference external" href="https://github.com/astral-sh/ruff">Ruff</a>, which is a linter and code formatter. Ruff formats Python code, and is written in Rust. This means it’s 10–100× faster than existing tools written in Python (according to Ruff’s own benchmarks). Fast is good, I guess, but what does this say about Python? Is the fact that package tools (which aren’t rocket science, maybe except for fast dependency solvers, and which often need access to Python internals to do their job) and code formatters (which require a deep understanding of Python syntax, and parsing Python sources to ASTs, something easy by the <code class="docutils literal">ast</code> Python module) are written in another language? Does this trend make Python a toy language (as it is also often considered <em>a glue language</em> for NumPy and friends)? Also, why should contributing to a tool important to many Python developers require learning Rust?</p>
</section>
<section id="the-standards">
<h1>The standards</h1>
<p>Last time we looked at packaging standards, we focused on <a class="reference external" href="https://peps.python.org/pep-0582/">PEP 582</a>. It proposed the introduction of <code class="docutils literal">__pypackages__</code>, which would be a place for third-party packages to be installed to locally, on a per-project basis, without involving virtual environments, similarly to what <code class="docutils literal">node_modules</code> is for node. The PEP was ultimately <a class="reference external" href="https://discuss.python.org/t/pep-582-python-local-packages-directory/963/430">rejected</a> in March 2023. The PEP wasn’t perfect, and some of its choices were questionable or insufficient (such as not recursively searching for <code class="docutils literal">__pypackages__</code> in parent directories, or focusing on simple use-cases only). No new standards for something in that vein (with a better design) were proposed to this day.</p>
<p>Another contentious topic is lock files. Lock files for packaging systems are useful for reproducible dependency installations. The lock file records all installed packages (i.e. includes transitive dependencies) and their versions. Lock files often include checksums (like sha512) of the installed packages, and they often support telling apart packages installed via different groups of dependencies (runtime, buildtime, optional, development, etc.).</p>
<p>The classic way of achieving this goal are <code class="docutils literal">requirements.txt</code> files. They are specific to pip, and they only contain a list of packages, versions, and possibly checksums. Those files can be generated by <code class="docutils literal">pip freeze</code>, or the third-party <code class="docutils literal"><span class="pre">pip-compile</span></code> from <code class="docutils literal"><span class="pre">pip-tools</span></code>. <code class="docutils literal">pip freeze</code> is very basic, <code class="docutils literal"><span class="pre">pip-compile</span></code> can’t handle different groups of dependencies other than making multiple <code class="docutils literal">requirements.in</code> files, compiling them, and hoping there are no conflicts.</p>
<p>Pipenv, Poetry, and PDM have their own lockfile implementations, incompatible with one another. Rye piggybacks on top of <code class="docutils literal"><span class="pre">pip-tools</span></code>. Hatch doesn’t have anything in core; they’re waiting for a standard implementation (there are some plugins though). <a class="reference external" href="https://peps.python.org/pep-0665/">PEP 665</a> was <a class="reference external" href="https://discuss.python.org/t/pep-665-take-2-a-file-format-to-list-python-dependencies-for-reproducibility-of-an-application/11736/140">rejected</a> in January 2022. Its author, Brett Cannon, <a class="reference external" href="https://snarky.ca/state-of-standardized-lock-files-for-python-august-2023/">is working on a PoC</a> of something that <em>might</em> become a standard (named <a class="reference external" href="https://github.com/brettcannon/mousebender">mousebender</a>).</p>
<p>This is the danger of the working model adopted by the Python packaging world. Even for something as simple as lock files, there are at least four incompatible standards. An attempt at a specification was rejected due to “lukewarm reception”, even though there exist at least four implementations which are achieving roughly the same goals, and other ecosystems also went through this before.</p>
<p>Another thing important to Python are extension modules. Extension modules are written in C, and they are usually used to interact with libraries written in other languages (and also sometimes for performance). Poetry, PDM, and Hatchling don’t really support building extension modules. Setuptools does; <a class="reference external" href="https://numpy.org/doc/stable/reference/distutils_status_migration.html">SciPy and NumPy migrated from their custom numpy.distutils to Meson</a>. The team behind the PyO3 Rust bindings for Python develops <a class="reference external" href="https://github.com/PyO3/maturin">Maturin</a>, which allows for building Rust-based extension modules — but it’s not useful if you’re working with C.</p>
<p>There weren’t many packaging-related standards that were accepted in 2023. A standard worth mentioning is <a class="reference external" href="https://peps.python.org/pep-0668/">PEP 668</a>, which allows distributors to prevent <cite>pip</cite> from working (to avoid breaking distro-owned site packages) by adding an <code class="docutils literal"><span class="pre">EXTERNALLY-MANAGED</span></code> file. It was accepted in June 2022, but pip only implemented support for it in January 2023, and many distros already have enabled this feature in 2023. Preventing broken systems is a good thing.</p>
<p>But some standards did make it through. Minor and small ones aside, the most prominent 2023 standard would be <a class="reference external" href="https://peps.python.org/pep-0723/">PEP 723</a>: inline script metadata. It allows to add a comment block at the top of the file, that specifies the dependencies and the minimum Python version in a way that can be consumed by tools. Is it super useful? I don’t think so; setting up a project with pyproject.toml would easily allow things to grow. If you’re sending something via a GitHub gist, just make a repo. If you’re sending something by e-mail, just tar the folder. That approach promotes messy programming without source control.</p>
<section id="learning-curves-and-the-deception-of-simple">
<h2>Learning curves and the deception of “simple”</h2>
<p>Microsoft Word is simple, and a great beginner’s writing tool. You can make text bold with a single click. You can also make it blue in two clicks. But it’s easy to make an inconsistent mess. To make section headers, many users may just make the text bold and a bit bigger, without any consistency or semantics <a class="brackets" href="https://chriswarrick.com/blog/2024/01/15/python-packaging-one-year-later/#footnote-1" id="footnote-reference-1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a>. Making a consistent document with semantic formatting is hard in Word. Adding <a class="reference external" href="https://www.techrepublic.com/article/how-to-create-multilevel-numbered-headings-in-word-2016/">section numbering</a> requires you to select a heading and turn it into a list. There’s also supposedly some magic involved, that magic doesn’t work for me, and I have to tell Word to update the heading style. Even if you try doing things nicely, Word will randomly break, mess up the styles, mix up styles and inline ad-hoc formatting, and your document may look differently on different computers.</p>
<p>LaTeX is very confusing to a beginner, and has a massive learning curve. And you can certainly write <code class="docutils literal">\textbf{hello}</code> everywhere. But with some learning, you’ll be producing beautiful documents. You’ll define a <code class="docutils literal">\code{}</code> command that makes code monospace and adds a border today, but it might change the background and typeset in Comic Sans tomorrow if you so desire. You’ll use packages that can render code from external files with syntax highlighting. Heading numbering is on by default, but it can easily be disabled for a section. LaTeX can also automatically put new sections on new pages, for example. LaTeX was built for scientific publishing, so it has stellar support for maths and bibliographies, among other things.</p>
<p>Let’s now talk about programming. Python is simple, and a great beginner’s programming language. You can write <em>hello world</em> in a single line of code. The syntax is simpler, there are no confusing leftovers from C (like the index-based <code class="docutils literal">for</code> loop) or machine-level code (like <code class="docutils literal">break</code> in <code class="docutils literal">switch</code>), no pointers in sight. You also don’t need to write classes at all; you don’t need to write a class only to put a <code class="docutils literal">public static void main(String[] args)</code> method there <a class="brackets" href="https://chriswarrick.com/blog/2024/01/15/python-packaging-one-year-later/#footnote-2" id="footnote-reference-2" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a>. You don’t need an IDE, you can just write code using any editor (even notepad.exe will do for the first day or so), you can save it as a .py  file and run it using <code class="docutils literal">python whatever.py</code>.</p>
<p>Your code got more complicated? No worry, you can split it into multiple <code class="docutils literal">.py</code> files, use <code class="docutils literal">import name_of_other_file_without_py</code> and it will just work. Do you need more structure, grouping into folders perhaps? Well, forget about <code class="docutils literal">python whatever.py</code>, you must use <code class="docutils literal">python <span class="pre">-m</span> whatever</code>, and you must <code class="docutils literal">cd</code> to where your code is, or mess with <code class="docutils literal">PYTHONPATH</code>, or install your thing with <code class="docutils literal">pip</code>. This simple yet common action (grouping things into folders) has massively increased complexity.</p>
<p>The standard library is not enough <a class="brackets" href="https://chriswarrick.com/blog/2024/01/15/python-packaging-one-year-later/#footnote-3" id="footnote-reference-3" role="doc-noteref"><span class="fn-bracket">[</span>3<span class="fn-bracket">]</span></a> and you need a third-party dependency? You find some tutorial that tells you to <code class="docutils literal">pip install</code>, but <code class="docutils literal">pip</code> will now tell you to use <code class="docutils literal">apt</code>. And <code class="docutils literal">apt</code> may work, but it may give you an ancient version that does not match the tutorial you’re reading. Or it may not have the package. Or the Internet will tell you not to use Python packages from <code class="docutils literal">apt</code>. So now you need to <a class="reference external" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/">learn about venvs</a> (which add more complexity, more things to remember; most tutorials teach activation, venvs are easy to mess up via basic operations like renaming a folder, and you may end up with a venv in git or your code in a venv). Or you need to pick one of the many one-stop-shop tools to manage things.</p>
<p>In other ecosystems, an IDE is often a necessity, even for beginners. The IDE will force you into a project system (maybe not the best or most common one by default, but it will still be a coherent project system). Java will force you to make more than one file with the “1 public class = 1 file” rule, and it will be easy to do so, you won’t even need an <code class="docutils literal">import</code>.</p>
<p>Do you want folders? In Java or C#, you just create a folder in the IDE, and create a class there. The new file may have a different <code class="docutils literal">package</code>/<code class="docutils literal">namespace</code>, but the IDE will help you to add the correct <code class="docutils literal">import</code>/<code class="docutils literal">using</code> to the codebase, and there is no risk of you using too many directories (including something like <code class="docutils literal">src</code>) or using too few (not making a top-level package for all your code) that will require correcting all imports. The disruption from adding a folder in Java or C# is minimal.</p>
<p>The project system will also handle third-party packages without you needing to think about where they’re downloaded or what a virtual environment is and how to activate it from different contexts. A few clicks and you’re done. And if you don’t like IDEs? Living in the CLI is certainly possible in many ecosystems, they have reasonable CLI tools for common management tasks, as well as building and running your project.</p>
<p>PEP 723 solves a very niche problem: dependency management for single-file programs. Improving life for one-off things and messy code was apparently more important to the packaging community than any other improvements for big projects.</p>
<p>By the way, you could adapt this lesson to static and dynamic typing. Dynamic typing is easier to get started with and requires less typing, but compile-type checking can prevent many bugs — bugs that require higher test coverage to catch with dynamic typing. That’s why the JS world has TypeScript, that’s why mypy/pyright/typing has gained a lot of mindshare in the Python world.</p>
</section>
</section>
<section id="the-future">
<h1>The future…</h1>
<p>Looking at the <a class="reference external" href="https://discuss.python.org/c/packaging/14">Python Packaging Discourse</a>, there were some discussions about ways to improve things.</p>
<p>For example, this <a class="reference external" href="https://discuss.python.org/t/user-experience-with-porting-off-setup-py/37502">discussion about porting off setup.py</a> was started by Gregory Szorc, who had <a class="reference external" href="https://gregoryszorc.com/blog/2023/10/30/my-user-experience-porting-off-setup.py/">a long list of complaints</a>, pointing out the issues with the communication from the packaging world, and documentation mess (his post is worth a read, or at least a skim, because it’s long and full of packaging failures). There’s one page which recommends setuptools, another which has four options with Hatchling as a default, and another still promoting Pipenv. We’ve seen this a year ago, nothing changed in that regard. Some people tried finding solutions, some people shared their opinions… and then the Discourse moderator decided to protect his PyPA friends from having to read user feedback and locked the thread.</p>
<p>Many other threads about visions were had, like the one about <a class="reference external" href="https://discuss.python.org/t/the-10-year-view-on-python-packaging-whats-yours/31834">10-year views</a> or about <a class="reference external" href="https://discuss.python.org/t/wanting-a-singular-packaging-tool-vision/21141">singular packaging tools</a>. The strategy discussions, based on the user survey, had <a class="reference external" href="https://discuss.python.org/t/python-packaging-strategy-discussion-part-2/23442">a second part</a> (the <a class="reference external" href="https://discuss.python.org/t/python-packaging-strategy-discussion-part-1/22420">first one</a> concluded in January 2023), but it saw less posts than the first one, and discussions did not continue (and there were <a class="reference external" href="https://discuss.python.org/t/structure-of-the-packaging-strategy-discussions/23478">discussions about how to hold the discussions</a>). There are plans to <a class="reference external" href="https://discuss.python.org/t/draft-update-to-python-packaging-governance/31608">create a packaging council</a> — design-by-committee at its finest.</p>
<p>But all those discussions, even when not locked by an overzealous moderator, haven’t had any meaningful effect. The packaging ecosystem is still severely fragmented and confusing. <a class="reference external" href="https://packaging.python.org/en/latest/tutorials/">The PyPA docs and tutorials</a> still contradict each other. The PyPA-affiliated tools still have less features than the unaffiliated competition (even the upstart Rye has some form of lockfiles, unlike Hatch or Flit), and going by the PEP 517 build backend usage statistics, they are more popular than the modern PyPA tools. The authors of similar yet competing tools have not joined forces to produce the One True Packaging Tool.</p>
<section id="is-looking-pretty-bleak">
<h2>…is looking pretty bleak</h2>
<p>On the other hand, if you look at the 2023 contribution graphs for most packaging tools, you might be worried about the state of the packaging ecosystem.</p>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/pypa/pip/graphs/contributors?from=2023-01-01&amp;to=2023-12-31&amp;type=c">Pip</a> has had a healthy mix of contributors and a lot of commits going into it.</p></li>
<li><p><a class="reference external" href="https://github.com/pypa/pipenv/graphs/contributors?from=2023-01-01&amp;to=2023-12-31&amp;type=c">Pipenv</a> and <a class="reference external" href="https://github.com/pypa/setuptools/graphs/contributors?from=2023-01-01&amp;to=2023-12-31&amp;type=c">setuptools</a> have two lead committers, but still a healthy amount of commits.</p></li>
<li><p><a class="reference external" href="https://github.com/pypa/hatch/graphs/contributors?from=2023-01-01&amp;to=2023-12-31&amp;type=c">Hatch</a>, however, is a <strong>one-man-show</strong>: Ofek Lev (the project founder) made 184 commits, the second place belongs to Dependabot with 6 commits, and the third-place contributor (who is a human) has five commits.  The bus factor of Hatch and Hatchling is 1.</p></li>
</ul>
<p>The non-PyPA tools aren’t doing much better:</p>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/python-poetry/poetry/graphs/contributors?from=2023-01-01&amp;to=2023-12-31&amp;type=c">Poetry</a> has two top contributors, but at least there are four human contributors with a double-digit number of commits.</p></li>
<li><p><a class="reference external" href="https://github.com/pdm-project/pdm/graphs/contributors?from=2023-01-01&amp;to=2023-12-31&amp;type=c">PDM</a> is a one-man-show, like Hatch.</p></li>
<li><p><a class="reference external" href="https://github.com/mitsuhiko/rye/graphs/contributors?from=2023-04-23&amp;to=2023-12-31&amp;type=c">Rye</a> has one main contributor, and three with a double-digit number of commits; note it’s pretty new (started in late April 2023) and it’s not as popular as the others.</p></li>
</ul>
</section>
</section>
<section id="conclusion">
<h1>Conclusion</h1>
<p>I understand the PyPA is a loose association of volunteers. It is sometimes said the name <em>Python Packaging Authority</em> was <a class="reference external" href="https://discuss.python.org/t/remove-the-authority-from-packaging/1993">originally a joke</a>. However, they are also the group that maintains all the packaging standards, so they <em>are</em> the authority when it comes to packaging. For example, <a class="reference external" href="https://peps.python.org/pep-0668/">PEP 668</a> starts with a warning block saying it’s a historical document, and <a class="reference external" href="https://packaging.python.org/en/latest/specifications/externally-managed-environments/">the up-to-date version of the specification is on PyPA’s site</a> (as well as a bunch of other <a class="reference external" href="https://packaging.python.org/en/latest/specifications/">packaging specs</a>).</p>
<p><strong>The PyPA should shut down or merge some duplicate projects, and work with the community (including maintainers of non-PyPA projects) to build One True Packaging Tool.</strong> To make things easier. To avoid writing code that does largely the same thing 5 times. To make sure thousands of projects don’t depend on tools with a bus factor of 1 or 2. To turn packaging from a problem and an insurmountable obstacle to something that <em>just works™</em>, something that an average developer doesn’t need to think about.</p>
<p>It’s not rocket science. Tons of languages, big and small, have a coherent packaging ecosystem (just read <a class="reference external" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/">last year’s post</a> for some examples of how simple it can be). Instead of focusing on specifications and governance, focus on producing one comprehensive, usable, user-friendly tool.</p>
<p>Discuss below or <a class="reference external" href="https://news.ycombinator.com/item?id=39004600">on Hacker News</a>.</p>
</section>
<section id="footnotes">
<h1>Footnotes</h1>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="footnote-1" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2024/01/15/python-packaging-one-year-later/#footnote-reference-1">1</a><span class="fn-bracket">]</span></span>
<p>Modern Word at least makes this easier, because the heading styles get top billing on the ribbon; they were hidden behind a completely non-obvious combo box that said <em>Normal</em> in Word 2003 and older.</p>
</aside>
<aside class="footnote brackets" id="footnote-2" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2024/01/15/python-packaging-one-year-later/#footnote-reference-2">2</a><span class="fn-bracket">]</span></span>
<p>C# 10 removed the requirement to make a class with a <code class="docutils literal">Main</code> method, it can pick up one file with top-level statements and make it the entrypoint.</p>
</aside>
<aside class="footnote brackets" id="footnote-3" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2024/01/15/python-packaging-one-year-later/#footnote-reference-3">3</a><span class="fn-bracket">]</span></span>
<p>The Python standard library gets a lot of praise. It <em>is</em> large compared to C, but nothing special compared to Java or C#. It is also full of low-quality libraries, like <code class="docutils literal">http.server</code> or <code class="docutils literal">urllib.request</code>, yet some people insist on only using the standard library. The standard library is also less stable and dependable (with constant deprecations and removals, and with new features requiring upgrading all of Python). All the “serious” use-cases, like web development or ML/AI/data science are impossible with just the standard library.</p>
</aside>
</aside>
</section>
]]></content:encoded><category>Python</category><category>packaging</category><category>PDM</category><category>pip</category><category>PyPA</category><category>Python</category><category>virtual environments</category></item><item><title>How to improve Python packaging, or why fourteen tools are at least twelve too many</title><dc:creator>Chris Warrick</dc:creator><link>https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/</link><pubDate>Sun, 15 Jan 2023 13:45:00 GMT</pubDate><guid>https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/</guid><description>
There is an area of Python that many developers have problems with. This is an area that has seen many different solutions pop up over the years, with many different opinions, wars, and attempts to solve it. Many have complained about the packaging ecosystem and tools making their lives harder. Many beginners are confused about virtual environments. But does it have to be this way? Are the current solutions to packaging problems any good? And is the organization behind most of the packaging tools and standards part of the problem itself?
Join me on a journey through packaging in Python and elsewhere. We’ll start by describing the classic packaging stack (involving setuptools and friends), the scientific stack (with conda), and some of the modern/alternate tools, such as Pipenv, Poetry, Hatch, or PDM. We’ll also look at some examples of packaging and dependency-related workflows seen elsewhere (Node.js and .NET). We’ll also take a glimpse at a possible future (with a venv-less workflow with PDM), and see if the PyPA agrees with the vision and insights of eight thousand users.
</description><content:encoded><![CDATA[
<p>There is an area of Python that many developers have problems with. This is an area that has seen many different solutions pop up over the years, with many different opinions, wars, and attempts to solve it. Many have complained about the packaging ecosystem and tools making their lives harder. Many beginners are confused about virtual environments. But does it have to be this way? Are the current solutions to packaging problems any good? And is the organization behind most of the packaging tools and standards part of the problem itself?</p>
<p>Join me on a journey through packaging in Python and elsewhere. We’ll start by describing the classic packaging stack (involving setuptools and friends), the scientific stack (with conda), and some of the modern/alternate tools, such as Pipenv, Poetry, Hatch, or PDM. We’ll also look at some examples of packaging and dependency-related workflows seen elsewhere (Node.js and .NET). We’ll also take a glimpse at a possible future (with a venv-less workflow with PDM), and see if the PyPA agrees with the vision and insights of eight thousand users.</p>



<section id="the-plethora-of-tools">
<h1>The plethora of tools</h1>
<p>There are many packaging-related tools in Python. All of them with different authors, lineages, and often different opinions, although most of them are now unified under the Python Packaging Authority (PyPA) umbrella. Let’s take a look at them.</p>
<section id="the-classic-stack">
<h2>The classic stack</h2>
<p>The classic Python packaging stack consists of many semi-related tools. Setuptools, probably the oldest tool of the group, and itself based on <code class="docutils literal">distutils</code>, which is part of the standard library (although it will be removed in Python 3.12), is responsible for installing a single package. It previously used <code class="docutils literal">setup.py</code> files to do its job, which required arbitrary code execution. It then added support for non-executable metadata specification formats: <code class="docutils literal">setup.cfg</code>, and also <code class="docutils literal">pyproject.toml</code> (partially still in beta). However, you aren’t supposed to use <code class="docutils literal">setup.py</code> files directly these days, you’re supposed to be using pip. Pip installs packages, usually from the PyPI, but it can also support other sources (such as git repositories or the local filesystem). But where does pip install things? The default used to be to install globally and system-wide, which meant you could introduce conflicts between packages installed by pip and apt (or whatever the system package manager is). Even with a user-wide install (which pip is likely to attempt these days), you can still end up with conflicts, and you can also have conflicts in which package A requests X version 1.0.0, but package B expects X version 2.0.0—but A and B are not at all related and could live separately with their preferred version of X. Enter <code class="docutils literal">venv</code>, a standard library descendant of <code class="docutils literal">virtualenv</code>, which can create a lightweight virtual environment for packages to live in. This virtual environment gives you the separation from system packages and from different environments, but it is still tied to the system Python in some ways (and if the system Python disappears, the virtual environment stops working).</p>
<p>A few extra tools would be used in a typical packaging workflow. The <code class="docutils literal">wheel</code> package enhances Setuptools with the ability to generate wheels, which are ready-to-install (without running <code class="docutils literal">setup.py</code>). Wheels can either be pure-Python and be installed anywhere, or they can contain pre-compiled extension modules (things written in C) for a given OS and Python (and there’s even a standard that allows building and distributing one wheel for all typical Linux distros). The <code class="docutils literal">wheel</code> package should be an implementation detail, something existing inside Setuptools and/or pip, but users need to be aware of it if they want to make wheels on their system, because virtual environments produced by <code class="docutils literal">venv</code> do not have <code class="docutils literal">wheel</code> installed. Regular users who do not maintain their own packages may sometimes be told that pip is using something legacy because <code class="docutils literal">wheel</code> is not installed, which is not a good user experience. Package authors also need <code class="docutils literal">twine</code>, whose sole task is uploading source distributions or wheels, created with other tools, to PyPI (and there’s not much more to say about that tool).</p>
</section>
<section id="and-a-few-extensions">
<h2>…and a few extensions</h2>
<p>Over the years, there have been a few tools that are based on things from the classic stack. For example, <code class="docutils literal"><span class="pre">pip-tools</span></code> can simplify dependency management. While <code class="docutils literal">pip freeze</code> lets you produce a file with everything installed in your environment, there is no way to specify the dependencies you need, and get a lock file with specific versions and transitive dependencies (without installing and freezing everything), there is no easy way to skip development dependencies (e.g. IPython) when you <code class="docutils literal">pip freeze</code>, and there is no workflow to update all your dependencies with just pip. <code class="docutils literal"><span class="pre">pip-tools</span></code> adds two tools, <code class="docutils literal"><span class="pre">pip-compile</span></code> which takes in <code class="docutils literal">requirements.in</code> files with the packages you care about, and produces a <code class="docutils literal">requrirements.txt</code> with pinned versions of them and all transitive dependencies; and also <code class="docutils literal"><span class="pre">pip-sync</span></code>, which can install <code class="docutils literal">requirements.txt</code> and removes things not listed in it.</p>
<p>Another tool that might come in useful is <code class="docutils literal">virtualenvwrapper</code>, which can help you manage (create and activate) virtual environments in a central location. It has a few bells and whistles (such as custom hooks to do actions on every virtualenv creation), although for basic usage, you could replace it with a single-line shell function.</p>
<p>Yet another tool that works alongside the classic toolset is <code class="docutils literal">pipx</code>, which creates and manages virtual environments for apps written in Python. You tell it to <code class="docutils literal">pipx install Nikola</code>, and it will create a virtual environment somewhere, install Nikola into it, and put a script for launching it in <code class="docutils literal"><span class="pre">~/.local/bin</span></code>. While you could do it all yourself with venv and some symlinks, pipx can take care of this, and you don’t need to remember where the virtual environment is.</p>
</section>
<section id="the-scientific-stack-and-conda">
<h2>The scientific stack and conda</h2>
<p>The scientific Python community have had their own tools for many years. The conda tool can manage environments and packages. It doesn’t use PyPI and wheels, but rather packages from conda channels (which are prebuilt, and expect an Anaconda-distributed Python). Back in the day, when there were no wheels, this was the easiest way to get things installed on Windows; this is not as much of a problem now with binary wheels on PyPI—but the Anaconda stack is still popular in the scientific world. Conda packages can be built with <code class="docutils literal"><span class="pre">conda-build</span></code>, which is separate, but closely related to <code class="docutils literal">conda</code> itself. Conda packages are not compatible with <code class="docutils literal">pip</code> in any way, they do not follow the packaging standards used by other tools. Is this good? No, because it makes integrating the two worlds harder, but also yes, because many problems that apply to scientific packages (and their C/C++ extension modules, and their high-performance numeric libraries, and other things) do not apply to other uses of Python, so having a separate tool lets people focusing the other uses simplify their workflows.</p>
</section>
<section id="the-new-tools">
<h2>The new tools</h2>
<p>A few years ago, new packaging tools appeared. Now, there were lots of “new fancy tools” introduced in the past, with setuptools extending distutils, then distribute forking setuptools, then distribute being merged back…</p>
<p>The earliest “new tool” was Pipenv. Pipenv had really terrible and misleading marketing, and it merged pip and venv, in that Pipenv would create a venv and install packages in it (from <code class="docutils literal">Pipfile</code> or <code class="docutils literal">Pipfile.lock</code>). Pipenv can place the venv in the project folder, or hide it somewhere in the project folder (the latter is the default). However, Pipenv does not handle any packages related to packaging your code, so it’s useful only for developing non-installable applications (Django sites, for example). If you’re a library developer, you need setuptools anyway.</p>
<p>The second new tool was Poetry. It manages environments and dependencies in a similar way to Pipenv, but it can also build <code class="docutils literal">.whl</code> files with your code, and it can upload wheels and source distributions to PyPI. This means it has pretty much all the features the other tools have, except you need just one tool. However, Poetry is opinionated, and its opinions are sometimes incompatible with the rest of the packaging scene. Poetry uses the <code class="docutils literal">pyproject.toml</code> standard, but it does not follow the standard specifying how metadata should be represented in a <code class="docutils literal">pyproject.toml</code> file (PEP 621), instead using a custom <code class="docutils literal">[tool.poetry]</code> table. This is partly because Poetry came out before PEP 621, but the PEP was accepted over 2 years ago—the biggest compatibility problem is Poetry’s node-inspired <code class="docutils literal">~</code> and <code class="docutils literal">^</code> dependency version markers, which are not compatible with PEP 508 (the dependency specification standard). Poetry can package C extension modules, although it uses setuptools’ infrastructure for this (and requires a custom <code class="docutils literal">build.py</code> script).</p>
<p>Another similar tool is Hatch. This tool can also manage environments (it allows multiple environments per project, but it does not allow to put them in the project directory), and it can manage packages (but without lockfile support). Hatch can also be used to package a project (with PEP 621-compliant <code class="docutils literal">pyproject.toml</code> files) and upload it to PyPI. It does not support C extension modules.</p>
<p>A tool that tries to be a simpler re-imagining of Setuptools is Flit. It can build and install a package using a <code class="docutils literal">pyproject.toml</code> file. It also supports uploads to PyPI. It lacks support for C extension modules, and it expects you to manage environments on your own.</p>
<p>There’s one more interesting (albeit not popular or well-known) tool. This tool is PDM. It can manage venvs (but it defaults to the saner <code class="docutils literal">.venv</code> location), manage dependencies, and it uses a standards-compliant <code class="docutils literal">pyproject.toml</code>. There’s also a curious little feature called PEP 582 support, which we’ll talk about later.</p>
</section>
</section>
<section id="tooling-proliferation-and-the-python-package-authority">
<h1>Tooling proliferation and the Python Package Authority</h1>
<p>The previous sections mentioned 14 (fourteen!) distinct tools. As we’ll discover soon, that’s at least 12 too many. Let’s try to compare them.</p>
<p>First, let’s define nine things that we would expect packaging tools to do:</p>
<ol class="arabic simple">
<li><p>Manage environments</p></li>
<li><p>Install packages</p></li>
<li><p>Package/develop apps</p></li>
<li><p>Package libraries</p></li>
<li><p>Package C extension modules</p></li>
<li><p>Install in editable mode</p></li>
<li><p>Lock dependencies</p></li>
<li><p>Support pyproject.toml files</p></li>
<li><p>Upload to PyPI</p></li>
</ol>
<div style="font-size: 90%"><table class="table table-hover">
<thead>
<tr><th class="head"><p>Tool</p></th>
<th class="head"><p>Maintainer</p></th>
<th class="head"><p>Use-case</p></th>
<th class="head"><p># of supported features</p></th>
<th class="head"><p># of partially supported features</p></th>
<th class="head"><p># of unsupported features</p></th>
</tr>
</thead>
<tbody>
<tr><td><p>setuptools</p></td>
<td><p>PyPA</p></td>
<td><p>Making things installable</p></td>
<td><p>4</p></td>
<td><p>2 (pyproject.toml partially in beta, installing—only setuptools-based sdists)</p></td>
<td><p>3</p></td>
</tr>
<tr><td><p>pip</p></td>
<td><p>PyPA</p></td>
<td><p>Installing packages</p></td>
<td><p>2</p></td>
<td><p>1 (Locking dependencies only manually)</p></td>
<td><p>6</p></td>
</tr>
<tr><td><p>venv</p></td>
<td><p>PyPA</p></td>
<td><p>Creating virtual environments</p></td>
<td><p>1 (Creating environments)</p></td>
<td><p>0</p></td>
<td><p>8</p></td>
</tr>
<tr><td><p>wheel</p></td>
<td><p>PyPA</p></td>
<td><p>Building wheels in setuptools</p></td>
<td><p>0</p></td>
<td><p>1 (Building wheels in setuptools)</p></td>
<td><p>8</p></td>
</tr>
<tr><td><p>Twine</p></td>
<td><p>PyPA</p></td>
<td><p>Uploading to PyPI</p></td>
<td><p>1 (Uploading to PyPI)</p></td>
<td><p>0</p></td>
<td><p>8</p></td>
</tr>
<tr><td><p>pip-tools</p></td>
<td><p>Jazzband</p></td>
<td><p>Managing requirements files</p></td>
<td><p>2 (Locking dependencies, installing packages)</p></td>
<td><p>0</p></td>
<td><p>7</p></td>
</tr>
<tr><td><p>virtualenvwrapper</p></td>
<td><p>Doug Hellmann</p></td>
<td><p>Managing virtual environments</p></td>
<td><p>1 (Managing environments)</p></td>
<td><p>0</p></td>
<td><p>8</p></td>
</tr>
<tr><td><p>pipx</p></td>
<td><p>PyPA</p></td>
<td><p>Installing Python command-line tools</p></td>
<td><p>2 (Installing packages, editable installs)</p></td>
<td><p>1 (Managing environments)</p></td>
<td><p>6</p></td>
</tr>
<tr><td><p>conda</p></td>
<td><p>Anaconda, Inc.</p></td>
<td><p>Managing environments and dependencies</p></td>
<td><p>3 (Managing environments, installing things)</p></td>
<td><p>4 (Manual locking, packaging requires conda-build)</p></td>
<td><p>2 (pyproject.toml and PyPI)</p></td>
</tr>
<tr><td><p>Pipenv</p></td>
<td><p>PyPA</p></td>
<td><p>Managing dependencies for apps</p></td>
<td><p>3 (Managing environments, installing and locking)</p></td>
<td><p>1 (Developing apps)</p></td>
<td><p>5</p></td>
</tr>
<tr><td><p>Poetry</p></td>
<td><p>Sébastien Eustace et al.</p></td>
<td><p>Packaging and managing dependencies</p></td>
<td><p>7</p></td>
<td><p>2 (pyproject.toml, C extensions)</p></td>
<td><p>0</p></td>
</tr>
<tr><td><p>Flit</p></td>
<td><p>PyPA</p></td>
<td><p>Packaging pure-Python projects</p></td>
<td><p>5</p></td>
<td><p>1 (Installing only flit packages)</p></td>
<td><p>3</p></td>
</tr>
<tr><td><p>Hatch</p></td>
<td><p>PyPA</p></td>
<td><p>Packaging and managing dependencies</p></td>
<td><p>7</p></td>
<td><p>0</p></td>
<td><p>2 (C extensions, locking dependencies)</p></td>
</tr>
<tr><td><p>PDM</p></td>
<td><p>Frost Ming</p></td>
<td><p>Packaging and managing dependencies</p></td>
<td><p>8</p></td>
<td><p>0</p></td>
<td><p>1 (C extensions)</p></td>
</tr>
</tbody>
</table>
</div>
<details style="margin-bottom: 1rem">
<summary style="background: rgba(0, 170, 221, 10%); padding: .25rem; border-radius: .25rem">Expand table with more details about support for each feature</summary>
<div style="font-size: 90%; margin-top: .5rem"><table class="table table-hover">
<thead>
<tr><th class="head"><p>Tool</p></th>
<th class="head"><p>F1 (Envs)</p></th>
<th class="head"><p>F2 (Install)</p></th>
<th class="head"><p>F3 (Apps)</p></th>
<th class="head"><p>F4 (Libraries)</p></th>
<th class="head"><p>F5 (Extensions)</p></th>
<th class="head"><p>F6 (Editable)</p></th>
<th class="head"><p>F7 (Lock)</p></th>
<th class="head"><p>F8 (pyproject.toml)</p></th>
<th class="head"><p>F9 (Upload)</p></th>
</tr>
</thead>
<tbody>
<tr><td><p>setuptools</p></td>
<td><p>No</p></td>
<td><p>Only if authoring the package, direct use not recommended</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>Beta</p></td>
<td><p>No (can build sdist)</p></td>
</tr>
<tr><td><p>pip</p></td>
<td><p>No</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>Yes</p></td>
<td><p>Manually</p></td>
<td><p>N/A</p></td>
<td><p>No</p></td>
</tr>
<tr><td><p>venv</p></td>
<td><p>Only creating environments</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
</tr>
<tr><td><p>wheel</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No (can build wheels)</p></td>
</tr>
<tr><td><p>Twine</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>Yes</p></td>
</tr>
<tr><td><p>pip-tools</p></td>
<td><p>No</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
</tr>
<tr><td><p>virtualenvwrapper</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
</tr>
<tr><td><p>pipx</p></td>
<td><p>Sort of</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
</tr>
<tr><td><p>conda</p></td>
<td><p>Yes</p></td>
<td><p>Yes (from conda channels)</p></td>
<td><p>Develop (conda-build is a separate tool)</p></td>
<td><p>With conda-build</p></td>
<td><p>With conda-build</p></td>
<td><p>Yes</p></td>
<td><p>Manually</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
</tr>
<tr><td><p>Pipenv</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Only develop</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>No</p></td>
</tr>
<tr><td><p>Poetry</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Sort of (custom build.py script)</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Yes, but using custom fields</p></td>
<td><p>Yes</p></td>
</tr>
<tr><td><p>Flit</p></td>
<td><p>No</p></td>
<td><p>Only if authoring the package</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
</tr>
<tr><td><p>Hatch</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
</tr>
<tr><td><p>PDM</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>No</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
<td><p>Yes</p></td>
</tr>
</tbody>
</table>
</div>
</details><p>You should pay close attention to the Maintainer column in the table. The vast majority of them are maintained by PyPA, the Python Packaging Authority. Even more curiously, the two tools that have the most “Yes” values (Poetry and PDM) are not maintained by the PyPA, but instead other people completely independent of them and not participating in the working group. So, is the working group successful, if it cannot produce one fully-featured tool? Is the group successful if it has multiple projects with overlapping responsibilities? Should the group focus their efforts on standards like <a class="reference external" href="https://peps.python.org/pep-0517/">PEP 517</a>, which is a common API for packaging tools, and which also encourages the creation of even more incompatible and competing tools?</p>
<p>Most importantly: which tool should a beginner use? The PyPA has a few guides and tutorials, one is <a class="reference external" href="https://packaging.python.org/en/latest/tutorials/installing-packages/">using pip + venv</a>, another is <a class="reference external" href="https://packaging.python.org/en/latest/tutorials/managing-dependencies/">using pipenv</a> (why would you still do that?), and <a class="reference external" href="https://packaging.python.org/en/latest/tutorials/packaging-projects/">another tutorial</a> that lets you pick between Hatchling (hatch’s build backend), setuptools, Flit, and PDM, without explaining the differences between them—and without using any environment tools, and without using Hatch’s/PDM’s build and PyPI upload features (instead opting to use <code class="docutils literal">python <span class="pre">-m</span> build</code> and <code class="docutils literal">twine</code>). The concept of virtual environments can be very confusing for beginners, and managing virtual environments is difficult if everyone has incompatible opinions about it.</p>
<p>It is also notable that <a class="reference external" href="https://peps.python.org/pep-0020/">PEP 20</a>, the Zen of Python, states this:</p>
<blockquote>
<p><em>There should be one-- and preferably only one --obvious way to do it.</em></p>
</blockquote>
<p>Python packaging definitely does not follow it <a class="brackets" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-1" id="footnote-reference-1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a>. There are 14 ways, and none of them is obvious or the only good one. All in all, this is an unsalvageable mess. Why can’t Python pick one tool? What does the competition do? We’ll look at this in a minute. But first, let’s talk about the elephant in the room: Python virtual environments.</p>
</section>
<section id="does-python-really-need-virtual-environments">
<h1>Does Python really need virtual environments?</h1>
<p>Python relies on virtual environments for separation between projects. Virtual environments (aka virtualenvs or venvs) are folders with symlinks to a system-installed Python, and their own set of site-packages. There are a few problems with them:</p>
<section id="how-to-use-python-from-a-virtual-environment">
<h2>How to use Python from a virtual environment?</h2>
<p>There are two ways to do this. The first one is to activate it, by running the activate shell script installed in the environment’s bin directory. Another is to run the python executable (or any other script in the bin directory) directly from the venv. <a class="brackets" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-2" id="footnote-reference-2" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a></p>
<p>Activating venvs directly is more convenient for developers, but it also has some problems. Sometimes, activation fails to work, due to the shell caching the location of things in <code class="docutils literal">$PATH</code>. Also, beginners are taught to <code class="docutils literal">activate</code> and run <code class="docutils literal">python</code>, which means they might be confused and try to use activate in scripts or cronjobs (but in those environments, you should not activate venvs, and instead use the Python executable directly). Virtual environment activation is more state you need to be aware of, and if you forget about it, or if it breaks, you might end up messing up your user-wide (or worse, system-wide) Python packages.</p>
</section>
<section id="how-are-system-pythons-and-virtual-environments-related">
<h2>How are (system) Pythons and virtual environments related?</h2>
<p>The virtual environment depends very tightly on the (system/global/pyenv-installed) Python used to create it. This is good for disk-space reasons (clean virtual environments don’t take up very much space), but this also makes the environment more fragile. If the Python used to create the environment is removed, the virtual environment stops working. If you fully manage your own Python, then it’s probably not going to happen, but if you depend on a system Python, upgrading packages on your OS might end up replacing Python 3.10 with Python 3.11. Some distributions (e.g. Ubuntu) would only make a jump like this on a new distribution release (so you can plan ahead), some of them (e.g. Arch) are rolling-release and a regular system upgrade may include a new Python, whereas some (e.g. Homebrew) make it even worse by using paths that include the patch Python version (3.x.y), which cause virtual environments to break much more often.</p>
</section>
<section id="how-to-manage-virtual-environments">
<h2>How to manage virtual environments?</h2>
<p>The original virtualenv tool, and its simplified standard library rewrite venv, allow you to put a virtual environment anywhere in the file system, as long as you have write privileges there. This has led to people and tools inventing their own standards. Virtualenvwrapper stores environments in a central location, and does not care about their contents. Pipenv and poetry allow you to choose (either a central location or the .venv directory in the project), and environments are tied to a project (they will use the project-specific environment if you’re in the project directory). Hatch stores environments in a central location, and it allows you to have multiple environments per project (but there is no option to share environments between projects).</p>
<p>Brett Cannon has recently done <a class="reference external" href="https://snarky.ca/classifying-python-virtual-environment-workflows/">a survey</a>, and it has shown the community is split on their workflows: some people use a central location, some put them in the project directory, some people have multiple environments with different Python versions, some people reuse virtualenvs between projects… Everyone has different needs, and different opinions. For example, I use a central directory (~/virtualenvs) and reuse environments when working on Nikola (sharing the same environment between development and 4 Nikola sites). But on the other hand, when deploying web apps, the venv lives in the project folder, because this venv needs to be used by processes running as different users (me, root, or the service account for the web server, which might have interactive login disabled, or whose home directory may be set to something ephemeral).</p>
<p>So: <strong>does Python need virtual environments?</strong> Perhaps looking how other languages handle this problem can help us figure this out for Python?</p>
</section>
</section>
<section id="how-everyone-else-is-doing-it">
<h1>How everyone else is doing it</h1>
<p>We’ll look at two ecosystems. We’ll start with <a class="reference internal" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#javascript-node-js-with-npm">JavaScript/Node.js (with npm)</a>, and then we’ll look at the <a class="reference internal" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#c-net-with-dotnet-cli-msbuild">C#/.NET (with dotnet CLI/MSBuild)</a> ecosystem for comparison. We’ll demonstrate a sample flow of making a project, installing dependencies in it, and running things. If you’re familiar with those ecosystems and want to skip the examples, continue with <a class="reference internal" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#how-is-node-better-than-python">How is Node better than Python?</a> and <a class="reference internal" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#are-those-ecosystems-tools-perfect">Are those ecosystems’ tools perfect?</a>. Otherwise, read on.</p>
<section id="javascript-node-js-with-npm">
<h2>JavaScript/Node.js (with npm)</h2>
<p>There are two tools for dealing with packages in the Node world, namely npm and Yarn. The npm CLI tool is shipped with Node, so we’ll focus on it.</p>
<p>Let’s create a project:</p>
<div class="code"><pre class="code text"><a id="rest_code_21bf981bce0e4148bc3f2129aa4786ec-1" name="rest_code_21bf981bce0e4148bc3f2129aa4786ec-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_21bf981bce0e4148bc3f2129aa4786ec-1"></a>$ mkdir mynpmproject
<a id="rest_code_21bf981bce0e4148bc3f2129aa4786ec-2" name="rest_code_21bf981bce0e4148bc3f2129aa4786ec-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_21bf981bce0e4148bc3f2129aa4786ec-2"></a>$ cd mynpmproject
<a id="rest_code_21bf981bce0e4148bc3f2129aa4786ec-3" name="rest_code_21bf981bce0e4148bc3f2129aa4786ec-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_21bf981bce0e4148bc3f2129aa4786ec-3"></a>$ npm init
<a id="rest_code_21bf981bce0e4148bc3f2129aa4786ec-4" name="rest_code_21bf981bce0e4148bc3f2129aa4786ec-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_21bf981bce0e4148bc3f2129aa4786ec-4"></a>…answer a few questions…
<a id="rest_code_21bf981bce0e4148bc3f2129aa4786ec-5" name="rest_code_21bf981bce0e4148bc3f2129aa4786ec-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_21bf981bce0e4148bc3f2129aa4786ec-5"></a>$ ls
<a id="rest_code_21bf981bce0e4148bc3f2129aa4786ec-6" name="rest_code_21bf981bce0e4148bc3f2129aa4786ec-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_21bf981bce0e4148bc3f2129aa4786ec-6"></a>package.json
</pre></div>
<p>We’ve got a package.json file, which has some metadata about our project (name, version, description, license). Let’s install a dependency:</p>
<div class="code"><pre class="code text"><a id="rest_code_46c57d5e926a4f7f9dd6ae4afd442928-1" name="rest_code_46c57d5e926a4f7f9dd6ae4afd442928-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_46c57d5e926a4f7f9dd6ae4afd442928-1"></a>$ npm install --save is-even
<a id="rest_code_46c57d5e926a4f7f9dd6ae4afd442928-2" name="rest_code_46c57d5e926a4f7f9dd6ae4afd442928-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_46c57d5e926a4f7f9dd6ae4afd442928-2"></a>
<a id="rest_code_46c57d5e926a4f7f9dd6ae4afd442928-3" name="rest_code_46c57d5e926a4f7f9dd6ae4afd442928-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_46c57d5e926a4f7f9dd6ae4afd442928-3"></a>added 5 packages, and audited 6 packages in 2s
<a id="rest_code_46c57d5e926a4f7f9dd6ae4afd442928-4" name="rest_code_46c57d5e926a4f7f9dd6ae4afd442928-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_46c57d5e926a4f7f9dd6ae4afd442928-4"></a>
<a id="rest_code_46c57d5e926a4f7f9dd6ae4afd442928-5" name="rest_code_46c57d5e926a4f7f9dd6ae4afd442928-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_46c57d5e926a4f7f9dd6ae4afd442928-5"></a>found 0 vulnerabilities
</pre></div>
<p>The mere existence of an <code class="docutils literal"><span class="pre">is-even</span></code> package is questionable; the fact that it includes four dependencies is yet another, and the fact that it depends on <code class="docutils literal"><span class="pre">is-odd</span></code> is even worse. But this post isn’t about <code class="docutils literal"><span class="pre">is-even</span></code> or the Node ecosystem’s tendency to use tiny packages for everything (but I wrote one about this topic <a class="reference external" href="https://chriswarrick.com/blog/2019/02/15/modern-web-development-where-you-need-500-packages-to-build-bootstrap/">before</a>). Let’s look at what we have in the filesystem:</p>
<div class="code"><pre class="code text"><a id="rest_code_b7b59a6104744d158a65decb4d8b848f-1" name="rest_code_b7b59a6104744d158a65decb4d8b848f-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_b7b59a6104744d158a65decb4d8b848f-1"></a>$ ls
<a id="rest_code_b7b59a6104744d158a65decb4d8b848f-2" name="rest_code_b7b59a6104744d158a65decb4d8b848f-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_b7b59a6104744d158a65decb4d8b848f-2"></a>node_modules/  package.json  package-lock.json
<a id="rest_code_b7b59a6104744d158a65decb4d8b848f-3" name="rest_code_b7b59a6104744d158a65decb4d8b848f-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_b7b59a6104744d158a65decb4d8b848f-3"></a>$ ls node_modules
<a id="rest_code_b7b59a6104744d158a65decb4d8b848f-4" name="rest_code_b7b59a6104744d158a65decb4d8b848f-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_b7b59a6104744d158a65decb4d8b848f-4"></a>is-buffer/  is-even/  is-number/  is-odd/  kind-of/
</pre></div>
<p>Let’s also take a peek at the <code class="docutils literal">package.json</code> file:</p>
<div class="code"><pre class="code json"><a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-1" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-1"></a><span class="p">{</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-2" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-2"></a><span class="w">  </span><span class="nt">&quot;name&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;mynpmproject&quot;</span><span class="p">,</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-3" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-3"></a><span class="w">  </span><span class="nt">&quot;version&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;1.0.0&quot;</span><span class="p">,</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-4" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-4"></a><span class="w">  </span><span class="nt">&quot;description&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;&quot;</span><span class="p">,</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-5" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-5"></a><span class="w">  </span><span class="nt">&quot;main&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;index.js&quot;</span><span class="p">,</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-6" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-6"></a><span class="w">  </span><span class="nt">&quot;scripts&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">{</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-7" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-7" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-7"></a><span class="w">    </span><span class="nt">&quot;test&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-8" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-8" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-8"></a><span class="w">  </span><span class="p">},</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-9" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-9" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-9"></a><span class="w">  </span><span class="nt">&quot;author&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;&quot;</span><span class="p">,</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-10" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-10" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-10"></a><span class="w">  </span><span class="nt">&quot;license&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;ISC&quot;</span><span class="p">,</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-11" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-11" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-11"></a><span class="w">  </span><span class="nt">&quot;dependencies&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">{</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-12" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-12" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-12"></a><span class="w">    </span><span class="nt">&quot;is-even&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;^1.0.0&quot;</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-13" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-13" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-13"></a><span class="w">  </span><span class="p">}</span>
<a id="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-14" name="rest_code_2d75fbb0bc1d4353b74ed823295e39d5-14" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_2d75fbb0bc1d4353b74ed823295e39d5-14"></a><span class="p">}</span>
</pre></div>
<p>Our <code class="docutils literal">package.json</code> file now lists the dependency, and we’ve also got a lock file (<code class="docutils literal"><span class="pre">package-lock.json</span></code>), which records all the dependency versions used for this install. If this file is kept in the repository, any future attempts to <code class="docutils literal">npm install</code> will use the dependency versions listed in this file, ensuring everything will work the same as it did originally (unless one of those packages were to get removed from the registry).</p>
<p>Let’s try writing a trivial program using the module and try running it:</p>
<div class="code"><pre class="code text"><a id="rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-1" name="rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-1"></a>$ cat index.js
<a id="rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-2" name="rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-2"></a>var isEven = require(&#39;is-even&#39;);
<a id="rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-3" name="rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-3"></a>console.log(isEven(0));
<a id="rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-4" name="rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-4"></a>
<a id="rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-5" name="rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-5"></a>$ node index.js
<a id="rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-6" name="rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_e7fb3d8f089d440f9b0f5b9eff4e3e16-6"></a>true
</pre></div>
<p>Let’s try removing <code class="docutils literal"><span class="pre">is-odd</span></code> to demonstrate how badly designed this package is:</p>
<div class="code"><pre class="code text"><a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-1" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-1"></a>$ rm -rf node_modules/is-odd
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-2" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-2"></a>$ node index.js
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-3" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-3"></a>node:internal/modules/cjs/loader:998
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-4" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-4"></a>  throw err;
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-5" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-5"></a>  ^
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-6" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-6"></a>
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-7" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-7" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-7"></a>Error: Cannot find module &#39;is-odd&#39;
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-8" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-8" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-8"></a>Require stack:
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-9" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-9" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-9"></a>- /tmp/mynpmproject/node_modules/is-even/index.js
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-10" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-10" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-10"></a>- /tmp/mynpmproject/index.js
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-11" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-11" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-11"></a>    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-12" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-12" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-12"></a>    at Module._load (node:internal/modules/cjs/loader:841:27)
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-13" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-13" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-13"></a>    at Module.require (node:internal/modules/cjs/loader:1061:19)
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-14" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-14" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-14"></a>    at require (node:internal/modules/cjs/helpers:103:18)
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-15" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-15" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-15"></a>    at Object.&lt;anonymous&gt; (/tmp/mynpmproject/node_modules/is-even/index.js:10:13)
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-16" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-16" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-16"></a>    at Module._compile (node:internal/modules/cjs/loader:1159:14)
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-17" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-17" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-17"></a>    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-18" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-18" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-18"></a>    at Module.load (node:internal/modules/cjs/loader:1037:32)
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-19" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-19" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-19"></a>    at Module._load (node:internal/modules/cjs/loader:878:12)
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-20" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-20" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-20"></a>    at Module.require (node:internal/modules/cjs/loader:1061:19) {
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-21" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-21" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-21"></a>  code: &#39;MODULE_NOT_FOUND&#39;,
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-22" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-22" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-22"></a>  requireStack: [
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-23" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-23" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-23"></a>    &#39;/tmp/mynpmproject/node_modules/is-even/index.js&#39;,
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-24" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-24" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-24"></a>    &#39;/tmp/mynpmproject/index.js&#39;
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-25" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-25" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-25"></a>  ]
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-26" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-26" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-26"></a>}
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-27" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-27" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-27"></a>
<a id="rest_code_1d4b90082f5249fe9d590a962f9584c8-28" name="rest_code_1d4b90082f5249fe9d590a962f9584c8-28" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_1d4b90082f5249fe9d590a962f9584c8-28"></a>Node.js v18.12.1
</pre></div>
<section id="how-is-node-better-than-python">
<h3>How is Node better than Python?</h3>
<p>Badly designed packages aside, we can see an important difference from Python in that there is <strong>no virtual environment</strong>, and all the packages live in the project directory. If we fix the <code class="docutils literal">node_modules</code> directory by running <code class="docutils literal">npm install</code>, we can see that I can run the script from somewhere else on the file system:</p>
<div class="code"><pre class="code text"><a id="rest_code_bc27cf0914284bc4be97070d5b0880e8-1" name="rest_code_bc27cf0914284bc4be97070d5b0880e8-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_bc27cf0914284bc4be97070d5b0880e8-1"></a>$ pwd
<a id="rest_code_bc27cf0914284bc4be97070d5b0880e8-2" name="rest_code_bc27cf0914284bc4be97070d5b0880e8-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_bc27cf0914284bc4be97070d5b0880e8-2"></a>/tmp/mynpmproject
<a id="rest_code_bc27cf0914284bc4be97070d5b0880e8-3" name="rest_code_bc27cf0914284bc4be97070d5b0880e8-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_bc27cf0914284bc4be97070d5b0880e8-3"></a>$ npm install
<a id="rest_code_bc27cf0914284bc4be97070d5b0880e8-4" name="rest_code_bc27cf0914284bc4be97070d5b0880e8-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_bc27cf0914284bc4be97070d5b0880e8-4"></a>
<a id="rest_code_bc27cf0914284bc4be97070d5b0880e8-5" name="rest_code_bc27cf0914284bc4be97070d5b0880e8-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_bc27cf0914284bc4be97070d5b0880e8-5"></a>added 1 package, and audited 6 packages in 436ms
<a id="rest_code_bc27cf0914284bc4be97070d5b0880e8-6" name="rest_code_bc27cf0914284bc4be97070d5b0880e8-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_bc27cf0914284bc4be97070d5b0880e8-6"></a>
<a id="rest_code_bc27cf0914284bc4be97070d5b0880e8-7" name="rest_code_bc27cf0914284bc4be97070d5b0880e8-7" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_bc27cf0914284bc4be97070d5b0880e8-7"></a>found 0 vulnerabilities
<a id="rest_code_bc27cf0914284bc4be97070d5b0880e8-8" name="rest_code_bc27cf0914284bc4be97070d5b0880e8-8" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_bc27cf0914284bc4be97070d5b0880e8-8"></a>$ node /tmp/mynpmproject/index.js
<a id="rest_code_bc27cf0914284bc4be97070d5b0880e8-9" name="rest_code_bc27cf0914284bc4be97070d5b0880e8-9" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_bc27cf0914284bc4be97070d5b0880e8-9"></a>true
<a id="rest_code_bc27cf0914284bc4be97070d5b0880e8-10" name="rest_code_bc27cf0914284bc4be97070d5b0880e8-10" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_bc27cf0914284bc4be97070d5b0880e8-10"></a>$ cd ~
<a id="rest_code_bc27cf0914284bc4be97070d5b0880e8-11" name="rest_code_bc27cf0914284bc4be97070d5b0880e8-11" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_bc27cf0914284bc4be97070d5b0880e8-11"></a>$ node /tmp/mynpmproject/index.js
<a id="rest_code_bc27cf0914284bc4be97070d5b0880e8-12" name="rest_code_bc27cf0914284bc4be97070d5b0880e8-12" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_bc27cf0914284bc4be97070d5b0880e8-12"></a>true
</pre></div>
<p><strong>If you try to do that with a Python tool…</strong></p>
<ul class="simple">
<li><p>If you’re using a manually managed venv, you need to remember to activate it, or to use the appropriate Python.</p></li>
<li><p>If you’re using something fancier, it might be tied to the current working directory, and it may expect you to change into that directory, or to pass an argument pointing at that directory.</p></li>
</ul>
<p>I can also run my code as <code class="docutils literal">root</code>, and as an unprivileged <code class="docutils literal">nginx</code> user, without any special preparation (like telling pipenv/poetry to put their venv in the project directory, or running them as the other users):</p>
<div class="code"><pre class="code text"><a id="rest_code_eca6b7c0c3f0414097f504b2b256ead7-1" name="rest_code_eca6b7c0c3f0414097f504b2b256ead7-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_eca6b7c0c3f0414097f504b2b256ead7-1"></a>$ su -
<a id="rest_code_eca6b7c0c3f0414097f504b2b256ead7-2" name="rest_code_eca6b7c0c3f0414097f504b2b256ead7-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_eca6b7c0c3f0414097f504b2b256ead7-2"></a># node /tmp/mynpmproject/index.js
<a id="rest_code_eca6b7c0c3f0414097f504b2b256ead7-3" name="rest_code_eca6b7c0c3f0414097f504b2b256ead7-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_eca6b7c0c3f0414097f504b2b256ead7-3"></a>true
<a id="rest_code_eca6b7c0c3f0414097f504b2b256ead7-4" name="rest_code_eca6b7c0c3f0414097f504b2b256ead7-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_eca6b7c0c3f0414097f504b2b256ead7-4"></a># sudo -u nginx node /tmp/mynpmproject/index.js
<a id="rest_code_eca6b7c0c3f0414097f504b2b256ead7-5" name="rest_code_eca6b7c0c3f0414097f504b2b256ead7-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_eca6b7c0c3f0414097f504b2b256ead7-5"></a>true
</pre></div>
<p><strong>If you try to do that with a Python tool…</strong></p>
<ul class="simple">
<li><p>If you’re using a manually managed venv, you can use its Python as another user (assuming it has the right permissions).</p></li>
<li><p>If your tool puts the venv in the project directory, this will work too.</p></li>
<li><p>If your tool puts the venv in some weird place in your home folder, the other users will get their own venvs. The <code class="docutils literal">uwsgi</code> user on Fedora uses <code class="docutils literal">/run/uwsgi</code> as its home directory, and <code class="docutils literal">/run</code> is ephemeral (tmpfs), so a reboot forces you to reinstall things.</p></li>
</ul>
<p>We can even try to change the name of our project:</p>
<div class="code"><pre class="code text"><a id="rest_code_336d5b5f844a4ec6a1fac2fd04133907-1" name="rest_code_336d5b5f844a4ec6a1fac2fd04133907-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_336d5b5f844a4ec6a1fac2fd04133907-1"></a>$ cd /tmp
<a id="rest_code_336d5b5f844a4ec6a1fac2fd04133907-2" name="rest_code_336d5b5f844a4ec6a1fac2fd04133907-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_336d5b5f844a4ec6a1fac2fd04133907-2"></a>$ mv mynpmproject mynodeproject
<a id="rest_code_336d5b5f844a4ec6a1fac2fd04133907-3" name="rest_code_336d5b5f844a4ec6a1fac2fd04133907-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_336d5b5f844a4ec6a1fac2fd04133907-3"></a>$ node /tmp/mynodeproject/index.js
<a id="rest_code_336d5b5f844a4ec6a1fac2fd04133907-4" name="rest_code_336d5b5f844a4ec6a1fac2fd04133907-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_336d5b5f844a4ec6a1fac2fd04133907-4"></a>true
</pre></div>
<p><strong>If you try to do that with a Python tool…</strong></p>
<ul class="simple">
<li><p>If you’re using a manually managed venv, and it lives in a central directory, all is well.</p></li>
<li><p>If you or your tool places the venv in the project directory, the venv is now broken, and you need to recreate it (hope you have a recent <code class="docutils literal">requirements.txt</code>!)</p></li>
<li><p>If your tool puts the venv in some weird place in your home folder, it may decide that this is a different project, which means it will recreate it, and you’ll have an unused virtual environment somewhere on your filesystem.</p></li>
</ul>
</section>
<section id="other-packaging-topics">
<h3>Other packaging topics</h3>
<p>Some packages may expose executable scripts (with the <code class="docutils literal">bin</code> property). Those can be run in three ways:</p>
<ol class="arabic simple">
<li><p>Installed globally using <code class="docutils literal">npm install <span class="pre">-g</span></code>, which would put the script in a global location that’s likely in <code class="docutils literal">$PATH</code> (e.g. <code class="docutils literal">/usr/local/bin</code>).</p></li>
<li><p>Installed locally using <code class="docutils literal">npm install</code>, and executed with the <code class="docutils literal">npx</code> tool or manually by running the script in <code class="docutils literal"><span class="pre">node_packages/.bin</span></code>.</p></li>
<li><p>Not installed at all, but executed using the <code class="docutils literal">npx</code> tool, which will install it into a cache and run it.</p></li>
</ol>
<p>Also, if we wanted to publish our thing, we can just run <code class="docutils literal">npm publish</code> (after logging in with <code class="docutils literal">npm login</code>).</p>
</section>
</section>
<section id="c-net-with-dotnet-cli-msbuild">
<h2>C#/.NET (with dotnet CLI/MSBuild)</h2>
<p>In modern .NET, the One True Tool is the dotnet CLI, which uses MSBuild for most of the heavy lifting. (In the classic .NET Framework, the duties were split between MSBuild and NuGet.exe, but let’s focus on the modern workflow.)</p>
<p>Let’s create a project:</p>
<div class="code"><pre class="code text"><a id="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-1" name="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-1"></a>$ mkdir mydotnetproject
<a id="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-2" name="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-2"></a>$ cd mydotnetproject
<a id="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-3" name="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-3"></a>$ dotnet new console
<a id="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-4" name="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-4"></a>The template &quot;Console App&quot; was created successfully.
<a id="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-5" name="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-5"></a>
<a id="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-6" name="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-6"></a>Processing post-creation actions...
<a id="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-7" name="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-7" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-7"></a>Running &#39;dotnet restore&#39; on /tmp/mydotnetproject/mydotnetproject.csproj...
<a id="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-8" name="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-8" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-8"></a>  Determining projects to restore...
<a id="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-9" name="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-9" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-9"></a>  Restored /tmp/mydotnetproject/mydotnetproject.csproj (in 92 ms).
<a id="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-10" name="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-10" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-10"></a>Restore succeeded.
<a id="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-11" name="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-11" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-11"></a>$ ls
<a id="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-12" name="rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-12" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_5b2a5a2f4beb43b1a1391c52a8baf0e3-12"></a>mydotnetproject.csproj  obj/  Program.cs
</pre></div>
<p>We get three things: a <code class="docutils literal">mydotnetproject.csproj</code> file, which defines a few properties of our project; <code class="docutils literal">Program.cs</code>, which is a hello world program, and <code class="docutils literal">obj/</code>, which contains a few files you don’t need to care about.</p>
<p>Let’s try adding a dependency. For a pointless example, but slightly more reasonable than the JS one, we’ll use <code class="docutils literal">AutoFixture</code>, which brings in a dependency on <code class="docutils literal">Fare</code>. If we run <code class="docutils literal">dotnet add package AutoFixture</code>, we get some console output, and our <code class="docutils literal">mydotnetproject.csproj</code> now looks like this:</p>
<div class="code"><pre class="code xml"><a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-1" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-1"></a><span class="nt">&lt;Project</span><span class="w"> </span><span class="na">Sdk=</span><span class="s">&quot;Microsoft.NET.Sdk&quot;</span><span class="nt">&gt;</span>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-2" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-2"></a>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-3" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-3"></a><span class="w">  </span><span class="nt">&lt;PropertyGroup&gt;</span>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-4" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-4"></a><span class="w">    </span><span class="nt">&lt;OutputType&gt;</span>Exe<span class="nt">&lt;/OutputType&gt;</span>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-5" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-5"></a><span class="w">    </span><span class="nt">&lt;TargetFramework&gt;</span>net6.0<span class="nt">&lt;/TargetFramework&gt;</span>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-6" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-6"></a><span class="w">    </span><span class="nt">&lt;ImplicitUsings&gt;</span>enable<span class="nt">&lt;/ImplicitUsings&gt;</span>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-7" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-7" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-7"></a><span class="w">    </span><span class="nt">&lt;Nullable&gt;</span>enable<span class="nt">&lt;/Nullable&gt;</span>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-8" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-8" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-8"></a><span class="w">  </span><span class="nt">&lt;/PropertyGroup&gt;</span>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-9" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-9" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-9"></a>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-10" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-10" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-10"></a><span class="w">  </span><span class="nt">&lt;ItemGroup&gt;</span>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-11" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-11" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-11"></a><span class="w">    </span><span class="nt">&lt;PackageReference</span><span class="w"> </span><span class="na">Include=</span><span class="s">&quot;AutoFixture&quot;</span><span class="w"> </span><span class="na">Version=</span><span class="s">&quot;4.17.0&quot;</span><span class="w"> </span><span class="nt">/&gt;</span>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-12" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-12" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-12"></a><span class="w">  </span><span class="nt">&lt;/ItemGroup&gt;</span>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-13" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-13" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-13"></a>
<a id="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-14" name="rest_code_f08cc48954da4007ab1cd6ab092bfb4f-14" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f08cc48954da4007ab1cd6ab092bfb4f-14"></a><span class="nt">&lt;/Project&gt;</span>
</pre></div>
<p>The first <code class="docutils literal">&lt;PropertyGroup&gt;</code> specifies what our project is (Exe = something you can run), specifies the target framework (.NET 6.0 <a class="brackets" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-3" id="footnote-reference-3" role="doc-noteref"><span class="fn-bracket">[</span>3<span class="fn-bracket">]</span></a>), and enables a few opt-in features of C#. The second <code class="docutils literal">&lt;ItemGroup&gt;</code> was inserted when we installed AutoFixture.</p>
<p>We can now write a pointless program in C#. Here’s our new <code class="docutils literal">Program.cs</code>:</p>
<div class="code"><pre class="code csharp"><a id="rest_code_3d97641a75b0423c987f8f6fb0c78955-1" name="rest_code_3d97641a75b0423c987f8f6fb0c78955-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_3d97641a75b0423c987f8f6fb0c78955-1"></a><span class="k">using</span><span class="w"> </span><span class="nn">AutoFixture</span><span class="p">;</span>
<a id="rest_code_3d97641a75b0423c987f8f6fb0c78955-2" name="rest_code_3d97641a75b0423c987f8f6fb0c78955-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_3d97641a75b0423c987f8f6fb0c78955-2"></a><span class="kt">var</span><span class="w"> </span><span class="n">fixture</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Fixture</span><span class="p">();</span>
<a id="rest_code_3d97641a75b0423c987f8f6fb0c78955-3" name="rest_code_3d97641a75b0423c987f8f6fb0c78955-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_3d97641a75b0423c987f8f6fb0c78955-3"></a><span class="kt">var</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fixture</span><span class="p">.</span><span class="n">Create</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">();</span>
<a id="rest_code_3d97641a75b0423c987f8f6fb0c78955-4" name="rest_code_3d97641a75b0423c987f8f6fb0c78955-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_3d97641a75b0423c987f8f6fb0c78955-4"></a><span class="kt">var</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fixture</span><span class="p">.</span><span class="n">Create</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">();</span>
<a id="rest_code_3d97641a75b0423c987f8f6fb0c78955-5" name="rest_code_3d97641a75b0423c987f8f6fb0c78955-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_3d97641a75b0423c987f8f6fb0c78955-5"></a><span class="kt">var</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">a</span><span class="p">;</span>
<a id="rest_code_3d97641a75b0423c987f8f6fb0c78955-6" name="rest_code_3d97641a75b0423c987f8f6fb0c78955-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_3d97641a75b0423c987f8f6fb0c78955-6"></a><span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">result</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">&quot;Math is working&quot;</span><span class="p">:</span><span class="w"> </span><span class="s">&quot;Math is broken&quot;</span><span class="p">);</span>
</pre></div>
<p>(We could just use C#’s/.NET’s built-in random number generator, AutoFixture is complete overkill here—it’s meant for auto-generating test data, with support for arbitrary classes and other data structures, and we’re just getting two random ints here. I’m using AutoFixture for this example, because it’s simple to use and demonstrate, and because it gets us a transitive dependency.)</p>
<p>And now, we can run it:</p>
<div class="code"><pre class="code text"><a id="rest_code_d9dee65276204d4cbb9b65e089915241-1" name="rest_code_d9dee65276204d4cbb9b65e089915241-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_d9dee65276204d4cbb9b65e089915241-1"></a>$ dotnet run
<a id="rest_code_d9dee65276204d4cbb9b65e089915241-2" name="rest_code_d9dee65276204d4cbb9b65e089915241-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_d9dee65276204d4cbb9b65e089915241-2"></a>Math is working
</pre></div>
<p>If we want something that can be run outside of the project, and possibly without .NET installed on the system, we can use dotnet publish. The most basic scenario:</p>
<div class="code"><pre class="code text"><a id="rest_code_0ac8438553dd4f7baedc301fd45a9d61-1" name="rest_code_0ac8438553dd4f7baedc301fd45a9d61-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0ac8438553dd4f7baedc301fd45a9d61-1"></a>$ dotnet publish
<a id="rest_code_0ac8438553dd4f7baedc301fd45a9d61-2" name="rest_code_0ac8438553dd4f7baedc301fd45a9d61-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0ac8438553dd4f7baedc301fd45a9d61-2"></a>$ ls bin/Debug/net6.0/publish
<a id="rest_code_0ac8438553dd4f7baedc301fd45a9d61-3" name="rest_code_0ac8438553dd4f7baedc301fd45a9d61-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0ac8438553dd4f7baedc301fd45a9d61-3"></a>AutoFixture.dll*  Fare.dll*  mydotnetproject*  mydotnetproject.deps.json  mydotnetproject.dll  mydotnetproject.pdb  mydotnetproject.runtimeconfig.json
<a id="rest_code_0ac8438553dd4f7baedc301fd45a9d61-4" name="rest_code_0ac8438553dd4f7baedc301fd45a9d61-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0ac8438553dd4f7baedc301fd45a9d61-4"></a>$ du -h bin/Debug/net6.0/publish
<a id="rest_code_0ac8438553dd4f7baedc301fd45a9d61-5" name="rest_code_0ac8438553dd4f7baedc301fd45a9d61-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0ac8438553dd4f7baedc301fd45a9d61-5"></a>424K    bin/Debug/net6.0/publish
<a id="rest_code_0ac8438553dd4f7baedc301fd45a9d61-6" name="rest_code_0ac8438553dd4f7baedc301fd45a9d61-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0ac8438553dd4f7baedc301fd45a9d61-6"></a>$ bin/Debug/net6.0/publish/mydotnetproject
<a id="rest_code_0ac8438553dd4f7baedc301fd45a9d61-7" name="rest_code_0ac8438553dd4f7baedc301fd45a9d61-7" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0ac8438553dd4f7baedc301fd45a9d61-7"></a>Math is working
</pre></div>
<p>You can see that we’ve got a few files related to our project, as well as <code class="docutils literal">AutoFixture.dll</code> and <code class="docutils literal">Fare.dll</code>, which are our dependencies (<code class="docutils literal">Fare.dll</code> is a dependency of <code class="docutils literal">AutoFixture.dll</code>). Now, let’s try to remove <code class="docutils literal">AutoFixture.dll</code> from the published distribution:</p>
<div class="code"><pre class="code text"><a id="rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-1" name="rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-1"></a>$ rm bin/Debug/net6.0/publish/AutoFixture.dll
<a id="rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-2" name="rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-2"></a>$ bin/Debug/net6.0/publish/mydotnetproject
<a id="rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-3" name="rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-3"></a>Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly &#39;AutoFixture, Version=4.17.0.0, Culture=neutral, PublicKeyToken=b24654c590009d4f&#39;. The system cannot find the file specified.
<a id="rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-4" name="rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-4"></a>
<a id="rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-5" name="rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-5"></a>File name: &#39;AutoFixture, Version=4.17.0.0, Culture=neutral, PublicKeyToken=b24654c590009d4f&#39;
<a id="rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-6" name="rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_0a8ff1b606084dffa7e66c2b7f31fb5a-6"></a>[1]    45060 IOT instruction (core dumped)  bin/Debug/net6.0/publish/mydotnetproject
</pre></div>
<p>We can also try a more advanced scenario:</p>
<div class="code"><pre class="code text"><a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-1" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-1"></a>$ rm -rf bin obj  # clean up, just in case
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-2" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-2"></a>$ dotnet publish --sc -r linux-x64 -p:PublishSingleFile=true -o myoutput
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-3" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-3"></a>Microsoft (R) Build Engine version 17.0.1+b177f8fa7 for .NET
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-4" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-4"></a>Copyright (C) Microsoft Corporation. All rights reserved.
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-5" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-5"></a>
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-6" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-6"></a>  Determining projects to restore...
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-7" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-7" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-7"></a>  Restored /tmp/mydotnetproject/mydotnetproject.csproj (in 4.09 sec).
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-8" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-8" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-8"></a>  mydotnetproject -&gt; /tmp/mydotnetproject/bin/Debug/net6.0/linux-x64/mydotnetproject.dll
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-9" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-9" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-9"></a>  mydotnetproject -&gt; /tmp/mydotnetproject/myoutput/
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-10" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-10" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-10"></a>$ ls myoutput
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-11" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-11" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-11"></a>mydotnetproject*  mydotnetproject.pdb
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-12" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-12" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-12"></a>$ myoutput/mydotnetproject
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-13" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-13" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-13"></a>Math is working
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-14" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-14" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-14"></a>$ du -h myoutput/*
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-15" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-15" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-15"></a>62M     myoutput/mydotnetproject
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-16" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-16" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-16"></a>12K     myoutput/mydotnetproject.pdb
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-17" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-17" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-17"></a>$ file -k myoutput/mydotnetproject
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-18" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-18" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-18"></a>myoutput/mydotnetproject: ELF 64-bit LSB pie executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=47637c667797007d777f4322729d89e7fa53a870, for GNU/Linux 2.6.32, stripped, too many notes (256)\012- data
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-19" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-19" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-19"></a>$ file -k myoutput/mydotnetproject.pdb
<a id="rest_code_f3c7156ebb3d43e2afd22d946c03183c-20" name="rest_code_f3c7156ebb3d43e2afd22d946c03183c-20" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_f3c7156ebb3d43e2afd22d946c03183c-20"></a>myoutput/mydotnetproject.pdb: Microsoft Roslyn C# debugging symbols version 1.0\012- data
</pre></div>
<p>We have a single output file that contains our program, its dependencies, and parts of the .NET runtime. We also get debugging symbols if we want to run our binary with a .NET debugger and see the associated source code. (There are ways to make the binary file smaller, and we can move most arguments of <code class="docutils literal">dotnet publish</code> to the .csproj file, but this post is about Python, not .NET, so I’m not going to focus on them too much.)</p>
<section id="how-is-net-better-than-python">
<h3>How is .NET better than Python?</h3>
<p>I’m not going to bore you with the same demonstrations I’ve already shown when discussing <a class="reference internal" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#how-is-node-better-than-python">How is Node better than Python?</a>, but:</p>
<ul class="simple">
<li><p>You can run built .NET projects as any user, from anywhere in the filesystem.</p></li>
<li><p>All you need to run your code is the output directory (publishing is optional, but useful to have a cleaner output, to simplify deployment, and to possibly enable compilation to native code).</p></li>
<li><p>If you do publish in single-executable mode, you can just distribute the single executable, and your users don’t even need to have .NET installed.</p></li>
<li><p>You do not need to manage environments, you do not need special tools to run your code, you do not need to think about the current working directory when running code.</p></li>
</ul>
</section>
<section id="other-packaging-topics-1">
<h3>Other packaging topics</h3>
<p>Locking dependencies is disabled by default, but if you add <code class="docutils literal"><span class="pre">&lt;RestorePackagesWithLockFile&gt;true&lt;/RestorePackagesWithLockFile&gt;</span></code> to the <code class="docutils literal">&lt;PropertyGroup&gt;</code> in your <code class="docutils literal">.csproj</code> file, you can enable it (and get a <code class="docutils literal">packages.lock.json</code> file in output).</p>
<p>Regarding <a class="reference external" href="https://learn.microsoft.com/en-us/dotnet/core/tools/global-tools">command line tools</a>, .NET has support for those as well. They can be installed globally or locally, and may be accessed via $PATH or via the <code class="docutils literal">dotnet</code> command.</p>
<p>As for publishing your package to NuGet.org or to another repository, you might want to look at the <a class="reference external" href="https://learn.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-the-dotnet-cli">full docs</a> for more details, but the short version is:</p>
<ol class="arabic simple">
<li><p>Add some metadata to the <code class="docutils literal">.csproj</code> file (e.g. <code class="docutils literal">PackageId</code> and <code class="docutils literal">Version</code>)</p></li>
<li><p>Run <code class="docutils literal">dotnet pack</code> to get a <code class="docutils literal">.nupkg</code> file</p></li>
<li><p>Run <code class="docutils literal">dotnet nuget push</code> to upload the <code class="docutils literal">.nupkg</code> file (passing the file name and an API key)</p></li>
</ol>
<p>Once again, everything is done with a single <code class="docutils literal">dotnet</code> tool. The .NET IDEs (in particular, Visual Studio and Rider) do offer friendly GUI versions of many features. Some of those GUIs might be doings things slightly differently behind the scenes, but this is transparent to the user (and the backend is still MSBuild or a close derivative of it). I can take a CLI-created project, add a dependency from Rider, and publish an executable from VS, and everything will work the same. And perhaps XML files aren’t as cool as TOML, but they’re still easy to work with in this case.</p>
</section>
</section>
<section id="other-languages-and-ecosystems">
<h2>Other languages and ecosystems</h2>
<p>While we have explored two tools for two languages in depth, there are also other languages that deserve at least a mention. In the <strong>Java</strong> world, the two most commonly used tools are Maven and Gradle. Both tools can be used to manage dependencies and build artifacts that can be executed or distributed further (things like JAR files). Other tools with support for building Java projects exist, but most people just pick one of the two. The community of <strong>Scala</strong>, which is another JVM-based language, prefers sbt (which can be used for plain Java as well), but there are also Maven or Gradle users in that community. Finally, two new-ish languages which are quite popular in the recent times, <strong>Go</strong> and <strong>Rust</strong>, have first-party tooling integrated with the rest of the toolchain. The <code class="docutils literal">go</code> command-line tool can accomplish many build/dependency/packaging tasks. Rust’s <code class="docutils literal">cargo</code>, which ships with the standard distribution of Rust, handles dependencies, builds, running code and tests, as well as publishing your stuff to a registry.</p>
</section>
<section id="are-those-ecosystems-tools-perfect">
<h2>Are those ecosystems’ tools perfect?</h2>
<p>Not always, they have their deficiencies as well. In the Node ecosystem, packages may execute arbitrary code on install, which can be a security risk (there are some known examples, like a npm package <a class="reference external" href="https://arstechnica.com/information-technology/2022/03/sabotage-code-added-to-popular-npm-package-wiped-files-in-russia-and-belarus/">wiping hard drives in Russia and Belarus</a>, or another one <a href="https://arstechnica.com/information-technology/2018/11/hacker-backdoors-widely-used-open-source-software-to-steal-bitcoin/">stealing <s>imaginary Internet money</s> Bitcoin</a>). Binary packages are not distributed on the npm registry directly, they’re either built with <code class="docutils literal"><span class="pre">node-gyp</span></code>, or have prebuilt packages downloaded via <code class="docutils literal"><span class="pre">node-pre-gyp</span></code> (which is a third-party tool).</p>
<p>In the .NET ecosystem, the tools also create an <code class="docutils literal">obj</code> directory with temporary files. Those temporary files are tied to the environment they’re running in, and while the tooling will usually re-create them if something changes, it can sometimes fail and leave you with confusing errors (which can generally be solved by removing the <code class="docutils literal">bin</code> and <code class="docutils literal">obj</code> directories). If a package depends on native code (which is not already available on the target OS as part of a shared library), it must include binary builds in the NuGet package for all the platforms it supports, as there is <a class="reference external" href="https://github.com/NuGet/Home/issues/9631">no standard way</a> to allow building something from source.</p>
<p>You can also find deficiencies in the tools for the other languages mentioned. Some people think Maven is terrible because it uses XML and Gradle is the way to go, and others think Gradle’s use of a Groovy-based DSL makes things much harder than they need to be and prefer Maven instead.</p>
</section>
</section>
<section id="pep-582-the-future-of-python-packaging">
<h1>PEP 582: the future of Python packaging?</h1>
<p>Recall that when introducing PDM, I mentioned <a class="reference external" href="https://peps.python.org/pep-0582/">PEP 582</a>. This PEP defines a <code class="docutils literal">__pypackages__</code> directory. This directory would be taken into consideration by Python when looking for imports. It would behave similarly to <code class="docutils literal">node_modules</code>. Since there will be no symlinks to the system Python, it will resolve the issues with moving the virtual environment. Because the packages live in the project, there is no problem with sharing a project directory between multiple system users. It might even be possible for different computers (but with the same Python version and OS) to share the <code class="docutils literal">__pypackages__</code> directory (in some specific cases). The proposed <code class="docutils literal">__pypackages__</code> directory structure has <code class="docutils literal"><span class="pre">lib/python3.10/site-packages/</span></code> subfolders, which still makes the “reinstall on Python upgrade” step mandatory, but it doesn’t apply to minor version upgrades, and if you’re dealing with a pure-Python dependency tree, <code class="docutils literal">mv __pypackages__/lib/python3.10 __pypackages__/lib/python3.11</code> might just work. This structure does make sense for binary dependencies, or for dependencies necessary only on older Python versions, as it allows you to use multiple Python versions with the same project directory. The PEP does not say anything about sharing <code class="docutils literal">__pypackages__</code> between projects, but you could probably solve that problem with symlinks (assuming the tooling doesn’t care if the directory is a symlink, and it shouldn’t care IMO).</p>
<p>While PEP 582 is a great vision, and it would simplify many package-related workflows, it hasn’t seen much care from the powers-that-be. The PEP was proposed in May 2018, and there’s even <a class="reference external" href="https://github.com/kushaldas/pep582/blob/main/pep582.py">a usable implementation</a> that’s less than 50 lines of code, there <a class="reference external" href="https://discuss.python.org/t/pep-582-python-local-packages-directory/963/">hasn’t been much progress</a> on having it accepted and implemented in Python proper. However, PDM does not care, and it allows you to enable the future on your own machine.</p>
<section id="enabling-the-future-on-your-own-machine">
<h2>Enabling the future on your own machine</h2>
<p>Let’s enable the future on my own machine. That will require one simple command:</p>
<div class="code"><pre class="code text"><a id="rest_code_57dd2fcf65b6410d89b4d31b34f42435-1" name="rest_code_57dd2fcf65b6410d89b4d31b34f42435-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_57dd2fcf65b6410d89b4d31b34f42435-1"></a>$ eval &quot;$(pdm --pep582)&quot;
</pre></div>
<p>After that, we can initialize our project and install requests into it. Let’s try:</p>
<div class="code"><pre class="code text"><a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-1" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-1"></a>$ mkdir mypdmproject
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-2" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-2"></a>$ cd mypdmproject
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-3" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-3"></a>$ pdm init
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-4" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-4"></a>Creating a pyproject.toml for PDM...
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-5" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-5"></a>Please enter the Python interpreter to use
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-6" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-6"></a>0. /usr/bin/python (3.11)
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-7" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-7" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-7"></a>1. /usr/bin/python3.11 (3.11)
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-8" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-8" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-8"></a>2. /usr/bin/python2.7 (2.7)
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-9" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-9" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-9"></a>Please select (0): 1
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-10" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-10" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-10"></a>Using Python interpreter: /usr/bin/python3.11 (3.11)
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-11" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-11" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-11"></a>Would you like to create a virtualenv with /usr/bin/python3.11? [y/n] (y): n
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-12" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-12" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-12"></a>You are using the PEP 582 mode, no virtualenv is created.
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-13" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-13" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-13"></a>For more info, please visit https://peps.python.org/pep-0582/
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-14" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-14" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-14"></a>Is the project a library that will be uploaded to PyPI [y/n] (n): n
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-15" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-15" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-15"></a>License(SPDX name) (MIT):
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-16" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-16" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-16"></a>Author name (Chris Warrick):
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-17" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-17" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-17"></a>Author email (…):
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-18" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-18" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-18"></a>Python requires(&#39;*&#39; to allow any) (&gt;=3.11):
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-19" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-19" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-19"></a>Changes are written to pyproject.toml.
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-20" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-20" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-20"></a>$ ls
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-21" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-21" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-21"></a>pyproject.toml
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-22" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-22" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-22"></a>$ pdm add requests
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-23" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-23" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-23"></a>Adding packages to default dependencies: requests
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-24" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-24" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-24"></a>🔒 Lock successful
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-25" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-25" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-25"></a>Changes are written to pdm.lock.
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-26" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-26" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-26"></a>Changes are written to pyproject.toml.
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-27" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-27" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-27"></a>Synchronizing working set with lock file: 5 to add, 0 to update, 0 to remove
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-28" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-28" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-28"></a>
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-29" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-29" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-29"></a>  ✔ Install charset-normalizer 2.1.1 successful
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-30" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-30" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-30"></a>  ✔ Install certifi 2022.12.7 successful
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-31" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-31" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-31"></a>  ✔ Install idna 3.4 successful
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-32" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-32" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-32"></a>  ✔ Install requests 2.28.1 successful
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-33" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-33" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-33"></a>  ✔ Install urllib3 1.26.13 successful
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-34" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-34" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-34"></a>
<a id="rest_code_8bf4eb95599e4a33a6173a9a786fd022-35" name="rest_code_8bf4eb95599e4a33a6173a9a786fd022-35" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_8bf4eb95599e4a33a6173a9a786fd022-35"></a>🎉 All complete!
</pre></div>
<p>So far, so good (I’m not a fan of emoji in terminals, but that’s my only real complaint here.) Our <code class="docutils literal">pyproject.toml</code> looks like this:</p>
<div class="code"><pre class="code toml"><a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-1" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-1"></a><span class="k">[tool.pdm]</span>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-2" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-2"></a>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-3" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-3"></a><span class="k">[project]</span>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-4" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-4"></a><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">&quot;&quot;</span>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-5" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-5"></a><span class="n">version</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">&quot;&quot;</span>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-6" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-6"></a><span class="n">description</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">&quot;&quot;</span>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-7" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-7" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-7"></a><span class="n">authors</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-8" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-8" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-8"></a><span class="w">    </span><span class="p">{</span><span class="n">name</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s2">&quot;Chris Warrick&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">email</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s2">&quot;…&quot;</span><span class="p">},</span>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-9" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-9" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-9"></a><span class="p">]</span>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-10" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-10" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-10"></a><span class="n">dependencies</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-11" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-11" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-11"></a><span class="w">    </span><span class="s2">&quot;requests&gt;=2.28.1&quot;</span><span class="p">,</span>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-12" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-12" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-12"></a><span class="p">]</span>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-13" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-13" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-13"></a><span class="n">requires-python</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">&quot;&gt;=3.11&quot;</span>
<a id="rest_code_05fafc9392584b41a4c7d09ebcf57aee-14" name="rest_code_05fafc9392584b41a4c7d09ebcf57aee-14" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_05fafc9392584b41a4c7d09ebcf57aee-14"></a><span class="n">license</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="n">text</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s2">&quot;MIT&quot;</span><span class="p">}</span>
</pre></div>
<p>If we try to look into our file structure, we have this:</p>
<div class="code"><pre class="code text"><a id="rest_code_dfba376b40f84648b2865a0745760a19-1" name="rest_code_dfba376b40f84648b2865a0745760a19-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_dfba376b40f84648b2865a0745760a19-1"></a>$ ls
<a id="rest_code_dfba376b40f84648b2865a0745760a19-2" name="rest_code_dfba376b40f84648b2865a0745760a19-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_dfba376b40f84648b2865a0745760a19-2"></a>pdm.lock  __pypackages__/  pyproject.toml
<a id="rest_code_dfba376b40f84648b2865a0745760a19-3" name="rest_code_dfba376b40f84648b2865a0745760a19-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_dfba376b40f84648b2865a0745760a19-3"></a>$ ls __pypackages__
<a id="rest_code_dfba376b40f84648b2865a0745760a19-4" name="rest_code_dfba376b40f84648b2865a0745760a19-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_dfba376b40f84648b2865a0745760a19-4"></a>3.11/
<a id="rest_code_dfba376b40f84648b2865a0745760a19-5" name="rest_code_dfba376b40f84648b2865a0745760a19-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_dfba376b40f84648b2865a0745760a19-5"></a>$ ls __pypackages__/3.11
<a id="rest_code_dfba376b40f84648b2865a0745760a19-6" name="rest_code_dfba376b40f84648b2865a0745760a19-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_dfba376b40f84648b2865a0745760a19-6"></a>bin/  include/  lib/
<a id="rest_code_dfba376b40f84648b2865a0745760a19-7" name="rest_code_dfba376b40f84648b2865a0745760a19-7" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_dfba376b40f84648b2865a0745760a19-7"></a>$ ls __pypackages__/3.11/lib
<a id="rest_code_dfba376b40f84648b2865a0745760a19-8" name="rest_code_dfba376b40f84648b2865a0745760a19-8" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_dfba376b40f84648b2865a0745760a19-8"></a>certifi/             certifi-2022.12.7.dist-info/
<a id="rest_code_dfba376b40f84648b2865a0745760a19-9" name="rest_code_dfba376b40f84648b2865a0745760a19-9" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_dfba376b40f84648b2865a0745760a19-9"></a>idna/                idna-3.4.dist-info/
<a id="rest_code_dfba376b40f84648b2865a0745760a19-10" name="rest_code_dfba376b40f84648b2865a0745760a19-10" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_dfba376b40f84648b2865a0745760a19-10"></a>charset_normalizer/  charset_normalizer-2.1.1.dist-info/
<a id="rest_code_dfba376b40f84648b2865a0745760a19-11" name="rest_code_dfba376b40f84648b2865a0745760a19-11" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_dfba376b40f84648b2865a0745760a19-11"></a>requests/            requests-2.28.1.dist-info/
<a id="rest_code_dfba376b40f84648b2865a0745760a19-12" name="rest_code_dfba376b40f84648b2865a0745760a19-12" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_dfba376b40f84648b2865a0745760a19-12"></a>urllib3/             urllib3-1.26.13.dist-info/
</pre></div>
<p>We’ll write a simple Python program (let’s call it <code class="docutils literal">mypdmproject.py</code>) that makes a HTTP request using <code class="docutils literal">requests</code>. It will also print <code class="docutils literal">requests.__file__</code> so we’re sure it isn’t using some random system copy: <a class="brackets" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-4" id="footnote-reference-4" role="doc-noteref"><span class="fn-bracket">[</span>4<span class="fn-bracket">]</span></a></p>
<div class="code"><pre class="code python"><a id="rest_code_88da4613f9c142128800d8f4917c7e96-1" name="rest_code_88da4613f9c142128800d8f4917c7e96-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_88da4613f9c142128800d8f4917c7e96-1"></a><span class="kn">import</span><span class="w"> </span><span class="nn">requests</span>
<a id="rest_code_88da4613f9c142128800d8f4917c7e96-2" name="rest_code_88da4613f9c142128800d8f4917c7e96-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_88da4613f9c142128800d8f4917c7e96-2"></a><span class="nb">print</span><span class="p">(</span><span class="n">requests</span><span class="o">.</span><span class="vm">__file__</span><span class="p">)</span>
<a id="rest_code_88da4613f9c142128800d8f4917c7e96-3" name="rest_code_88da4613f9c142128800d8f4917c7e96-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_88da4613f9c142128800d8f4917c7e96-3"></a><span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;https://chriswarrick.com/&quot;</span><span class="p">)</span>
<a id="rest_code_88da4613f9c142128800d8f4917c7e96-4" name="rest_code_88da4613f9c142128800d8f4917c7e96-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_88da4613f9c142128800d8f4917c7e96-4"></a><span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">text</span><span class="p">[:</span><span class="mi">15</span><span class="p">])</span>
</pre></div>
<div class="code"><pre class="code text"><a id="rest_code_d287b0c578984663a2928bb41455147e-1" name="rest_code_d287b0c578984663a2928bb41455147e-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_d287b0c578984663a2928bb41455147e-1"></a>$ python mypdmproject.py
<a id="rest_code_d287b0c578984663a2928bb41455147e-2" name="rest_code_d287b0c578984663a2928bb41455147e-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_d287b0c578984663a2928bb41455147e-2"></a>/tmp/mypdmproject/__pypackages__/3.11/lib/requests/__init__.py
<a id="rest_code_d287b0c578984663a2928bb41455147e-3" name="rest_code_d287b0c578984663a2928bb41455147e-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_d287b0c578984663a2928bb41455147e-3"></a>&lt;!DOCTYPE html&gt;
</pre></div>
<p>Let’s finally try the tests we’ve done in the other languages. Requests is useless without urllib3, so let’s remove it <a class="brackets" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-5" id="footnote-reference-5" role="doc-noteref"><span class="fn-bracket">[</span>5<span class="fn-bracket">]</span></a> and see how well it works.</p>
<div class="code"><pre class="code text"><a id="rest_code_025901aac58247a4a73a4e28c999ae5d-1" name="rest_code_025901aac58247a4a73a4e28c999ae5d-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_025901aac58247a4a73a4e28c999ae5d-1"></a>$ rm -rf __pypackages__/3.11/lib/urllib3*
<a id="rest_code_025901aac58247a4a73a4e28c999ae5d-2" name="rest_code_025901aac58247a4a73a4e28c999ae5d-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_025901aac58247a4a73a4e28c999ae5d-2"></a>$ python mypdmproject.py
<a id="rest_code_025901aac58247a4a73a4e28c999ae5d-3" name="rest_code_025901aac58247a4a73a4e28c999ae5d-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_025901aac58247a4a73a4e28c999ae5d-3"></a>Traceback (most recent call last):
<a id="rest_code_025901aac58247a4a73a4e28c999ae5d-4" name="rest_code_025901aac58247a4a73a4e28c999ae5d-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_025901aac58247a4a73a4e28c999ae5d-4"></a>  File &quot;/tmp/mypdmproject/mypdmproject.py&quot;, line 1, in &lt;module&gt;
<a id="rest_code_025901aac58247a4a73a4e28c999ae5d-5" name="rest_code_025901aac58247a4a73a4e28c999ae5d-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_025901aac58247a4a73a4e28c999ae5d-5"></a>    import requests
<a id="rest_code_025901aac58247a4a73a4e28c999ae5d-6" name="rest_code_025901aac58247a4a73a4e28c999ae5d-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_025901aac58247a4a73a4e28c999ae5d-6"></a>  File &quot;/tmp/mypdmproject/__pypackages__/3.11/lib/requests/__init__.py&quot;, line 43, in &lt;module&gt;
<a id="rest_code_025901aac58247a4a73a4e28c999ae5d-7" name="rest_code_025901aac58247a4a73a4e28c999ae5d-7" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_025901aac58247a4a73a4e28c999ae5d-7"></a>    import urllib3
<a id="rest_code_025901aac58247a4a73a4e28c999ae5d-8" name="rest_code_025901aac58247a4a73a4e28c999ae5d-8" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_025901aac58247a4a73a4e28c999ae5d-8"></a>ModuleNotFoundError: No module named &#39;urllib3&#39;
</pre></div>
<p>Finally, can we try with a different directory? How about a different user?</p>
<div class="code"><pre class="code text"><a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-1" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-1" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-1"></a>$ pdm install
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-2" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-2" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-2"></a>Synchronizing working set with lock file: 1 to add, 0 to update, 0 to remove
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-3" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-3" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-3"></a>
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-4" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-4" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-4"></a>  ✔ Install urllib3 1.26.13 successful
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-5" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-5" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-5"></a>
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-6" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-6" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-6"></a>🎉 All complete!
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-7" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-7" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-7"></a>$ pwd
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-8" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-8" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-8"></a>/tmp/mypdmproject
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-9" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-9" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-9"></a>$ cd ~
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-10" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-10" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-10"></a>$ python /tmp/mypdmproject/mypdmproject.py
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-11" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-11" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-11"></a>/tmp/mypdmproject/__pypackages__/3.11/lib/requests/__init__.py
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-12" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-12" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-12"></a>&lt;!DOCTYPE html&gt;
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-13" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-13" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-13"></a># su -s /bin/bash -c &#39;eval &quot;$(/tmp/pdmvenv/bin/pdm --pep582 bash)&quot;; python /tmp/mypdmproject/mypdmproject.py&#39; - nobody
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-14" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-14" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-14"></a>su: warning: cannot change directory to /nonexistent: No such file or directory
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-15" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-15" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-15"></a>/tmp/mypdmproject/__pypackages__/3.11/lib/requests/__init__.py
<a id="rest_code_9544e824a8e84543b9a705b203b8f3f0-16" name="rest_code_9544e824a8e84543b9a705b203b8f3f0-16" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#rest_code_9544e824a8e84543b9a705b203b8f3f0-16"></a>&lt;!DOCTYPE html&gt;
</pre></div>
<p>This is looking pretty good. An independent project manages to do what the big Authority failed to do over so many years.</p>
</section>
<section id="is-this-the-perfect-thing">
<h2>Is this the perfect thing?</h2>
<p>Well, almost. There are two things that I have complaints about. The first one is the <code class="docutils literal">pdm <span class="pre">--pep582</span></code> hack, but hopefully, the PyPA gets its act together and gets it into Python core soon. However, another important problem is the lack of separation from system site-packages. Avid readers of footnotes <a class="brackets" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-6" id="footnote-reference-6" role="doc-noteref"><span class="fn-bracket">[</span>6<span class="fn-bracket">]</span></a> might have noticed I had to use a Docker container in my PDM experiments, because requests is very commonly found in system <code class="docutils literal"><span class="pre">site-packages</span></code> (especially when using system Pythons, which have requests because of some random package, or because it was unbundled from pip). <a class="brackets" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-7" id="footnote-reference-7" role="doc-noteref"><span class="fn-bracket">[</span>7<span class="fn-bracket">]</span></a> This can break things in ways you don’t expect, because you might end up importing and depending on system-wide things, or mixing system-wide and local packages (if you don’t install an extra requirement, but those packages are present system-wide, then you might end up using an extra you haven’t asked for). This is an important problem—a good solution would be to disable system site-packages if a <code class="docutils literal">__pypackages__</code> directory is in use.</p>
</section>
<section id="the-part-where-the-steering-council-kills-it">
<h2>The part where the Steering Council kills it</h2>
<p>In late March 2023, the Python Steering Council has announced <a class="reference external" href="https://discuss.python.org/t/pep-582-python-local-packages-directory/963/430">the rejection of PEP 582</a>. The reasons cited in the SC decision cited the limitations of the PEP (the <code class="docutils literal">__pypackages__</code> directory not always being enough, and the lack of specification on how it would behave in edge cases). Another argument is that it is possible to get <code class="docutils literal">__pypackages__</code> via “one of the many existing customization mechanisms for <code class="docutils literal">sys.path</code>, like <code class="docutils literal">.pth</code> files or a <code class="docutils literal">sitecustomize</code> module” — things commonly considered hacks, not real solutions. While users certainly can do anything they want to the <code class="docutils literal">sys.path</code> (often with tragic consequences), the point of having a common standard is to encourage tools to add support for it — if you use the aforementioned hacks, your IDE might end up not noticing the packages or considering them part of your code (trying to index them and search for things in them). Another reason cited for the rejection is the disagreement among the packaging community, which should not be surprising, especially in light of the next section.</p>
<p>The PEP 582/<code class="docutils literal">__pypackages__</code> mechanism may become official one day, and finally make Python packaging approachable. That would probably require someone to step up and write a new PEP that would make more people happy. Or Python might be stuck with all these incompatible tools, and invent 10 more in the next few years. (PDM is still there, and it still supports <code class="docutils literal">__pypackages__</code>, even though its implementation isn’t exactly the same as suggested by the now-rejected PEP.) Python’s current trajectory, as demonstrated by this decision, and by many people still being forced to struggle with the needlessly complicated virtual environments, sounds an awful lot like <a class="reference external" href="https://en.wikipedia.org/wiki/%27No_Way_to_Prevent_This,%27_Says_Only_Nation_Where_This_Regularly_Happens">the classic Onion headline</a>: ‘No Way to Prevent This,’ Says Only Programming Community Where This Regularly Happens.</p>
</section>
</section>
<section id="pypa-versus-reality-packaging-survey-results-and-pypa-reaction">
<h1>PyPA versus reality: packaging survey results and PyPA reaction</h1>
<p>Some time ago, the PSF ran a survey on packaging. Over 8000 people responded. <a class="reference external" href="https://drive.google.com/file/d/1U5d5SiXLVkzDpS0i1dJIA4Hu5Qg704T9/view">The users have spoken:</a></p>
<ul class="simple">
<li><p>Most people think packaging is too complex.</p></li>
<li><p>An overwhelming majority prefers using just a single tool.</p></li>
<li><p>Most people also think the existence of multiple tools is not beneficial for the Python packaging ecosystem.</p></li>
<li><p>Virtually everyone would prefer a clearly defined official workflow.</p></li>
<li><p>Over 50% of responses think tools for other ecosystems are better at managing dependencies and installing packages.</p></li>
</ul>
<p>The next step after this survey was for the packaging community to <a class="reference external" href="https://discuss.python.org/t/python-packaging-strategy-discussion-part-1/22420">discuss its results</a> and try to come up with a new packaging strategy. The first post from Shamika Mohanan (the Packaging Project Manager at PSF) that triggered the discussion also focused heavily on the users’ vision to unify packaging tools and to have One True Tool. This discussion was open to people involved with the packaging world; many participants of the discussion are involved with PyPA, and I don’t think I’ve seen a single comment from the people behind Poetry or PDM.</p>
<p>Most of the thread ended up being discussion of binary extensions, including discussions of how to help tool proliferation by making it possible for tools that aren’t setuptools to build binary extensions. There was also a lot of focus on the scientific community’s issues with <a class="reference external" href="https://pypackaging-native.github.io/">libraries with native code</a>, heavily rooted in C/C++, and with attempts to replace Conda with new PyPA-approved tools. The “unified tool” for everyone else was mentioned in some posts, but they were certainly the minority.</p>
<p>Some PyPA members talked about a UX analysis, and that they expect the unified tool to be re-exporting functionality from existing tools—which immediately raises the question: which tools should it export functionality from and why? Is <code class="docutils literal">pip install <span class="pre">unified-packaging-tool</span></code> going to bring in all fourteen? Is the fact that users are unhappy with what they have, and many of them would be happy with something lke npm/dotnet/cargo, not enough to determine the UX direction of the unified tool?</p>
<p>Some of them are also against breaking existing workflows. Is a unified packaging tool going to work for every single user? Definitely not. But are there that many distinct basic workflows? If we ignore things that border on bikeshedding, such as src vs no-src, or venv locations, are there that many workflows to consider? Someone making a library and someone making an application do have different needs (e.g. with regard to publishing the package or acceptable dependency versions). Someone working with C extensions (or extensions using something like Cython) may have different needs, but their needs would usually be a superset of the needs of someone working on a pure-Python project. The scientific community might have more specialized needs, related to complex non-Python parts, but I am positive many of their points could be solved by the unified tool as well, even if it’s not by the time this tool reaches v1.0. It is also possible that the scientific community might prefer to stay with Conda, or with some evolution of it that brings it closer in line with the Unified Packaging Tool but also solves the scientists’ needs better than a tool also solving the non-scientists’ needs can.</p>
<p>Then there’s a discussion about the existing tools and which one is the tool for the future. The maintainer of Hatch (Ofek Lev) says that <a class="reference external" href="https://discuss.python.org/t/python-packaging-strategy-discussion-part-1/22420/4">Hatch can provide the “unified UX”</a>. But do the maintainers of Poetry or PDM agree? Poetry seems to be far more active than Hatch, going by GitHub issues, and it’s also worth noting that Hatch’s bus factor is 1 (with Ofek Lev responsible for 542 out of 576 commits to the master branch). <a class="reference external" href="https://discuss.python.org/t/python-packaging-strategy-discussion-part-1/22420/46">Russell Keith-Magee from BeeWare</a> has highlighted the fact that tooling aside, the PyPA does a bad job at communicating things. Russell mentioned that one of PyPA tutorials now uses Hatch, but there is no way to know if the PyPA considers Hatch to be the future, are people supposed to migrate onto Hatch, and is Flit, another recent PyPA tool, now useless? Russell also makes good points about focusing efforts: should people focus on helping Hatch support extension modules (which, according to the Hatch maintainer, is the last scenario requiring setuptools; other participants note that you can already build native code without setuptools), or should people focus on improving setuptools compatibility with PEP 517?</p>
<p>There were also some people stating their opinions on unifying things in various ways—and many of them are <a class="reference external" href="https://discuss.python.org/t/python-packaging-strategy-discussion-part-1/22420/136">against</a> <a class="reference external" href="https://discuss.python.org/t/python-packaging-strategy-discussion-part-1/22420/137">unifying</a> things. There were some voices of reason, like that of Russell Keith-Magee, or of <a class="reference external" href="https://discuss.python.org/t/python-packaging-strategy-discussion-part-1/22420/140">Simon Notley</a>, who correctly noticed the thread fails to resolve problems of developers, who are confused about packaging, and don’t understand the different choices available and how they interoperate. Simon does agree that native dependencies are important and happen often in Python projects (and so do I), but the users who responded to the survey had something else in mind — as exemplified by the discussion opening post, mentioning the user expecting the simplicity of Rust’s cargo, and by the survey results. 70% of the survey respondents also use <code class="docutils literal">npm</code>, so many Python users have already seen the simpler workflows. The survey respondents were also asked to rank a few focus areas based on importance. “Making Python packaging better serve common use cases and workflows” was ranked first out of the provided options <a class="brackets" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-8" id="footnote-reference-8" role="doc-noteref"><span class="fn-bracket">[</span>8<span class="fn-bracket">]</span></a> by 3248 participants. “Supporting a wider range of use cases (e.g. edge cases, etc.)” was ranked first by 379 people, and it was the least important in the minds of 2989 people.</p>
<p>One more point that highlights the detachment of packaging folk from reality was mentioned by Anderson Bravalheri. To Anderson, a new unified tool would be <a class="reference external" href="https://discuss.python.org/t/python-packaging-strategy-discussion-part-1/22420/133">disrespectful of the work</a> the maintainers of the existing tools put into maintaining them, and disrespectful of users who had to adapt to the packaging mess. This point is completely absurd. Was the replacement of MS-DOS/Windows 9x and Classic Mac OS with Windows NT and <s>Mac OS X</s> <s>OS X</s> macOS disrespectful to their respective designers, and the users who had to adapt to manually configuring minutiae, figuring out how to get all your software and hardware to run with weird limitations that were necessary in the 1980s, and the system crashing every once in a while? Was the replacement of horses with cars disrespectful to horses, and the people who were removing horse manure from the streets? Was the replacement of the Ford Model T with faster, safer, more efficient, and easier to use cars disrespectful to Henry Ford? Technology comes and goes, and sometimes, getting an improvement means we need to get rid of the old stuff. This applies outside of technology, too—you could come up with many examples of change in the world, which might have put some people out of power, but has greatly improved the lives of millions of people (the fall of communism in Europe, for example). Also, going back to the technology world of today, this sentiment suggests Anderson is far too attached to the software they write—is this a healthy approach?</p>
<p>Nobody raised PEP 582 or the complexity of virtual environments. It might not be visible from the ivory towers of packaging tool maintainers, who have years of experience dealing with them, but it certainly does exist for regular people, for people who think the Python provided by their Linux distro is good enough, and especially for people for whom Python is their introduction to programming.</p>
<p>I would like to once again highlight: that’s not just the opinion of one random rambling Chris. The opinion that Python packaging needs to be simplified and unified is held by about half of the 8774 people who took the survey.</p>
<p>But here’s one more interesting thing: Discourse, the platform that the discussion was held on, shows the number of times a link was clicked. Granted, this count might not be always accurate, but if we assume it is, the link to the results summary was clicked only 14 times (as of 2023-01-14 21:20 UTC). The discussion has 28 participants and 2.2k views. If we believe the link click counter, <strong>half of the discussion participants did not even bother reading what the people think</strong>.</p>
<img alt="/images/python-packaging/discourse-link-clicks.png" class="align-center" src="https://chriswarrick.com/images/python-packaging/discourse-link-clicks.png">
</section>
<section id="summary">
<h1>Summary</h1>
<p>Python packaging is a mess, and it always has been. There are tons of tools, mostly incompatible with each other, and no tool can solve <em>all</em> problems (especially no tool from the PyPA). PDM is really close to the ideal, since it can do away with the overhead of managing virtual environments—which is hopefully the future of Python packaging, or the 2010s of Node.js packaging (although it is not going to be the 2023 of Python packaging, considering the Steering Council rejection). Perhaps in a few years, Python developers (and more importantly, Python learners!) will be able to just <code class="docutils literal">pip install</code> (or <code class="docutils literal">pdm install</code>?) what they need, without worrying about some “virtual environment” thing, that is separate but not quite from a system Python, and that is not a virtual machine. Python needs less tools, not more.</p>
<p><a class="reference external" href="https://en.wikipedia.org/wiki/Carthago_delenda_est">Furthermore, I consider that the PyPA must be destroyed.</a> The strategy discussion highlights the fact that they are unable to make Python packaging work the way the users expect. The PyPA should focus on producing one good tool, and on getting PEP 582 into Python. A good way to achieve this would be to put its resources behind PDM. The issues with native code and binary wheels are important, but plain-Python workflows, or workflows with straightforward binary dependencies, are much more common, and need to be improved. This improvement needs to happen now.</p>
<p>Discuss in the comments below, on <a class="reference external" href="https://news.ycombinator.com/item?id=34390585">Hacker News</a>, or on <a class="reference external" href="https://www.reddit.com/r/Python/comments/10cnx5i/how_to_improve_python_packaging_or_why_fourteen/">Reddit</a>.</p>
</section>
<section id="footnotes">
<h1>Footnotes</h1>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="footnote-1" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-reference-1">1</a><span class="fn-bracket">]</span></span>
<p>Funnily enough, the aphorism itself fails at “one obvious way to do it”. It is with dashes set in two different ways (with spaces after but not before, and with spaces before but not after), and none of them is the correct one (most English style guides prefer no spaces, but some allow spaces on both sides).</p>
</aside>
<aside class="footnote brackets" id="footnote-2" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-reference-2">2</a><span class="fn-bracket">]</span></span>
<p>Apologies for the slight Linux focus of this post; all the points I make apply on Windows as well, but perhaps with some slightly different names and commands.</p>
</aside>
<aside class="footnote brackets" id="footnote-3" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-reference-3">3</a><span class="fn-bracket">]</span></span>
<p>There’s a new major version of .NET every year, with the even-numbered versions being LTS. Those are far less revolutionary than the Python 2 → 3 transition, and after you jump on the modern .NET train, upgrading a project to the new major version is fairly simple (possibly even just bumping the version number).</p>
</aside>
<aside class="footnote brackets" id="footnote-4" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-reference-4">4</a><span class="fn-bracket">]</span></span>
<p>And to be extra sure, I used a clean <code class="docutils literal">python:latest</code> Docker container, since requests is so commonly found in system site packages.</p>
</aside>
<aside class="footnote brackets" id="footnote-5" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-reference-5">5</a><span class="fn-bracket">]</span></span>
<p>A little caveat here, I also had to remove the <code class="docutils literal"><span class="pre">dist-info</span></code> folder, so that PDM would know it needs to be reinstalled.</p>
</aside>
<aside class="footnote brackets" id="footnote-6" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-reference-6">6</a><span class="fn-bracket">]</span></span>
<p>Yes, that’s you!</p>
</aside>
<aside class="footnote brackets" id="footnote-7" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-reference-7">7</a><span class="fn-bracket">]</span></span>
<p>Also, why is there no good HTTP client library in Python’s standard library? Is the “standard library is where packages go to die” argument still relevant, if requests had four releases in 2022, and urllib3 had six, and most of the changes were minor?</p>
</aside>
<aside class="footnote brackets" id="footnote-8" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/#footnote-reference-8">8</a><span class="fn-bracket">]</span></span>
<p>I have removed the “Other” option, and shifted all options ranked below it by one place, since we don’t know what the other thing was and how it related to the options presented (the free-form responses were removed from the public results spreadsheet to preserve the users’ anonymity). In the event a respondent left some of the options without a number, the blank options were not considered neither first nor last.</p>
</aside>
</aside>
</section>
<section id="revision-history">
<h1>Revision History</h1>
<p>This post got amended in April 2023 with an update about the SC rejection of PEP 582 (in a new subsection and in the Summary section).</p>
</section>
]]></content:encoded><category>Python</category><category>.NET</category><category>C#</category><category>JavaScript</category><category>Node.js</category><category>npm</category><category>packaging</category><category>PDM</category><category>pip</category><category>PyPA</category><category>Python</category><category>virtual environments</category></item><item><title>Python Hackery: merging signatures of two Python functions</title><dc:creator>Chris Warrick</dc:creator><link>https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/</link><pubDate>Thu, 20 Sep 2018 13:52:20 GMT</pubDate><guid>https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/</guid><description>
Today’s blog post is going to contain fairly advanced Python hackery. We’ll
take two functions — one is a wrapper for the other, but also adds some
positional arguments.  And we’ll change the signature displayed everywhere from
the uninformative f(new_arg, *args, **kwargs) to something more
appropriate.
</description><content:encoded><![CDATA[
<p>Today’s blog post is going to contain fairly advanced Python hackery. We’ll
take two functions — one is a wrapper for the other, but also adds some
positional arguments.  And we’ll change the signature displayed everywhere from
the uninformative <code class="docutils literal">f(new_arg, *args, **kwargs)</code> to something more
appropriate.</p>



<p>This blog post was inspired by F4D3C0D3 on #python (freenode IRC). I also took
some inspiration from
Gynvael Coldwind’s classic <a class="reference external" href="https://www.youtube.com/watch?v=7VJaprmuHcw">Python 101</a> (April Fools) video. (Audio and some comments are in Polish, but even if you don’t speak the language, it’s still worth it to click through the time bar and see some (fairly unusual) magic happen.)</p>
<section id="starting-point">
<h1>Starting point</h1>
<div class="code"><pre class="code python"><a id="rest_code_4d7b7ce96f28484bb5c554955ef085fa-1" name="rest_code_4d7b7ce96f28484bb5c554955ef085fa-1" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_4d7b7ce96f28484bb5c554955ef085fa-1"></a><span class="k">def</span><span class="w"> </span><span class="nf">old</span><span class="p">(</span><span class="n">foo</span><span class="p">,</span> <span class="n">bar</span><span class="p">):</span>
<a id="rest_code_4d7b7ce96f28484bb5c554955ef085fa-2" name="rest_code_4d7b7ce96f28484bb5c554955ef085fa-2" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_4d7b7ce96f28484bb5c554955ef085fa-2"></a><span class="w">    </span><span class="sd">&quot;&quot;&quot;This is old&#39;s docstring.&quot;&quot;&quot;</span>
<a id="rest_code_4d7b7ce96f28484bb5c554955ef085fa-3" name="rest_code_4d7b7ce96f28484bb5c554955ef085fa-3" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_4d7b7ce96f28484bb5c554955ef085fa-3"></a>    <span class="nb">print</span><span class="p">(</span><span class="n">foo</span><span class="p">,</span> <span class="n">bar</span><span class="p">)</span>
<a id="rest_code_4d7b7ce96f28484bb5c554955ef085fa-4" name="rest_code_4d7b7ce96f28484bb5c554955ef085fa-4" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_4d7b7ce96f28484bb5c554955ef085fa-4"></a>    <span class="k">return</span> <span class="n">foo</span> <span class="o">+</span> <span class="n">bar</span>
<a id="rest_code_4d7b7ce96f28484bb5c554955ef085fa-5" name="rest_code_4d7b7ce96f28484bb5c554955ef085fa-5" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_4d7b7ce96f28484bb5c554955ef085fa-5"></a>
<a id="rest_code_4d7b7ce96f28484bb5c554955ef085fa-6" name="rest_code_4d7b7ce96f28484bb5c554955ef085fa-6" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_4d7b7ce96f28484bb5c554955ef085fa-6"></a>
<a id="rest_code_4d7b7ce96f28484bb5c554955ef085fa-7" name="rest_code_4d7b7ce96f28484bb5c554955ef085fa-7" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_4d7b7ce96f28484bb5c554955ef085fa-7"></a><span class="k">def</span><span class="w"> </span><span class="nf">new</span><span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="n">foo</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<a id="rest_code_4d7b7ce96f28484bb5c554955ef085fa-8" name="rest_code_4d7b7ce96f28484bb5c554955ef085fa-8" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_4d7b7ce96f28484bb5c554955ef085fa-8"></a>    <span class="k">return</span> <span class="n">old</span><span class="p">(</span><span class="n">prefix</span> <span class="o">+</span> <span class="n">foo</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
<p>Let’s test it.</p>
<div class="code"><pre class="code pycon"><a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-1" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-1" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-1"></a><span class="gp">&gt;&gt;&gt; </span><span class="n">o</span> <span class="o">=</span> <span class="n">old</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">)</span>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-2" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-2" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-2"></a><span class="go">a b</span>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-3" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-3" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-3"></a><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">new</span><span class="p">(</span><span class="s1">&#39;!&#39;</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">)</span>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-4" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-4" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-4"></a><span class="go">!a b</span>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-5" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-5" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-5"></a><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">o</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&#39; - &#39;</span><span class="p">)</span>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-6" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-6" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-6"></a><span class="go">ab - !ab</span>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-7" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-7" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-7"></a><span class="gp">&gt;&gt;&gt; </span><span class="n">help</span><span class="p">(</span><span class="n">old</span><span class="p">)</span>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-8" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-8" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-8"></a><span class="go">Help on function old in module __main__:</span>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-9" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-9" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-9"></a>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-10" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-10" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-10"></a><span class="go">old(foo, bar)</span>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-11" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-11" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-11"></a><span class="go">    This is old&#39;s docstring.</span>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-12" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-12" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-12"></a>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-13" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-13" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-13"></a><span class="gp">&gt;&gt;&gt; </span><span class="n">help</span><span class="p">(</span><span class="n">new</span><span class="p">)</span>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-14" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-14" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-14"></a><span class="go">Help on function new in module __main__:</span>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-15" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-15" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-15"></a>
<a id="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-16" name="rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-16" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_bed2964a8ddd429a8b6c770e6bd1cbd7-16"></a><span class="go">new(prefix, foo, *args, **kwargs)</span>
</pre></div>
<p>The last line is not exactly informative — it doesn’t tell us that we need to
pass <code class="docutils literal">bar</code> as an argument.  Sure, you could define <code class="docutils literal">new</code> as just <code class="docutils literal">(prefix, foo,
bar)</code> — but that means every change to <code class="docutils literal">old</code> requires editing <code class="docutils literal">new</code> as
well. So, not ideal. Let’s try to fix this.</p>
</section>
<section id="the-existing-infrastructure-functools-wraps">
<h1>The existing infrastructure: functools.wraps</h1>
<p>First, let’s start with the basic facility Python already has.  The standard
library already comes with <code class="docutils literal">functools.wraps</code> and
<code class="docutils literal">functools.update_wrapper</code>.</p>
<p>If you’ve never heard of those two functions, here’s a crash course:</p>
<div class="code"><pre class="code python"><a id="rest_code_36bca7dffa6042ad98ceab813c492019-1" name="rest_code_36bca7dffa6042ad98ceab813c492019-1" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_36bca7dffa6042ad98ceab813c492019-1"></a><span class="k">def</span><span class="w"> </span><span class="nf">decorator</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
<a id="rest_code_36bca7dffa6042ad98ceab813c492019-2" name="rest_code_36bca7dffa6042ad98ceab813c492019-2" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_36bca7dffa6042ad98ceab813c492019-2"></a>    <span class="nd">@functools</span><span class="o">.</span><span class="n">wraps</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<a id="rest_code_36bca7dffa6042ad98ceab813c492019-3" name="rest_code_36bca7dffa6042ad98ceab813c492019-3" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_36bca7dffa6042ad98ceab813c492019-3"></a>    <span class="k">def</span><span class="w"> </span><span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<a id="rest_code_36bca7dffa6042ad98ceab813c492019-4" name="rest_code_36bca7dffa6042ad98ceab813c492019-4" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_36bca7dffa6042ad98ceab813c492019-4"></a>        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Inside wrapper&quot;</span><span class="p">)</span>
<a id="rest_code_36bca7dffa6042ad98ceab813c492019-5" name="rest_code_36bca7dffa6042ad98ceab813c492019-5" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_36bca7dffa6042ad98ceab813c492019-5"></a>        <span class="n">f</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<a id="rest_code_36bca7dffa6042ad98ceab813c492019-6" name="rest_code_36bca7dffa6042ad98ceab813c492019-6" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_36bca7dffa6042ad98ceab813c492019-6"></a>    <span class="k">return</span> <span class="n">wrapper</span>
<a id="rest_code_36bca7dffa6042ad98ceab813c492019-7" name="rest_code_36bca7dffa6042ad98ceab813c492019-7" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_36bca7dffa6042ad98ceab813c492019-7"></a>
<a id="rest_code_36bca7dffa6042ad98ceab813c492019-8" name="rest_code_36bca7dffa6042ad98ceab813c492019-8" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_36bca7dffa6042ad98ceab813c492019-8"></a><span class="nd">@decorator</span>
<a id="rest_code_36bca7dffa6042ad98ceab813c492019-9" name="rest_code_36bca7dffa6042ad98ceab813c492019-9" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_36bca7dffa6042ad98ceab813c492019-9"></a><span class="k">def</span><span class="w"> </span><span class="nf">square</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="nb">float</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">float</span><span class="p">:</span>
<a id="rest_code_36bca7dffa6042ad98ceab813c492019-10" name="rest_code_36bca7dffa6042ad98ceab813c492019-10" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_36bca7dffa6042ad98ceab813c492019-10"></a><span class="w">    </span><span class="sd">&quot;&quot;&quot;Square a number.&quot;&quot;&quot;</span>
<a id="rest_code_36bca7dffa6042ad98ceab813c492019-11" name="rest_code_36bca7dffa6042ad98ceab813c492019-11" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_36bca7dffa6042ad98ceab813c492019-11"></a>    <span class="k">return</span> <span class="n">n</span> <span class="o">*</span> <span class="n">n</span>
</pre></div>
<p>If we try to inspect the <code class="docutils literal">square</code> function, we’ll see the original name, arguments,
annotations, and the docstring.  If we ran this code again, but with the
<code class="docutils literal">&#64;functools.wraps(f)</code> line commented out, we would only see <code class="docutils literal"><span class="pre">wrapper(*args,</span>
**kwargs)</code>.</p>
<p>This approach gives us a hint of what we need to do.  However, if we apply
<code class="docutils literal">wraps</code> (or <code class="docutils literal">update_wrapper</code>, which is what <code class="docutils literal">wraps</code> ends up calling)
to our function, it will only have <code class="docutils literal">foo</code> and <code class="docutils literal">bar</code> as arguments, and its
name will be displayed as <code class="docutils literal">old</code>.</p>
<p>So, let’s take a look at <a class="reference external" href="https://github.com/python/cpython/blob/4fe8dc68577f9e22aaf24db08fb6647277c42d4c/Lib/functools.py#L27-L79">functools.update_wrapper</a>. What does it do? Two things:</p>
<ul class="simple">
<li><p>copy some attributes from the old function to the new one
(<code class="docutils literal">__module__</code>, <code class="docutils literal">__name__</code>, <code class="docutils literal">__qualname__</code>, <code class="docutils literal">__doc__</code>, <code class="docutils literal">__annotations__</code>)</p></li>
<li><p>update <code class="docutils literal">__dict__</code> of the new function</p></li>
<li><p>set <code class="docutils literal">wrapper.__wrapped__</code></p></li>
</ul>
<p>If we try to experiment with it — by changing the list of things to copy, for
example — we’ll find out that the annotations, the docstring, and the displayed name come from
the copied attributes, but the signature itself is apparently taken from <code class="docutils literal">__wrapped__</code>.</p>
<p>Further investigation reveals this fact about <code class="docutils literal">inspect.signature</code>:</p>
<blockquote>
<p><code class="docutils literal">inspect.signature(callable, *, follow_wrapped=True)</code></p>
<p><em>New in version 3.5:</em> <code class="docutils literal">follow_wrapped</code> parameter. Pass <code class="docutils literal">False</code> to get a signature of callable specifically (<code class="docutils literal">callable.__wrapped__</code> will not be used to unwrap decorated callables.)</p>
</blockquote>
<p>And so, this is our <strong>end goal:</strong></p>
<p class="lead">Craft a function with a specific signature (that merges <code class="docutils literal">old</code> and <code class="docutils literal">new</code>) and set it as <code class="docutils literal">new.__wrapped__</code>.</p>
<p>But first, we need to talk about parallel universes.</p>
<p>Or actually, code objects.</p>
</section>
<section id="defining-a-function-programmatically">
<h1>Defining a function programmatically</h1>
<p>Let’s try an experiment.</p>
<div class="code"><pre class="code pycon"><a id="rest_code_a62b013b29454ad78fe76dca6cddcf0f-1" name="rest_code_a62b013b29454ad78fe76dca6cddcf0f-1" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_a62b013b29454ad78fe76dca6cddcf0f-1"></a><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span><span class="w"> </span><span class="nf">foo</span><span class="p">(</span><span class="n">bar</span><span class="p">):</span> <span class="k">pass</span>
<a id="rest_code_a62b013b29454ad78fe76dca6cddcf0f-2" name="rest_code_a62b013b29454ad78fe76dca6cddcf0f-2" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_a62b013b29454ad78fe76dca6cddcf0f-2"></a><span class="gp">&gt;&gt;&gt; </span><span class="n">foo</span><span class="o">.</span><span class="n">__wrapped__</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="kc">None</span>
<a id="rest_code_a62b013b29454ad78fe76dca6cddcf0f-3" name="rest_code_a62b013b29454ad78fe76dca6cddcf0f-3" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_a62b013b29454ad78fe76dca6cddcf0f-3"></a><span class="gp">&gt;&gt;&gt; </span><span class="n">help</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>
<a id="rest_code_a62b013b29454ad78fe76dca6cddcf0f-4" name="rest_code_a62b013b29454ad78fe76dca6cddcf0f-4" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_a62b013b29454ad78fe76dca6cddcf0f-4"></a><span class="go">foo(x, y)</span>
</pre></div>
<p>So, there are two ways to do this.  The first one would be to generate a string
with the signature and just use <code class="docutils literal">eval</code> to get a <code class="docutils literal">__wrapped__</code> function. But
that would be cheating, and honestly, quite boring. (The inspect module could
help us with preparing the string.)  The second one? Create code objects
manually.</p>
<section id="code-objects">
<h2>Code objects</h2>
<p>To create a function, we’ll need the <code class="docutils literal">types</code> module. <code class="docutils literal">types.FunctionType</code>
gives us a function, but it asks us for a code object. As the <a class="reference external" href="https://docs.python.org/3/reference/datamodel.html">docs</a> state,
<em>Code objects represent byte-compiled executable Python code, or bytecode.</em></p>
<p>To create one by
hand, we’ll need <code class="docutils literal">types.CodeType</code>. Well, not exactly by hand — we’ll end up doing a three-way merge between
<code class="docutils literal">source</code> (<code class="docutils literal">old</code>), <code class="docutils literal">dest</code> (<code class="docutils literal">new</code>) and <code class="docutils literal">def <span class="pre">_blank():</span> pass</code> (a function
that does nothing).</p>
<p>Let’s look at the docstring for <code class="docutils literal">CodeType</code>:</p>
<div class="code"><pre class="code text"><a id="rest_code_6208107589054526ac90219ce627e4dc-1" name="rest_code_6208107589054526ac90219ce627e4dc-1" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_6208107589054526ac90219ce627e4dc-1"></a>code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,
<a id="rest_code_6208107589054526ac90219ce627e4dc-2" name="rest_code_6208107589054526ac90219ce627e4dc-2" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_6208107589054526ac90219ce627e4dc-2"></a>    constants, names, varnames, filename, name, firstlineno,
<a id="rest_code_6208107589054526ac90219ce627e4dc-3" name="rest_code_6208107589054526ac90219ce627e4dc-3" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_6208107589054526ac90219ce627e4dc-3"></a>    lnotab[, freevars[, cellvars]])
<a id="rest_code_6208107589054526ac90219ce627e4dc-4" name="rest_code_6208107589054526ac90219ce627e4dc-4" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_6208107589054526ac90219ce627e4dc-4"></a>
<a id="rest_code_6208107589054526ac90219ce627e4dc-5" name="rest_code_6208107589054526ac90219ce627e4dc-5" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_6208107589054526ac90219ce627e4dc-5"></a>Create a code object.  Not for the faint of heart.
</pre></div>
<p>All of the arguments end up being fields of a code objects (name starts with
<code class="docutils literal">co_</code>).  For each
function <code class="docutils literal">f</code>, its code object is <code class="docutils literal">f.__code__</code>. You can find the filename in
<code class="docutils literal">f.__code__.co_filename</code>, for example. The meaning of all fields can be
found in docs for the <a class="reference external" href="https://docs.python.org/3/library/inspect.html#types-and-members">inspect module</a>. We’ll be
interested in the following three fields:</p>
<ul class="simple">
<li><p><code class="docutils literal">argcount</code> — number of arguments (not including keyword only arguments, * or ** args)</p></li>
<li><p><code class="docutils literal">kwonlyargcount</code> — number of keyword only arguments (not including ** arg)</p></li>
<li><p><code class="docutils literal">varnames</code> — tuple of names of arguments and local variables</p></li>
</ul>
<p>For all the other fields, we’ll copy them from the appropriate function (one of
the three).  We don’t expect anyone to call the wrapped function directly; as
long as <code class="docutils literal">help</code> and <code class="docutils literal">inspect</code> members don’t crash when they look into it,
we’re fine.</p>
</section>
<section id="everything-you-need-to-know-about-function-arguments">
<h2>Everything you need to know about function arguments</h2>
<div class="code"><pre class="code pycon"><a id="rest_code_f4b49082358149cf861286d903952272-1" name="rest_code_f4b49082358149cf861286d903952272-1" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_f4b49082358149cf861286d903952272-1"></a><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span><span class="w"> </span><span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span> <span class="k">pass</span>
<a id="rest_code_f4b49082358149cf861286d903952272-2" name="rest_code_f4b49082358149cf861286d903952272-2" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_f4b49082358149cf861286d903952272-2"></a><span class="gp">&gt;&gt;&gt; </span><span class="n">inspect</span><span class="o">.</span><span class="n">getfullargspec</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<a id="rest_code_f4b49082358149cf861286d903952272-3" name="rest_code_f4b49082358149cf861286d903952272-3" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_f4b49082358149cf861286d903952272-3"></a><span class="go">FullArgSpec(args=[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], varargs=None, varkw=None, defaults=(1, 2), kwonlyargs=[&#39;d&#39;], kwonlydefaults={&#39;d&#39;: 3}, annotations={})</span>
</pre></div>
<p>A function signature has the following syntax:</p>
<ol class="arabic simple">
<li><p>Any positional (non-optional) arguments</p></li>
<li><p>Variable positional arguments (<code class="docutils literal">*x</code>, name stored in <code class="docutils literal">varargs</code>)</p></li>
<li><p>Arguments with defaults (keyword-maybe arguments); their value is stored in <code class="docutils literal">__defaults__</code> left-to-right</p></li>
<li><p>Keyword-only arguments (after an asterisk); their values are stored in a dictionary.  Cannot be used if <code class="docutils literal">varargs</code> are defined.</p></li>
<li><p>Variable keyword arguments (<code class="docutils literal">**y</code>, name stored in <code class="docutils literal">varkw</code>)</p></li>
</ol>
<p>We’re going to make one assumption: we aren’t going to support a <code class="docutils literal">source</code>
function that uses variable arguments of any kind.  So, our final signature
will be composed like this:</p>
<ol class="arabic simple">
<li><p><code class="docutils literal">dest</code> positional arguments</p></li>
<li><p><code class="docutils literal">source</code> positional arguments</p></li>
<li><p><code class="docutils literal">dest</code> keyword-maybe arguments</p></li>
<li><p><code class="docutils literal">source</code> keyword-maybe arguments</p></li>
<li><p><code class="docutils literal">dest</code> keyword-only arguments</p></li>
<li><p><code class="docutils literal">source</code> keyword-only arguments</p></li>
</ol>
<p>That will be saved into <code class="docutils literal">co_names</code>.  The first two arguments are counts —
the first one is <code class="docutils literal">len(1+2+3+4)</code> and the other is <code class="docutils literal">len(5+6)</code>. The remaining
arguments to <code class="docutils literal">CodeType</code> will be either safe minimal defaults, or things taken from
one of the three functions.</p>
<p>We’ll also need to do one more thing: we must ensure <code class="docutils literal">__defaults__</code>,
<code class="docutils literal">__kwdefaults__</code>, and <code class="docutils literal">__annotations__</code> are all in the right places.
That’s also a fairly simple thing to do (it requires more tuple/dict merging).
And with that, we’re done.</p>
</section>
</section>
<section id="final-results">
<h1>Final results</h1>
<p>Before I show you the code, let’s test it out:</p>
<div class="code"><pre class="code python"><a id="rest_code_0f4fd7ac3e33455b8abe64839a219429-1" name="rest_code_0f4fd7ac3e33455b8abe64839a219429-1" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_0f4fd7ac3e33455b8abe64839a219429-1"></a><span class="c1"># old defined as before</span>
<a id="rest_code_0f4fd7ac3e33455b8abe64839a219429-2" name="rest_code_0f4fd7ac3e33455b8abe64839a219429-2" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_0f4fd7ac3e33455b8abe64839a219429-2"></a><span class="nd">@merge_args</span><span class="p">(</span><span class="n">old</span><span class="p">)</span>
<a id="rest_code_0f4fd7ac3e33455b8abe64839a219429-3" name="rest_code_0f4fd7ac3e33455b8abe64839a219429-3" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_0f4fd7ac3e33455b8abe64839a219429-3"></a><span class="k">def</span><span class="w"> </span><span class="nf">new</span><span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="n">foo</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<a id="rest_code_0f4fd7ac3e33455b8abe64839a219429-4" name="rest_code_0f4fd7ac3e33455b8abe64839a219429-4" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_0f4fd7ac3e33455b8abe64839a219429-4"></a>    <span class="k">return</span> <span class="n">old</span><span class="p">(</span><span class="n">prefix</span> <span class="o">+</span> <span class="n">foo</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
<p>And the end result — <code class="docutils literal">help(new)</code> says:</p>
<div class="code"><pre class="code text"><a id="rest_code_1088cc0d6c3d483fb32f8946fd791655-1" name="rest_code_1088cc0d6c3d483fb32f8946fd791655-1" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_1088cc0d6c3d483fb32f8946fd791655-1"></a>new(prefix, foo, bar)
<a id="rest_code_1088cc0d6c3d483fb32f8946fd791655-2" name="rest_code_1088cc0d6c3d483fb32f8946fd791655-2" href="https://chriswarrick.com/blog/2018/09/20/python-hackery-merging-signatures-of-two-python-functions/#rest_code_1088cc0d6c3d483fb32f8946fd791655-2"></a>    This is old&#39;s docstring.
</pre></div>
<p>We did it!</p>
<p class="lead">The code is available on <a class="reference external" href="https://github.com/Kwpolska/merge_args">GitHub</a> and on <a class="reference external" href="https://pypi.org/project/merge-args/">PyPI</a> (<code class="docutils literal">pip install merge_args</code>).
There’s also an extensive test suite.</p>
<p>PS. you might be interested in another related post of mine, in which I
reverse-engineer the compilation of a function: <a class="reference external" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/">Gynvael’s Mission 11 (en): Python bytecode reverse-engineering</a></p>
</section>
]]></content:encoded><category>Python</category><category>hacking</category><category>Python</category><category>Python hackery</category><category>Python internals</category></item><item><title>Python Virtual Environments in Five Minutes</title><dc:creator>Chris Warrick</dc:creator><link>https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/</link><pubDate>Tue, 04 Sep 2018 18:15:00 GMT</pubDate><guid>https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/</guid><description>
In Python, virtual environments are used to isolate projects from each other
(if they require different versions of the same library, for example). They let
you install and manage packages without administrative privileges, and without
conflicting with the system package manager.  They also allow to quickly create
an environment somewhere else with the same dependencies.
Virtual environments are a crucial tool for any Python developer. And at that,
a very simple tool to work with.
</description><content:encoded><![CDATA[
<p>In Python, virtual environments are used to isolate projects from each other
(if they require different versions of the same library, for example). They let
you install and manage packages without administrative privileges, and without
conflicting with the system package manager.  They also allow to quickly create
an environment somewhere else with the same dependencies.</p>
<p>Virtual environments are a crucial tool for any Python developer. And at that,
a very simple tool to work with.</p>



<p>Let’s get started!</p>
<section id="install">
<h1>Install</h1>
<p>The best tool that can be used to create virtual environments is the
<a class="reference external" href="https://docs.python.org/3/library/venv.html">venv</a> module, which is part of
the standard library since Python 3.3.</p>
<p><code class="docutils literal">venv</code> is built into Python, and most users don’t need to install anything.
However, Debian/Ubuntu users will need to run <code class="docutils literal">sudo <span class="pre">apt-get</span> install <span class="pre">python3-venv</span></code> to make it work (due to Debian not installing some components
that <code class="docutils literal">venv</code> needs by default). <a class="brackets" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#footnote-1" id="footnote-reference-1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p>
<p>The alternative (and original, and previously standard) virtual environment tool is <a class="reference external" href="https://virtualenv.pypa.io/">virtualenv</a>. It works with Python 2.7, and has a couple
extra fetures (that you generally won’t need). virtualenv can be installed with your system package manager, or <code class="docutils literal">pip install <span class="pre">--user</span> virtualenv</code>.</p>
<p>Which one to use? Probably <code class="docutils literal">venv</code>. Both tools achieve the same goal in similar
ways. And if one of them does not work, you can try the other and it might just
work better.</p>
<p><em>(Terminology note: most of the time, the names of both tools are used
interchargeably, “venv” was often used as an abbreviation for “virtualenv”
before the stdlib tool was created)</em></p>
</section>
<section id="create">
<h1>Create</h1>
<p>To create a virtual environment named <code class="docutils literal">env</code>, you need to run the <code class="docutils literal">venv</code>
tool with the Python you want to use in that environment.</p>
<div class="code"><pre class="code text"><a id="rest_code_1fb767790a68420a97f0697df45d2e92-1" name="rest_code_1fb767790a68420a97f0697df45d2e92-1" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_1fb767790a68420a97f0697df45d2e92-1"></a>Linux:   $ python3 -m venv env
<a id="rest_code_1fb767790a68420a97f0697df45d2e92-2" name="rest_code_1fb767790a68420a97f0697df45d2e92-2" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_1fb767790a68420a97f0697df45d2e92-2"></a>Windows: &gt; py -m venv env
</pre></div>
<p>or, if you’re using <code class="docutils literal">virtualenv</code>:</p>
<div class="code"><pre class="code text"><a id="rest_code_d2d40ad9883b450e9264cf578d6f68af-1" name="rest_code_d2d40ad9883b450e9264cf578d6f68af-1" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_d2d40ad9883b450e9264cf578d6f68af-1"></a>$ python3 -m virtualenv env
<a id="rest_code_d2d40ad9883b450e9264cf578d6f68af-2" name="rest_code_d2d40ad9883b450e9264cf578d6f68af-2" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_d2d40ad9883b450e9264cf578d6f68af-2"></a>&gt; py -m virtualenv env
</pre></div>
<p>Afterwards, you will end up with a folder named <code class="docutils literal">env</code> that contains folders
named <code class="docutils literal">bin</code> (<code class="docutils literal">Scripts</code> on Windows — contains executables and scripts
installed by packages, including
<code class="docutils literal">python</code>), <code class="docutils literal">lib</code> (contains code), and <code class="docutils literal">include</code> (contains C headers).</p>
<p>Both tools install <code class="docutils literal">pip</code> and <code class="docutils literal">setuptools</code>, but <code class="docutils literal">venv</code> does not ship with
<code class="docutils literal">wheel</code>. In addition, the default versions tend to be more-or-less outdated.
Let’s upgrade them real quick: <a class="brackets" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#footnote-2" id="footnote-reference-2" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a></p>
<div class="code"><pre class="code text"><a id="rest_code_39b9e0a93e0746fbac8d7b7f11734578-1" name="rest_code_39b9e0a93e0746fbac8d7b7f11734578-1" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_39b9e0a93e0746fbac8d7b7f11734578-1"></a>$ env/bin/python -m pip install --upgrade pip setuptools wheel
<a id="rest_code_39b9e0a93e0746fbac8d7b7f11734578-2" name="rest_code_39b9e0a93e0746fbac8d7b7f11734578-2" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_39b9e0a93e0746fbac8d7b7f11734578-2"></a>&gt; env\Scripts\python -m pip install --upgrade pip setuptools wheel
</pre></div>
<section id="where-to-store-virtual-environments">
<h2>Where to store virtual environments?</h2>
<p>While the tools allow you to put your virtual environments anywhere in the
system, it is not a desirable thing to do. There are two options:</p>
<ol class="arabic simple">
<li><p>Have one global place for them, like <code class="docutils literal">~/virtualenvs</code>.</p></li>
<li><p>Store them in each project’s directory, like <code class="docutils literal"><span class="pre">~/git/foobar/.venv</span></code>.</p></li>
</ol>
<p>The first option can be easier to manage, there are tools that can help manage
those (eg. <code class="docutils literal">virtualenvwrapper</code>, shell auto-activation scripts, or the
<code class="docutils literal">workon</code> functions described below).  The second option is equally easy to
work with, but comes with one caveat — you must add the venv directory to your
<code class="docutils literal">.gitignore</code> file (or <code class="docutils literal">.git/info/exclude</code> if you don’t want to commit
changes to <code class="docutils literal">.gitignore</code>), since you don’t want it in your repository (it’s
binary bloat, and works only on your machine).</p>
<p>If you pick the global virtual environment store option, you can use the following short
function (put it in <code class="docutils literal">.bashrc</code> / <code class="docutils literal">.zshrc</code> / your shell configuration file)
to get a simple way to activate an environment (by running <code class="docutils literal">workon foo</code>).
<code class="docutils literal">virtualenvwrapper</code> also has a <code class="docutils literal">workon</code> feature, although I don’t think
<code class="docutils literal">virtualenvwrapper</code> is really necessary and too helpful — the <code class="docutils literal">workon</code>
feature is handy though, and so here’s a way to do it without
<code class="docutils literal">virtualenvwrapper</code>:</p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-1"><code data-line-number="1"></code></a></td><td class="code"><code><a id="rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-1" name="rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-1"></a><span class="w"> </span><span class="nb">export</span><span class="w"> </span><span class="nv">WORKON_HOME</span><span class="o">=</span>~/virtualenvs
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-2"><code data-line-number="2"></code></a></td><td class="code"><code><a id="rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-2" name="rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-2"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-3"><code data-line-number="3"></code></a></td><td class="code"><code><a id="rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-3" name="rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-3"></a><span class="w"> </span><span class="k">function</span><span class="w"> </span>workon<span class="w"> </span><span class="o">{</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-4"><code data-line-number="4"></code></a></td><td class="code"><code><a id="rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-4" name="rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-4"></a><span class="w">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="nb">source</span><span class="w"> </span><span class="s2">&quot;</span><span class="nv">$WORKON_HOME</span><span class="s2">/</span><span class="nv">$1</span><span class="s2">/bin/activate&quot;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-5"><code data-line-number="5"></code></a></td><td class="code"><code><a id="rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-5" name="rest_code_d4c94a5f476e4e5ab3fe0a7682a1bdb7-5"></a><span class="w"> </span><span class="o">}</span>
</code></td></tr></table></div><p>And for PowerShell fans, here’s a <code class="docutils literal">workon.ps1</code> script:</p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_33d18c86da274b70953d97d327e4e0e7-1"><code data-line-number="1"></code></a></td><td class="code"><code><a id="rest_code_33d18c86da274b70953d97d327e4e0e7-1" name="rest_code_33d18c86da274b70953d97d327e4e0e7-1"></a> <span class="nv">$WORKON_HOME</span> <span class="p">=</span> <span class="s2">&quot;$home\virtualenvs&quot;</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_33d18c86da274b70953d97d327e4e0e7-2"><code data-line-number="2"></code></a></td><td class="code"><code><a id="rest_code_33d18c86da274b70953d97d327e4e0e7-2" name="rest_code_33d18c86da274b70953d97d327e4e0e7-2"></a> <span class="p">&amp;</span> <span class="s2">&quot;$WORKON_HOME\</span><span class="p">$(</span><span class="nv">$args</span><span class="p">[</span><span class="n">0</span><span class="p">])</span><span class="s2">\Scripts\activate.ps1&quot;</span>
</code></td></tr></table></div><p>And for cmd.exe fans… you should switch to PowerShell, it’s a very nice and
friendly shell (though perhaps requiring some effort to learn how to be
productive with it).</p>
</section>
</section>
<section id="use">
<h1>Use</h1>
<p>There are three ways of working with virtual environments interactively (in a
shell):</p>
<ul class="simple">
<li><p>activation (run <code class="docutils literal">source env/bin/activate</code> on *nix;
<code class="docutils literal">env\Scripts\activate</code> on Windows) — it simplifies work and requires less
typing, although it can sometimes fail to work properly. (After installing
scripts, <code class="docutils literal">hash <span class="pre">-r</span></code> may be necessary on *nix to use them.)</p></li>
<li><p>executing <code class="docutils literal">env/bin/python</code> (<code class="docutils literal">env\Scripts\python</code>) and other scripts directly, as
activation only changes <code class="docutils literal">$PATH</code> and some helper variables — those variables
are not mandatory for operation, running the correct <code class="docutils literal">python</code> is, and that
method is failsafe.</p></li>
<li><p><a class="reference external" href="https://gist.github.com/datagrok/2199506">in subshells</a> (IMO, it’s bad UX)</p></li>
</ul>
<p>Whichever method you use, you must remember that without doing any of these
things, you will still be working with the system Python.</p>
<p>For non-interactive work (eg. crontab entries, system services, etc.),
activation and subshells are not viable solutions. In these cases, you must
always use the full path to Python.</p>
<p>Here are some usage examples (paths can be relative, of course):</p>
<div class="code"><pre class="code text"><a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-1" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-1" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-1"></a>## *nix, activation ##
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-2" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-2" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-2"></a>$ source /path/to/env/bin/activate
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-3" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-3" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-3"></a>(env)$ pip install Django
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-4" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-4" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-4"></a>(env)$ deactivate
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-5" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-5" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-5"></a>
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-6" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-6" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-6"></a>## *nix, manual execution ##
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-7" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-7" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-7"></a>$ /path/to/env/bin/pip install Django
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-8" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-8" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-8"></a>
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-9" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-9" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-9"></a>## Windows, activation ##
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-10" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-10" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-10"></a>&gt; C:\path\to\env\Scripts\activate
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-11" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-11" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-11"></a>(venv)&gt; pip install Django
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-12" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-12" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-12"></a>(venv)&gt; deactivate
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-13" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-13" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-13"></a>
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-14" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-14" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-14"></a>## Windows, manual execution ##
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-15" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-15" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-15"></a>&gt; C:\path\to\env\Scripts\pip install Django
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-16" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-16" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-16"></a>
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-17" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-17" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-17"></a>## Windows, updating pip/setuptools/wheel ##
<a id="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-18" name="rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-18" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_99f32ee1312e43b19d00feb1c5e6ae7f-18"></a>&gt; C:\path\to\env\Scripts\python -m pip install -U pip setuptools wheel
</pre></div>
<p>The same principle applies to running Python itself, or any other script
installed by a package. (With Django’s <code class="docutils literal">manage.py</code>, calling it as
<code class="docutils literal">./manage.py</code> requires activation, or you can run
<code class="docutils literal">venv/bin/python manage.py</code>.)</p>
<section id="moving-renaming-copying-environments">
<h2>Moving/renaming/copying environments?</h2>
<p>If you try to copy or rename a virtual environment, you will discover that the
copied environment does not work. This is because a virtual environment is
closely tied to both the Python it was created with, and the location it was
created in. (The “relocatable” option of <code class="docutils literal">virtualenv</code> does not work and is deprecated.) <a class="brackets" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#footnote-3" id="footnote-reference-3" role="doc-noteref"><span class="fn-bracket">[</span>3<span class="fn-bracket">]</span></a></p>
<p>However, this is very easy to fix. Instead of moving/copying, just create a new
environment in the new location. Then, run <code class="docutils literal">pip freeze &gt; requirements.txt</code> in
the old environment to create a list of packages installed in it. With that,
you can just run <code class="docutils literal">pip install <span class="pre">-r</span> requirements.txt</code> in the new environment to
install packages from the saved list. (Of course, you can copy <code class="docutils literal">requirements.txt</code>
between machines. In many cases, it will just work; sometimes, you might need a few
modifications to <code class="docutils literal">requirements.txt</code> to remove OS-specific stuff.)</p>
<div class="code"><pre class="code text"><a id="rest_code_118b2d54954242548dd7d3e590824a4c-1" name="rest_code_118b2d54954242548dd7d3e590824a4c-1" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_118b2d54954242548dd7d3e590824a4c-1"></a>$ oldenv/bin/pip freeze &gt; requirements.txt
<a id="rest_code_118b2d54954242548dd7d3e590824a4c-2" name="rest_code_118b2d54954242548dd7d3e590824a4c-2" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_118b2d54954242548dd7d3e590824a4c-2"></a>$ python3 -m venv newenv
<a id="rest_code_118b2d54954242548dd7d3e590824a4c-3" name="rest_code_118b2d54954242548dd7d3e590824a4c-3" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_118b2d54954242548dd7d3e590824a4c-3"></a>$ newenv/bin/pip install -r requirements.txt
<a id="rest_code_118b2d54954242548dd7d3e590824a4c-4" name="rest_code_118b2d54954242548dd7d3e590824a4c-4" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#rest_code_118b2d54954242548dd7d3e590824a4c-4"></a>(You may rm -rf oldenv now if you desire)
</pre></div>
<p>Note that it might also be necessary to re-create your virtual environment
after a Python upgrade, <a class="brackets" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#footnote-4" id="footnote-reference-4" role="doc-noteref"><span class="fn-bracket">[</span>4<span class="fn-bracket">]</span></a> so it might be handy to keep an up-to-date
<code class="docutils literal">requirements.txt</code> for your virtual environments (for many projects, it makes
sense to put that in the repository).</p>
<p>To manage those <code class="docutils literal">requirements.txt</code> files in a more orgnized yet still simple
way, you might be interested in <a class="reference external" href="https://github.com/jazzband/pip-tools">pip-tools</a>.</p>
</section>
</section>
<section id="frequently-asked-questions">
<h1>Frequently Asked Questions</h1>
<section id="im-using-virtualenv-do-i-need-to-install-it-for-each-python-i-want-to-use-it-with">
<h2>I’m using virtualenv. Do I need to install it for each Python I want to use it with?</h2>
<p>In most cases, you can use <code class="docutils literal">virtualenv <span class="pre">-p</span> pythonX env</code> to specify a different
Python version, but with some Python version combinations, that approach might
be unsuccessful. (The <code class="docutils literal">venv</code> module is tied to the Python version it’s
installed in.)</p>
</section>
<section id="im-the-only-user-on-my-system-do-i-still-need-virtual-environments">
<h2>I’m the only user on my system. Do I still need virtual environments?</h2>
<p>Yes, you do. First, you will still need separation between projects, sooner or
later.  Moreover, if you were to install packages system-wide with pip, you
might end up causing conflicts between packages installed by the system package
manager and by pip. Running <code class="docutils literal">sudo pip</code> is never a good idea because of this.</p>
</section>
<section id="im-using-docker-do-i-still-need-virtual-environments">
<h2>I’m using Docker. Do I still need virtual environments?</h2>
<p>They are still a good idea in that case. They protect you against any bad
system-wide Python packages your OS image might have (and one popular base OS
is famous for those). They don’t introduce any extra overhead, while allowing
to have a clean environment and the ability to re-create it outside of Docker
(eg. for local development without Docker)</p>
</section>
<section id="what-about-pipenv">
<h2>What about Pipenv?</h2>
<p>Pipenv is a dependency management tool. It isn’t compatible with most workflows, and comes with many issues. In my opinion, it’s not worth using (Also, that thing about it being an officially recommended tool? Turns out it’s not true.)</p>
<p>I also wrote a blog post detailing concerns with that tool, titled <a class="reference external" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/">Pipenv: promises a lot, delivers very little</a>.</p>
<p>Consider using <a class="reference external" href="https://github.com/jazzband/pip-tools">pip-tools</a> instead.</p>
</section>
</section>
<section id="footnotes">
<h1>Footnotes</h1>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="footnote-1" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#footnote-reference-1">1</a><span class="fn-bracket">]</span></span>
<p>The thing you’re actually installing is <code class="docutils literal">ensurepip</code>. In general, Debian isn’t exactly friendly with Python packaging.</p>
</aside>
<aside class="footnote brackets" id="footnote-2" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#footnote-reference-2">2</a><span class="fn-bracket">]</span></span>
<p>On Windows, you <em>must</em> run <code class="docutils literal">python <span class="pre">-m</span> pip</code> instead of <code class="docutils literal">pip</code> if you want to upgrade the package manager itself.</p>
</aside>
<aside class="footnote brackets" id="footnote-3" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#footnote-reference-3">3</a><span class="fn-bracket">]</span></span>
<p>All script shebangs contain the direct path to the environment’s Python executable.  Many things in the virtual environment are symlinks that point to the original Python.</p>
</aside>
<aside class="footnote brackets" id="footnote-4" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/#footnote-reference-4">4</a><span class="fn-bracket">]</span></span>
<p>Definitely after a minor version (3.x → 3.y) upgrade, sometimes (I’m looking at you Homebrew) after a patch version upgrade (3.x.y → 3.x.z) as well.</p>
</aside>
</aside>
</section>
]]></content:encoded><category>Python</category><category>best practices</category><category>devel</category><category>guide</category><category>Python</category><category>venv</category><category>virtual environments</category><category>virtualenv</category></item><item><title>Pipenv: promises a lot, delivers very little</title><dc:creator>Chris Warrick</dc:creator><link>https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/</link><pubDate>Tue, 17 Jul 2018 17:40:00 GMT</pubDate><guid>https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/</guid><description>
Pipenv is a Python packaging tool that does one thing reasonably well — application dependency management. However, it is also plagued by issues, limitations and a break-neck development process. In the past, Pipenv’s promotional material was highly misleading as to its purpose and backers.
In this post, I will explore the problems with Pipenv. Was it really
recommended by Python.org? Can everyone — or at least, the vast majority
of people — benefit from it?
(This post has been updated in February 2020 and May 2020 to reflect the
current state of Pipenv.)
</description><content:encoded><![CDATA[
<p>Pipenv is a Python packaging tool that does one thing reasonably well — application dependency management. However, it is also plagued by issues, limitations and a break-neck development process. In the past, Pipenv’s promotional material was highly misleading as to its purpose and backers.</p>
<p>In this post, I will explore the problems with Pipenv. Was it really
recommended by Python.org? Can everyone — or at least, the vast majority
of people — benefit from it?</p>
<p>(This post has been updated in February 2020 and May 2020 to reflect the
current state of Pipenv.)</p>



<section id="a-2020-update-updated">
<h1>A 2020 update (updated)</h1>
<p>This blog post was written in 2018, and it’s still pretty accurate when it
comes to the criticisms of Pipenv, but something else happened since then.</p>
<p class="lead">No release was made between November 2018 and May 2020. Pipenv was effectively
dead for 1.5 years, and the state of Pipenv maintenance is alarming.</p>
<p>A release of Pipenv was made in late 2018 (aptly named v2018.11.26). But then,
there was silence. New commits were made (on the order of 600-700 by the end of
the year). People asked for new releases, in more or less strong words, <a class="reference external" href="https://github.com/pypa/pipenv/issues/3742">in May
2019</a>, then <a class="reference external" href="https://github.com/pypa/pipenv/issues/3978">in October</a>, and again <a class="reference external" href="https://github.com/pypa/pipenv/issues/4058">in December</a>. Many people — including yours
truly, in this post — considered Pipenv dead. On 13th December 2019, the current
maintainer claimed <a class="reference external" href="https://github.com/pypa/pipenv/issues/4058#issuecomment-565550646">a new release is almost finished</a>.</p>
<p>Pipenv 2019/2020 was vaporware for five months. Not much progress was made since the
December post until March 2020, when an issue from 2018 was renamed the <a class="reference external" href="https://github.com/pypa/pipenv/issues/3369">March 2020
Release Tracking Issue</a>. Some
progress was happening, and many release dates were given, but delays stacked
up. March became April. The first beta release was promised by 21st April, it
was delayed until the 29th. The final release was scheduled for a week from
that, but it didn’t happen. Finally, Beta 2 came out on 20th May 2020, and the
final release landed as v2020.5.28.</p>
<p>If you read further into the post, you’ll encounter a chapter titled <a class="reference internal" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#the-break-neck-pace-of-pipenv">The
break-neck pace of Pipenv</a>. Am I being a hypocrite right now? No, not at all.
Projects that are being depended on, such as a package manager, should have
clear policies about how they’re maintained. A new release when the maintainer
feels like adding a feature is too often. A new release every 1.5 years is not
often enough. And silence from maintainers, when faced with questions about
releases, is simply unacceptable.  Pip, for example, has updates every few
months in a fairly stable pace (with the exception of emergency bugfix
releases), and pip has years of development behind it, unlike the fairly new
Pipenv.</p>
<p>And even if the May release was successful, you can’t be sure about any future
releases, and what will happen with Pipenv. At the same time, Pipenv isn’t a
good tool, as this post tries to explain — those criticisms are still valid,
since they are at the core of what Pipenv is. Instead, perhaps consider using
<strong>pip-tools</strong> for locking dependencies? It does one thing, and one thing well.
It doesn’t enforce any specific structures on users, and supports any workflow
you have. (If you don’t need to lock dependencies, <strong>pip + venv</strong> will
suffice.)</p>
</section>
<section id="officially-recommended-tool-or-how-we-got-here">
<h1>“Officially recommended tool”, or how we got here</h1>
<blockquote>
<p>“Pipenv — the officially recommended Python packaging tool from Python.org, free (as in freedom).”</p>
</blockquote>
<p>Pipenv’s README used to have a version of the above line in their README for
many months: it was added on  <a class="reference external" href="https://github.com/pypa/pipenv/commit/6e06fc451767a57e6fccb828c74a1412f6cef687">2017-08-31</a> and eventually disappeared on <a class="reference external" href="https://github.com/pypa/pipenv/commit/47debed9a1c2a3649bef4d59a3f1cf01bf059522">2018-05-19</a>. For a short while (2018-05-16), it was clarified (<em>managing application dependencies</em>, and <em>PyPA</em> instead of <em>Python.org</em>), and for about 15 minutes, the tagline called Pipenv <em>the world’s worst</em> or <a class="reference external" href="https://github.com/pypa/pipenv/commit/6d77e4a0551528d5d72d81e8a15da4722ad82f26">something</a> <a class="reference external" href="https://github.com/pypa/pipenv/commit/1c956d37e6ad20babdb5021610b2ed2c9c4203f2">along</a> <a class="reference external" href="https://github.com/pypa/pipenv/commit/e3c72e167d21b921bd3bd89d4217b04628919bb2">these</a> <a class="reference external" href="https://github.com/pypa/pipenv/commit/fe78628903948013e8687d1a3be9fd4da2b6bd3d">lines</a> (this coming from the maintainer).</p>
<p>The README tagline claimed that Pipenv is the be-all, end-all of Python
packaging. The problem is: it isn’t that. There are some use cases that benefit
from Pipenv, but for many others, trying to use that tool will only lead to
frustration. We will explore this issue later.</p>
<p>Another issue with this tagline was the <em>Python.org</em> and <em>official</em> parts. The
thing that made it “official” was a <a class="reference external" href="https://packaging.python.org/tutorials/managing-dependencies/">short tutorial</a> <a class="brackets" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#footnote-1" id="footnote-reference-1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> on packaging.python.org,
which is the PyPA’s packaging user guide. Also of note is the <em>Python.org</em>
domain used. It makes it sound as if Pipenv was endorsed by the Python core
team. PyPA (Python Packaging Authority) is a separate organization — they are
responsible for the packaging parts (including pypi.org, setuptools, pip,
wheel, virtualenv, etc.) of Python. This made the endorsement misleading. Of
course, PyPA is a valued part of the Python world; an endorsement by the core
team — say, <a class="reference external" href="https://docs.python.org/3/library/ensurepip.html">inclusion in official Python distributions</a> — is something far more
important.</p>
<p>This tagline has led to many discussions and flamewars, perhaps with <a class="reference external" href="https://np.reddit.com/r/Python/comments/8jd6aq/why_is_pipenv_the_recommended_packaging_tool_by/">this
Reddit thread from May</a> being the most heated and most important. The change
was the direct result of this Reddit thread. I recommend reading this thread in
full.</p>
</section>
<section id="what-pipenv-does">
<h1>What pipenv does</h1>
<p>We’ve already learned that Pipenv is used to <em>manage application dependencies</em>.
Let’s learn what that term really means.</p>
<section id="application-dependencies">
<h2>Application dependencies</h2>
<p>Here is an example use case for Pipenv:
I’m working on a website based on Django.  I create <code class="docutils literal">~/git/website</code> and run
<code class="docutils literal">pipenv install Django</code> in that directory.  Pipenv:</p>
<ul class="simple">
<li><p>automatically creates a virtualenv somewhere in my home directory</p></li>
<li><p>writes a Pipfile, which lists Django as my dependency</p></li>
<li><p>installs Django using pip</p></li>
<li><p>proceeds to write <code class="docutils literal">Pipfile.lock</code>, which stores the exact version and source file hash <a class="brackets" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#footnote-2" id="footnote-reference-2" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a> of each package installed (including <code class="docutils literal">pytz</code>, Django’s dependency).</p></li>
</ul>
<p>The last part of the process was the most time consuming. At one point, while
locking the dependency versions, Pipenv hangs for 46 seconds. This is one of
Pipenv’s notable issues: <strong>it’s slow.</strong> Of course, this isn’t the only one,
but it defintely doesn’t help. Losing 46 seconds isn’t much, but when we get to
the longer waits in the timing test section later, we’ll see something that
could easily discourage users from using a package.</p>
</section>
<section id="running-scripts-badly">
<h2>Running scripts (badly)</h2>
<p>But let’s continue with our workflow. <code class="docutils literal">pipenv run <span class="pre">django-admin</span> startproject
foobanizer</code> is what I must use now, which is rather unwieldy to type, and
requires running pipenv even for the smallest things. (The <code class="docutils literal">manage.py</code> script
has <code class="docutils literal">/usr/bin/env python</code> in its shebang.) I can run <code class="docutils literal">pipenv shell</code> to get
a new shell which runs the <code class="docutils literal">activate</code> script by default, giving you the worst
of both worlds when it comes to virtualenv activation: the unwieldiness of a
new shell, and the activate script, which the proponents of the shell spawning
dislike.</p>
<p>Using <code class="docutils literal">pipenv shell</code> means spawning a new subshell, executing the shell
startup scripts (eg. <code class="docutils literal">.bashrc</code>), and requiring you to exit with <code class="docutils literal">exit</code> or
^D. If you type <code class="docutils literal">deactivate</code>, you are working with an extra shell, but now
outside of the virtualenv. Or you can use the <code class="docutils literal"><span class="pre">--fancy</span></code> mode that manipulates
<code class="docutils literal">$PATH</code> before launching the subshell, but it requires a specific shell
configuration, in which <code class="docutils literal">$PATH</code> is not overridden in non-login shells — and
also often changing the config of your terminal emulator to run a login shell,
as many of the Linux terminals don’t do it.</p>
<p>Now, why does all this happen? Because a command cannot manipulate the
environment of the shell it spawns. This means that Pipenv must pretend what it
does is a reasonable thing instead of a workaround. This can be solved with
manual activation using <code class="docutils literal">source $(pipenv <span class="pre">--venv)/bin/activate</span></code> (can be made
into a neat alias), or shell wrappers (similar to what virtualenvwrapper does).</p>
</section>
<section id="finishing-it-all-up">
<h2>Finishing it all up</h2>
<p>Anyway, I want a blog on my site. I want to write them in Markdown syntax, so I
run <code class="docutils literal">pipenv install Markdown</code>, and a few long seconds later, it’s added to
both Pipfiles.  Another thing I can do is <code class="docutils literal">pipenv install <span class="pre">--dev</span> ipython</code> and
get a handy shell for tinkering, but it will be marked as a development
dependency — so, not installed in production. That last part is an important
advantage of using Pipenv.</p>
<p>When I’m done working on my website, I commit both Pipfiles to my git
repository, and push it to the remote server. Then I can clone it to, say,
<code class="docutils literal">/srv/website</code>. Now I can just <code class="docutils literal">pipenv install</code> to get all the production
packages installed (but not the development ones — Django, pytz, Markdown will
be installed, but IPython and all its million dependencies won’t). There’s just
one caveat: by default, the virtualenv will still be created in the current
user’s home directory. This is a problem in this case, since it needs to be
accessible by <a class="reference external" href="https://chriswarrick.com/blog/2016/02/10/deploying-python-web-apps-with-nginx-and-uwsgi-emperor/">nginx and uWSGI</a>, which do not have access to my (or root’s)
home directory, and don’t have a home directory of their own.  This can be
solved with <code class="docutils literal">export PIPENV_VENV_IN_PROJECT=1</code>. But note that I will now need
to export this environment variable every time I work with the app in <code class="docutils literal">/srv</code>
via Pipenv. The tool supports loading <code class="docutils literal">.env</code> files, <strong>but</strong> only when
running <code class="docutils literal">pipenv shell</code> and <code class="docutils literal">pipenv run</code>. You can’t use it to configure
Pipenv. And to run my app with nginx/uWSGI, I will need to know the exact virtualenv
path anyway, since I can’t use <code class="docutils literal">pipenv run</code> as part of uWSGI configuration.</p>
</section>
</section>
<section id="what-pipenv-doesnt-do">
<h1>What pipenv doesn’t do</h1>
<p>The workflow I mentioned above looks pretty reasonable, right? There are some
deficiencies, but other than that, it seems to work well. The main issue with
Pipenv is: <strong>it works with one workflow, and one workflow only.</strong> Try to do
anything else, and you end up facing multiple obstacles.</p>
<section id="setup-py-source-distributions-and-wheels">
<h2>Setup.py, source distributions, and wheels</h2>
<p>Pipenv only concerns itself with managing dependencies. <strong>It isn’t a packaging
tool.</strong> If you want your thing up on PyPI, Pipenv won’t help you with anything.
You still need to write a <code class="docutils literal">setup.py</code> with <code class="docutils literal">install_requires</code>, because the
Pipfile format only specifies the dependencies and runtime requirements (Python
version), there is no place in it for the package name, and Pipenv does not
mandate/expect you to install your project. It can come in handy to manage the
development environment (as a <code class="docutils literal">requirements.txt</code> replacement, or something
used to write said file), but if your project has a <code class="docutils literal">setup.py</code>, you still
need to manually manage <code class="docutils literal">install_requires</code>. Pipenv can’t create wheels on its
own either. And <code class="docutils literal">pip freeze</code> is going to be a lot faster than Pipenv ever
will be.</p>
</section>
<section id="working-outside-of-the-project-root">
<h2>Working outside of the project root</h2>
<p>Another issue with Pipenv is the use of the working directory to select
the virtual environment. <a class="brackets" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#footnote-3" id="footnote-reference-3" role="doc-noteref"><span class="fn-bracket">[</span>3<span class="fn-bracket">]</span></a> Let’s say I’m a library author.  A user of my <code class="docutils literal">foobar</code>
library has just reported a bug and attached a <code class="docutils literal">repro.py</code> file that lets me
reproduce the issue. I download that file to <code class="docutils literal">~/Downloads</code> on my filesystem.
With plain old virtualenv, I can easily confirm the reproduction in a spare
shell with:</p>
<div class="code"><pre class="code shell"><a id="rest_code_83381fdf79414bf799a8ce2777ae054f-1" name="rest_code_83381fdf79414bf799a8ce2777ae054f-1" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#rest_code_83381fdf79414bf799a8ce2777ae054f-1"></a>$<span class="w"> </span>~/virtualenvs/foobar/bin/python<span class="w"> </span>~/Downloads/repro.py
</pre></div>
<p>And then I can launch my fancy IDE to fix the bug.  I don’t have to <code class="docutils literal">cd</code> into
the project. But with Pipenv, I can’t really do that.  If I put the virtualenv
in <code class="docutils literal">.venv</code> with the command line option, I can type
<code class="docutils literal"><span class="pre">~/git/foobar/.venv/bin/python</span> ~/Downloads/repro.py</code>. If I use the
centralized directory + hashes thing, Tab completion becomes mandatory, if I
haven’t memorized the hash.</p>
<div class="code"><pre class="code shell"><a id="rest_code_227b6f3431ed43459b23802ee8423842-1" name="rest_code_227b6f3431ed43459b23802ee8423842-1" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#rest_code_227b6f3431ed43459b23802ee8423842-1"></a>$<span class="w"> </span><span class="nb">cd</span><span class="w"> </span>~/git/foobar
<a id="rest_code_227b6f3431ed43459b23802ee8423842-2" name="rest_code_227b6f3431ed43459b23802ee8423842-2" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#rest_code_227b6f3431ed43459b23802ee8423842-2"></a>$<span class="w"> </span>pipenv<span class="w"> </span>run<span class="w"> </span>python<span class="w"> </span>~/Downloads/repro.py
</pre></div>
<p>What if I had two <code class="docutils literal">.py</code> files, or <code class="docutils literal">repro.py</code> otherwise depended on being in
the current working directory?</p>
<div class="code"><pre class="code shell"><a id="rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-1" name="rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-1" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-1"></a>$<span class="w"> </span><span class="nb">cd</span><span class="w"> </span>~/git/foobar
<a id="rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-2" name="rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-2" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-2"></a>$<span class="w"> </span>pipenv<span class="w"> </span>shell
<a id="rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-3" name="rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-3" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-3"></a><span class="o">(</span>foobar-Mwd1l2m9<span class="o">)</span>$<span class="w"> </span><span class="nb">cd</span><span class="w"> </span>~/Downloads
<a id="rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-4" name="rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-4" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-4"></a><span class="o">(</span>foobar-Mwd1l2m9<span class="o">)</span>$<span class="w"> </span>python<span class="w"> </span>repro.py
<a id="rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-5" name="rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-5" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#rest_code_6d3e90e2fbf547908eaaa3ef2a0e9646-5"></a><span class="o">(</span>foobar-Mwd1l2m9<span class="o">)</span>$<span class="w"> </span><span class="nb">exit</span><span class="w">  </span><span class="c1"># (not deactivate!)</span>
</pre></div>
<p><strong>This is becoming ugly fairly quickly.</strong> Also, with virtualenvwrapper, I can
do this:</p>
<div class="code"><pre class="code shell"><a id="rest_code_1303a58c0d4e43319a2fc0e5e958e914-1" name="rest_code_1303a58c0d4e43319a2fc0e5e958e914-1" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#rest_code_1303a58c0d4e43319a2fc0e5e958e914-1"></a>$<span class="w"> </span><span class="nb">cd</span><span class="w"> </span>~/Downloads
<a id="rest_code_1303a58c0d4e43319a2fc0e5e958e914-2" name="rest_code_1303a58c0d4e43319a2fc0e5e958e914-2" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#rest_code_1303a58c0d4e43319a2fc0e5e958e914-2"></a>$<span class="w"> </span>workon<span class="w"> </span>foobar
<a id="rest_code_1303a58c0d4e43319a2fc0e5e958e914-3" name="rest_code_1303a58c0d4e43319a2fc0e5e958e914-3" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#rest_code_1303a58c0d4e43319a2fc0e5e958e914-3"></a><span class="o">(</span>foobar<span class="o">)</span>$<span class="w"> </span>python<span class="w"> </span>repro.py
<a id="rest_code_1303a58c0d4e43319a2fc0e5e958e914-4" name="rest_code_1303a58c0d4e43319a2fc0e5e958e914-4" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#rest_code_1303a58c0d4e43319a2fc0e5e958e914-4"></a><span class="o">(</span>foobar<span class="o">)</span>$<span class="w"> </span>deactivate
</pre></div>
<p>And let’s not forget that Pipenv doesn’t help me to write a <code class="docutils literal">setup.py</code>,
distribute code, or manage releases.  It just manages dependencies.  And it
does it pretty badly.</p>
</section>
<section id="nikola">
<h2>Nikola</h2>
<p>I’m a co-maintainer of a static site generator, <a class="reference external" href="https://getnikola.com/">Nikola</a>.  As part of this, I have the following places where
I need to run Nikola:</p>
<ul class="simple">
<li><p><code class="docutils literal">~/git/nikola</code></p></li>
<li><p><code class="docutils literal"><span class="pre">~/git/nikola-site</span></code></p></li>
<li><p><code class="docutils literal"><span class="pre">~/git/nikola-plugins</span></code></p></li>
<li><p><code class="docutils literal"><span class="pre">~/git/nikola-themes</span></code></p></li>
<li><p><code class="docutils literal">~/website</code> (this blog)</p></li>
<li><p><code class="docutils literal">/Volumes/RAMDisk/n</code> (demo site, used for testing and created when needed, on a <a class="reference external" href="https://en.wikipedia.org/wiki/RAM_drive">RAM disk</a>)</p></li>
</ul>
<p>That list is long.  End users of Nikola probably don’t have a list that long,
but they might just have more than one Nikola site.  For me, and for the
aforementioned users, Pipenv does not work.  To use Pipenv, all those
repositories would need to live in one directory. I would also need to have a
<em>separate</em> Pipenv environment for <code class="docutils literal"><span class="pre">nikola-users</span></code>, because that needs Django.
Moreover, the Pipfile would have to be symlinked from <code class="docutils literal">~/git/nikola</code> if we
were to make use of those in the project.  So, I would have a <code class="docutils literal">~/nikola</code>
directory just to make Pipenv happy, do testing/bug reproduction on a SSD (and
wear it out faster), and so on… Well, I could also use the virtualenv directly.
But in that case, Pipenv loses its usefulness, and makes my workflow more
complicated. I can’t use <code class="docutils literal">virtualenvwrapper</code>, because I would need to hack a
fuzzy matching system onto it, or memorize the random string appended to my
virtualenv name.  All because Pipenv relies on the current directory too much.</p>
<p>Nikola end users who want to use Pipenv will also have a specific directory
structure forced on them. What if the site serves as docs for a project, and
lives inside another project’s repo? Two virtualenvs, 100 megabytes wasted.
Or worse, Nikola ends up in the other project’s Pipfile, which is technically
good for our download stats, but not really good for the other project’s
contributors.</p>
</section>
</section>
<section id="the-part-where-i-try-to-measure-times">
<h1>The part where I try to measure times</h1>
<p>Pipenv is famous for being slow.  But how slow is it really?
I put it to the test.  I used two test environments:</p>
<ul class="simple">
<li><p>Remote: a DigitalOcean VPS, the cheapest option (1 vCPU), Python 3.6/Fedora
28, in Frankfurt</p></li>
<li><p>Local: my 2015 13” MacBook Pro (base model), Python 3.7, on a rather slow
Internet connection (10 Mbps on a good day, and the test was not performed on
one of them)</p></li>
</ul>
<p>Both were runninng Pipenv 2018.7.1, installed from pip.</p>
<p>And with the following cache setups:</p>
<ul class="simple">
<li><p>Removed: <code class="docutils literal"><span class="pre">~/.cache/pipenv</span></code> removed</p></li>
<li><p>Partial: <code class="docutils literal">rm <span class="pre">-rf</span> <span class="pre">~/.cache/pipenv/depcache-py*.json</span> <span class="pre">~/.cache/pipenv/hash-cache/</span></code></p></li>
<li><p>Kept: no changes done from previous run</p></li>
</ul>
<p>Well, turns out Pipenv likes doing strange things with caching and locking.  A
look at the Activity Monitor hinted that there is network activity going on
when Pipenv displays its <em>Locking [packages] dependencies...</em> line and
hangs. Now, the docs don’t tell you that. The most atrocious example was a
local Nikola install that was done in two runs: the first <code class="docutils literal">pipenv install
Nikola</code> run was interrupted <a class="brackets" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#footnote-4" id="footnote-reference-4" role="doc-noteref"><span class="fn-bracket">[</span>4<span class="fn-bracket">]</span></a> right after it was done installing packages,
so the cache had all the necessary wheels in it. The install took 10 minutes
and 7 seconds, 9:50 of which were taken by locking dependencies and installing
the locked dependencies — so, roughly nine and a half minutes were spent
staring at a static screen, with the tool doing <em>something</em> in the background —
and Pipenv doesn’t tell you what happens in this phase.</p>
<p>(Updated 2018-07-22: In the pipenv measurements: the first entry is the total
time of pipenv executon. The second is the long wait for pipenv to do its
“main” job: locking dependencies and installing them. The timing starts when
pipenv starts locking dependencies and ends when the prompt appears. The third
item is pipenv’s reported installation time.  So, pipenv install ⊇ locking/installing ⊇ Pipfile.lock install.)</p>
<table class="table table-striped table-bordered">
<thead>
<tr><th class="head" rowspan="2"><p>Task</p></th>
<th class="head" rowspan="2"><p>Action</p></th>
<th class="head" rowspan="2"><p>Measurement
method</p></th>
<th class="head" rowspan="2"><p>Environment</p></th>
<th class="head" rowspan="2"><p>Cache</p></th>
<th class="head" colspan="4"><p>Times in seconds</p></th>
</tr>
<tr><th class="head"><p>Attempt 1</p></th>
<th class="head"><p>Attempt 2</p></th>
<th class="head"><p>Attempt 3</p></th>
<th class="head"><p>Average</p></th>
</tr>
</thead>
<tbody>
<tr><td><p>1</p></td>
<td><p>virtualenv</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td><p>Remote</p></td>
<td><p>(not applicable)</p></td>
<td><p>3.911</p></td>
<td><p>4.052</p></td>
<td><p>3.914</p></td>
<td><p>3.959</p></td>
</tr>
<tr><td><p>2</p></td>
<td><p>pip install Nikola</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td><p>Remote</p></td>
<td><p>Removed</p></td>
<td><p>11.562</p></td>
<td><p>11.943</p></td>
<td><p>11.773</p></td>
<td><p>11.759</p></td>
</tr>
<tr><td><p>3</p></td>
<td><p>pip install Nikola</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td><p>Remote</p></td>
<td><p>Kept</p></td>
<td><p>7.404</p></td>
<td><p>7.681</p></td>
<td><p>7.569</p></td>
<td><p>7.551</p></td>
</tr>
<tr><td rowspan="3"><p>4</p></td>
<td><p>pipenv install Nikola</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td rowspan="3"><p>Remote</p></td>
<td rowspan="3"><p>Removed</p></td>
<td><p>67.536</p></td>
<td><p>62.973</p></td>
<td><p>71.305</p></td>
<td><p>67.271</p></td>
</tr>
<tr><td><p>├─ locking/installing from lockfile</p></td>
<td><p>stopwatch</p></td>
<td><p>42.6</p></td>
<td><p>40.5</p></td>
<td><p>39.6</p></td>
<td><p>40.9</p></td>
</tr>
<tr><td><p>└─ Pipfile.lock install</p></td>
<td><p>pipenv</p></td>
<td><p>14</p></td>
<td><p>14</p></td>
<td><p>13</p></td>
<td><p>13.667</p></td>
</tr>
<tr><td rowspan="3"><p>5</p></td>
<td><p>adding Django to an environment</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td rowspan="3"><p>Remote</p></td>
<td rowspan="3"><p>Kept (only Nikola in cache)</p></td>
<td><p>39.576</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>39.576</p></td>
</tr>
<tr><td><p>├─ locking/installing from lockfile</p></td>
<td><p>stopwatch</p></td>
<td><p>32</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>32</p></td>
</tr>
<tr><td><p>└─ Pipfile.lock install</p></td>
<td><p>pipenv</p></td>
<td><p>14</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>14</p></td>
</tr>
<tr><td rowspan="3"><p>6</p></td>
<td><p>adding Django to another environment</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td rowspan="3"><p>Remote</p></td>
<td rowspan="3"><p>Kept (both in cache)</p></td>
<td><p>37.978</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>37.978</p></td>
</tr>
<tr><td><p>├─ locking/installing from lockfile</p></td>
<td><p>stopwatch</p></td>
<td><p>30.2</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>30.2</p></td>
</tr>
<tr><td><p>└─ Pipfile.lock install</p></td>
<td><p>pipenv</p></td>
<td><p>14</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>14</p></td>
</tr>
<tr><td rowspan="3"><p>7</p></td>
<td><p>pipenv install Django</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td rowspan="3"><p>Remote</p></td>
<td rowspan="3"><p>Removed</p></td>
<td><p>20.612</p></td>
<td><p>20.666</p></td>
<td><p>20.665</p></td>
<td><p>20.648</p></td>
</tr>
<tr><td><p>├─ locking/installing from lockfile</p></td>
<td><p>stopwatch</p></td>
<td><p>6.6</p></td>
<td><p>6.4</p></td>
<td><p>6</p></td>
<td><p>6.333</p></td>
</tr>
<tr><td><p>└─ Pipfile.lock install</p></td>
<td><p>pipenv</p></td>
<td><p>1</p></td>
<td><p>1</p></td>
<td><p>1</p></td>
<td><p>1</p></td>
</tr>
<tr><td rowspan="3"><p>8</p></td>
<td><p>pipenv install Django (new env)</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td rowspan="3"><p>Remote</p></td>
<td rowspan="3"><p>Kept</p></td>
<td><p>17.615</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>17.615</p></td>
</tr>
<tr><td><p>├─ locking/installing from lockfile</p></td>
<td><p>stopwatch</p></td>
<td><p>3.5</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>3.5</p></td>
</tr>
<tr><td><p>└─ Pipfile.lock install</p></td>
<td><p>pipenv</p></td>
<td><p>1</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>1</p></td>
</tr>
<tr><td rowspan="3"><p>9</p></td>
<td><p>pipenv install Nikola</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td rowspan="3"><p>Remote</p></td>
<td rowspan="3"><p>Partial</p></td>
<td><p>61.507</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>61.507</p></td>
</tr>
<tr><td><p>├─ locking/installing from lockfile</p></td>
<td><p>stopwatch</p></td>
<td><p>38.40</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>38.40</p></td>
</tr>
<tr><td><p>└─ Pipfile.lock install</p></td>
<td><p>pipenv</p></td>
<td><p>14</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>14</p></td>
</tr>
<tr><td rowspan="3"><p>10</p></td>
<td><p>pipenv install Django</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td rowspan="3"><p>Local</p></td>
<td rowspan="3"><p>Removed</p></td>
<td><p>73.933</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>73.933</p></td>
</tr>
<tr><td><p>├─ locking/installing from lockfile</p></td>
<td><p>stopwatch</p></td>
<td><p>46</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>46</p></td>
</tr>
<tr><td><p>└─ Pipfile.lock install</p></td>
<td><p>pipenv</p></td>
<td><p>0</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>0</p></td>
</tr>
<tr><td><p>11</p></td>
<td><p>virtualenv</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td><p>Local</p></td>
<td><p>(not applicable)</p></td>
<td><p>5.864</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>5.864</p></td>
</tr>
<tr><td><p>12</p></td>
<td><p>pip install Nikola (cached)</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td><p>Local</p></td>
<td><p>Kept</p></td>
<td><p>10.951</p></td>
<td><p>—</p></td>
<td><p>—</p></td>
<td><p>10.951</p></td>
</tr>
<tr><td rowspan="3"><p>13</p></td>
<td><p>pipenv install Nikola</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td rowspan="3"><p>Local</p></td>
<td rowspan="3"><p>Partial, after interruption</p></td>
<td><p>607.647</p></td>
<td colspan="2"><p>(10m 7s)</p></td>
<td><p>607.647</p></td>
</tr>
<tr><td><p>├─ locking/installing from lockfile</p></td>
<td><p>stopwatch</p></td>
<td><p>590.85</p></td>
<td colspan="2"><p>(9m 50s)</p></td>
<td><p>590.85</p></td>
</tr>
<tr><td><p>└─ Pipfile.lock install</p></td>
<td><p>pipenv</p></td>
<td><p>6</p></td>
<td colspan="2"></td>
<td><p>6</p></td>
</tr>
<tr><td><p>14</p></td>
<td><p>pipenv install</p></td>
<td><p><code class="docutils literal">time</code></p></td>
<td><p>Local</p></td>
<td><p>Kept</p></td>
<td><p>31.399</p></td>
<td colspan="2"><p>(L/I: 10.51 s)</p></td>
<td><p>31.399</p></td>
</tr>
</tbody>
</table>
</section>
<section id="alternative-tools">
<h1>Alternative tools</h1>
<p>Python packaging is something with the state of which nobody seems to be
satisfied. As such, there are many new contenders for the role of “best new
packaging tool”.</p>
<p>Two popular alternatives packaging tools are <a class="reference external" href="https://github.com/jazzband/pip-tools">pip-tools</a> (by Vincent Driessen
and Jazzband) and <a class="reference external" href="https://github.com/sdispater/poetry">Poetry</a> (by Sébastien Eustace).</p>
<section id="pip-tools-locking-and-hashing-and-that-is-all">
<h2>Pip-tools: locking and hashing, and that is all</h2>
<p><strong>Pip-tools</strong> contains two tools. The first one is <code class="docutils literal"><span class="pre">pip-compile</span></code>. It locks
dependencies in <code class="docutils literal">requirements.txt</code> files, and that’s all it does. It allows
updating dependencies in the file based on what’s on PyPI. You can optionally
add hashes to that file. The second tool is <code class="docutils literal"><span class="pre">pip-sync</span></code>. It will synchronize
your virtualenv and the requirements file: it will delete packages not in that
file, so that you don’t work with stuff not declared in requirements, and will
ensure versions match the requirements file.  <code class="docutils literal"><span class="pre">pip-compile</span></code> takes roughly
10-20 seconds to run in the Nikola repo with a clean pip-tools cache (but with
the pip cache intact).</p>
<p>Its speed is fairly reasonable, and it does not try to be the be-all-end-all
tool for development. It handles a specific task, does it well, and does not
try to handle tasks it should not. Pip-tools lets you work with venvs in any
way you like, and it does not require anything specific. Unlike Pipenv and
Poetry, you can install it into the virtualenvs that need it, and not
system-wide.</p>
</section>
<section id="poetry-better-but-still-not-convincing">
<h2>Poetry: better, but still not convincing</h2>
<p><strong>Poetry</strong> is somewhere in between. Its main aim is close to Pipenv, but it
also makes it possible to distribute things to PyPI. It tries really hard to
hide that it uses Pip behind the scenes. Its README comes with an extensive
<a class="reference external" href="https://github.com/sdispater/poetry#what-about-pipenv">“What about Pipenv?”</a>
section, which I recommend reading — it has a few more examples of bad Pipenv
features.  Poetry claims to use the standardized (PEP 518) <code class="docutils literal">pyproject.toml</code>
file to replace the usual lot of files. Unfortunately, the only thing that is
standardized is the file name and syntax. Poetry uses custom <code class="docutils literal">[tool.poetry]</code>
sections, which means that one needs Poetry to fully use the packages created
with it, leading to vendor lock-in. There is a <code class="docutils literal">build</code> feature to produce a
sdist with setup.py and friends.</p>
<p>In February 2020, in a simple <code class="docutils literal">poetry add Nikola</code> test, it took <strong>about a
minute</strong> (55.1/50.8/53.6 s) to resolve dependencies (according to Poetry’s own
count, Local environment, Poetry cache removed), complete with reassuring output and
no quiet lockups.  Not as good as pip, but it’s more reasonable than Pipenv.
Also, the codebase and its layout are rather convoluted, and the docs are very
sparse and lacking. Poetry produces packages instead of just managing
dependencies, so it’s generally more useful than Pipenv. That said, I am not
convinced by that tool either.</p>
</section>
</section>
<section id="pip-is-here-to-stay">
<h1>Pip is here to stay!</h1>
<p>But in all the talk about new tools, we’re forgetting about the old ones, and
they do their job well — so well in fact, that the new tools still need them
under the covers.</p>
<p>Pip is fast. It does its job well enough. It lacks support for splitting
packages between production and development (as Pipenv and Poetry do). This
means that <code class="docutils literal">pip freeze</code> and <code class="docutils literal">pip install</code> are instant, at the cost of (a)
needing two separate environments, or (b) installing development dependencies
in production (which <em>should</em> only be a waste of HDD space and nothing more in
a well-architected system). But at the same time, pip-tools can help keep the
environments separate, as long as you take some time to write separate
<code class="docutils literal">requirements.in</code> files.</p>
<p>The virtualenv management features can be provided by virtualenvwrapper. That
tool’s main advantage is the shell script implementation, which means that
<code class="docutils literal">workon foo</code> activates the <code class="docutils literal">foo</code> virtualenv without spawning a new
subshell (an issue with Pipenv and Poetry, that I already covered when
describing Pipenv’s operation in the <a class="reference internal" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#running-scripts-badly">Running scripts (badly)</a> chapter.) An
argument often raised by Pipenv proponents is that one does not need to concern
itself with creating the virtualenv, and doesn’t need to care where it is.
Unfortuntately, many tools require this knowledge from their user, or force a
specific location, or require it to be different to the home directory.</p>
<p>And for a reasonable project template with release automation — well, I have my
own entry in that category, called (rather unoriginally) the <a class="reference external" href="https://github.com/Kwpolska/python-project-template">Python Project
Template (PyPT)</a>.</p>
<p>Yes, setup.py files are not ideal, since they use <code class="docutils literal">.py</code> code and a function
execution, making access to meta information hard (<code class="docutils literal">./setup.py egg_info</code>
creates tool-accessible text files). Their main advantage is that they are the
<em>only</em> format that is widely supported — pip is the de-facto default
Python package manager (which is pre-installed on Windows and Mac), and other
tools would require installation/bootstrapping first.</p>
</section>
<section id="the-break-neck-pace-of-pipenv">
<h1>The break-neck pace of Pipenv</h1>
<p>A good packaging tool is stable. In other words, it doesn’t change often, and
it strives to support existing environments. It wouldn’t be fun to re-download
everything on your system, because someone decided that <code class="docutils literal">/usr</code> is now called
<code class="docutils literal">/stuff</code>, and all the files in <code class="docutils literal">/usr</code> would become forgotten and not
removed. Well, this is what Pipenv did:</p>
<table class="table table-striped table-bordered">
<thead>
<tr><th class="head"><p>Date/Time (UTC)</p></th>
<th class="head"><p>Event</p></th>
</tr>
</thead>
<tbody>
<tr><td><p>2017-01-31 22:01</p></td>
<td><p>v3.2.14 released. <code class="docutils literal">pipenv <span class="pre">--three</span></code> creates <code class="docutils literal"><span class="pre">./.venv</span></code> (eg. <code class="docutils literal"><span class="pre">~/git/foo/.venv</span></code>). Last version with the original behavior of pipenv.</p></td>
</tr>
<tr><td><p>2017-02-01 05:36</p></td>
<td><p>v3.3.0 released. <code class="docutils literal">pipenv <span class="pre">--three</span></code> creates <code class="docutils literal"><span class="pre">~/.local/share/virtualenvs/foo</span></code> (to be precise, <code class="docutils literal">$WORKON_HOME/foo</code>).</p></td>
</tr>
<tr><td><p>2017-02-01 06:10</p></td>
<td><p><a class="reference external" href="https://github.com/pypa/pipenv/issues/178">Issue #178</a> is reported regarding the behavior change.</p></td>
</tr>
<tr><td><p>2017-02-01 06:18</p></td>
<td><p>Kenneth Reitz responds: “no plans for making it configurable.” and closes the issue.</p></td>
</tr>
<tr><td><p>2017-02-02 03:05</p></td>
<td><p>Kenneth Reitz responds: “added <code class="docutils literal">PIPENV_VENV_IN_PROJECT</code> mode for classic operation. Not released yet.”</p></td>
</tr>
<tr><td><p>2017-02-02 04:29</p></td>
<td><p>v3.3.3 released. The default is still uses a “remote” location, but <code class="docutils literal">.venv</code> can now be used.</p></td>
</tr>
<tr><td><p>2017-03-02 13:48</p></td>
<td><p>v3.5.0 released. The new default path is <code class="docutils literal"><span class="pre">$WORKON_HOME/foo-HASH</span></code>, eg. <code class="docutils literal"><span class="pre">~/.local/share/virtualenvs/foo-7pl2iuUI</span></code>.</p></td>
</tr>
</tbody>
</table>
<p>Over the course of a month, the location of the virtualenv changed twice. If
the user didn’t read the changelog and didn’t manually intervene (also of note,
the option name was mentioned in the issue and in v3.3.4’s changelog), they
would have a stale <code class="docutils literal">.venv</code> directory, since the new scheme was adopted for
them. And then, after switching to v3.5.0, they would have a stale virtualenv
hidden somewhere in their home directory, because pipenv decided to add hashes.</p>
<p>Also, this is not configurable. One cannot disable the hashes in paths, even
though <a class="reference external" href="https://github.com/pypa/pipenv/issues/589">users</a> <a class="reference external" href="https://github.com/pypa/pipenv/issues/1049">wanted</a> to. It would also help people
who want to mix Pipenv and virtualenvwrapper.</p>
<p>Pipenv is a very <strong>opinionated</strong> tool, and if the dev team changes their mind,
the old way is not supported.</p>
<p>Pipenv moves fast and doesn’t care if anything breaks. As an example, between
2018-03-13 13:21 and 2018-03-14 13:44 (a little over 24 hours), Pipenv had 10
releases, ranging from v11.6.2 to v11.7.3. The <a class="reference external" href="https://github.com/pypa/pipenv/blob/25df09c171a548fd71d4df735767bf763a653b83/HISTORY.txt">changelog</a> is rather unhelpful
when it comes to informing users what happened in each of the releases.</p>
<p>Extra reading:</p>
<ul class="simple">
<li><p><a class="reference external" href="http://web.archive.org/web/20180717140106/https://journal.kennethreitz.org/entry/r-python">Kenneth Reitz, A Letter to /r/python (with some notes about bipolar disorder)</a> (replaced with Wayback Machine link on 2020-02-07)</p></li>
<li><p>Reddit comment threads for the letter: <a class="reference external" href="https://np.reddit.com/r/Python/comments/8kdfd6/kenneth_reitz_a_letter_to_rpython_with_some_notes/">first</a> and <a class="reference external" href="https://np.reddit.com/r/Python/comments/8kjv8x/a_letter_to_rpython_kenneth_reitzs_journal/">second</a></p></li>
</ul>
</section>
<section id="conclusion">
<h1>Conclusion</h1>
<ul class="simple">
<li><p>Pipenv, contrary to popular belief and (now removed) propaganda, is not an
officially recommended tool of Python.org. It merely has a tutorial written
about it on packaging.python.org (page run by the PyPA).</p></li>
<li><p>Pipenv solves one use case reasonably well, but fails at many others, because
it forces a particular workflow on its users.</p></li>
<li><p>Pipenv does not handle any parts of packaging (cannot produce sdists and
wheels).  Users who want to upload to PyPI need to manage a <code class="docutils literal">setup.py</code> file
manually, alongside and independently of Pipenv.</p></li>
<li><p>Pipenv produces lockfiles, which are useful for reproducibility, at the cost
of installation speed. The speed is a noticeable issue with the tool. <code class="docutils literal">pip
freeze</code> is good enough for this, even if there are no dependency classes
(production vs development) and no hashes (which
have minor benefits) <a class="brackets" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#footnote-2" id="footnote-reference-5" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a></p></li>
<li><p>Poetry supports the same niche Pipenv does, while also adding the ability to
create packages and improving over many gripes of Pipenv. A notable issue is
the use of a custom all-encompassing file format, which makes switching tools
more difficult (vendor lock-in).</p></li>
<li><p>Pip, setup.py, and virtualenv — the traditional, tried-and-true tools — are
still available, undergoing constant development. Using them can lead to a
simpler, better experience.  Also of note, tools like virtualenvwrapper
can manage virtualenvs better than the aforementioned new Python tools,
because it is based on shell scripts (which can modify the enivironment).</p></li>
<li><p>Since 2018, the packaging scene deteriorated even more. See <a class="reference external" href="https://chriswarrick.com/blog/2023/01/15/how-to-improve-python-packaging/">How to improve
Python packaging, or why fourteen tools are at least twelve too many</a> (from
January 2023) and <a class="reference external" href="https://chriswarrick.com/blog/2024/01/15/python-packaging-one-year-later/">Python Packaging, One Year Later: A Look Back at 2023 in
Python Packaging</a> (from January 2024).</p></li>
</ul>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="footnote-1" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#footnote-reference-1">1</a><span class="fn-bracket">]</span></span>
<p>On a side note, the tutorial explains nothing. A prospective user only learns it’s similar to npm or bundler (what does that mean?), installs one package, and runs a <code class="docutils literal">.py</code> file through <code class="docutils literal">pipenv run</code>.</p>
</aside>
<aside class="footnote brackets" id="footnote-2" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></span>
<span class="backrefs">(<a role="doc-backlink" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#footnote-reference-2">1</a>,<a role="doc-backlink" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#footnote-reference-5">2</a>)</span>
<p>Note that one can’t change the file on PyPI after uploading it, so this would only be protection against rogue PyPI admins or a MitM attack (in which case you’ve got bigger problems anyways). <a class="reference external" href="https://github.com/nedbat/coveragepy/issues/679#issuecomment-406396761">Also, the feature is fairly broken.</a></p>
</aside>
<aside class="footnote brackets" id="footnote-3" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#footnote-reference-3">3</a><span class="fn-bracket">]</span></span>
<p>Fortunately, it looks in the parent directories for Pipfiles as well. Otherwise, you might end up with one environment for <code class="docutils literal">foo</code> and another for <code class="docutils literal">foo/foo</code> and yet another for <code class="docutils literal">foo/docs</code> and so on…</p>
</aside>
<aside class="footnote brackets" id="footnote-4" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2018/07/17/pipenv-promises-a-lot-delivers-very-little/#footnote-reference-4">4</a><span class="fn-bracket">]</span></span>
<p>The interruption happened by mistake due to the RAM disk running out of space, but it was actually a good thing to have happened.</p>
</aside>
</aside>
<hr class="docutils">
<p class="alert alert-info"><strong>Other discussion threads:</strong> <a class="reference external" href="https://www.reddit.com/r/Python/comments/a3h81m/pipenv_promises_a_lot_delivers_very_little/">r/Python</a>, <a class="reference external" href="https://news.ycombinator.com/item?id=18612590">Hacker News</a>.</p>
</section>
]]></content:encoded><category>Python</category><category>packaging</category><category>Pipenv</category><category>Python</category></item><item><title>Spawning subprocesses smartly and securely</title><dc:creator>Chris Warrick</dc:creator><link>https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/</link><pubDate>Sat, 02 Sep 2017 18:40:00 GMT</pubDate><guid>https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/</guid><description>
As part of your code, you may be inclined to call a command to do
something. But is it always a good idea? How to do it safely? What happens
behind the scenes?
</description><content:encoded><![CDATA[
<p>As part of your code, you may be inclined to call a command to do
something. But is it always a good idea? How to do it safely? What happens
behind the scenes?</p>



<p>This article is written from a general perspective, with a Unix/C bias and a
very slight Python bias. The problems mentioned apply to all languages in most
environments, including Windows.</p>
<section id="use-the-right-tool-for-the-job">
<h1>Use the right tool for the job</h1>
<p>By calling another process, you introduce a third-party dependency.
That dependency isn’t controlled by your code, and your code becomes more fragile.
The problems include:</p>
<ul class="simple">
<li><p>the program is not installed, or even available, for the user’s OS of choice</p></li>
<li><p>the program is not in the <code class="docutils literal">$PATH</code> your process gets</p></li>
<li><p>the hard-coded path is not correct on the end user’s system</p></li>
<li><p>the program is in a different version (eg. GNU vs. BSD, updates/patches),
which means different option names or other behaviors</p></li>
<li><p>the program’s output is not what you expected due to user config (including
locale)</p></li>
<li><p>error reporting is based on numeric exit codes, and the meaning of those
differs between programs (<em>if</em> they have meaning besides 0/1 in the first
place)</p></li>
</ul>
<p>On the other hand, if your code uses a lot of subprocesses, perhaps you should
stay with Bash. You can do the harder parts with Python, Ruby, or some other
language by calling them from within your Bash script.</p>
</section>
<section id="dont-spawn-subprocesses-if-theres-an-alternative">
<h1>Don’t spawn subprocesses if there’s an alternative</h1>
<p>Spawning a subprocess always incurs a (minor) <a class="brackets" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#footnote-1" id="footnote-reference-1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> performance hit minor
compared to the alternatives. With that in mind, and the resiliency issues
listed above, you should always try to find an alternative for the
external command.</p>
<p>The simplest ones are the basic Unix utilities. Replace <code class="docutils literal">grep</code>, <code class="docutils literal">sed</code> and
<code class="docutils literal">awk</code> with string operations and regular expressions. Filesystem utilities
will have equivalents — for Python, in <code class="docutils literal">os</code> or <code class="docutils literal">shutil</code>. Your language of
choice can also handle things like networking (don’t call <code class="docutils literal">curl</code>), file
compression, working with date/time…</p>
<p>Similarly, you should check if there are packages available that already do
what you want — library bindings or re-implementations. And if there isn’t,
perhaps you could help the world by writing one of those and sharing it?</p>
<p>One more important thing: if the program uses the same language as your code,
then you should try to import the code and run it from the same process instead
of spawning a process, if this is feasible.</p>
</section>
<section id="security-considerations-shells-spaces-and-command-injection">
<h1>Security considerations: shells, spaces, and command injection</h1>
<p>We come to the most important part of this article: how to spawn subprocesses
without compromising your system. When you spawn a subprocess on a typical Unix
system,  <code class="docutils literal">fork()</code> is called, and your process is copied. Many modern Unix
systems have a copy-on-write implementation of that syscall, meaning that the
operation does not result in copying all the memory of the host process over.
Forking is (almost) immediately followed by calling <code class="docutils literal">execve()</code> (or a helper
function from the exec family) <a class="brackets" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#footnote-2" id="footnote-reference-2" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a> in the child process — that function
<em>transforms the calling process into a new process</em> <a class="brackets" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#footnote-3" id="footnote-reference-3" role="doc-noteref"><span class="fn-bracket">[</span>3<span class="fn-bracket">]</span></a>. This technique is
called <em>fork-exec</em> and is the typical way to spawn a new process on Unix. <a class="brackets" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#footnote-4" id="footnote-reference-4" role="doc-noteref"><span class="fn-bracket">[</span>4<span class="fn-bracket">]</span></a></p>
<p>There are two ways to access this API, from the C perspective:</p>
<ul>
<li><p>directly, by calling <code class="docutils literal">fork()</code> and <code class="docutils literal"><span class="pre">exec*()</span></code> (or <code class="docutils literal">posix_spawn()</code>), and providing an array of
arguments passed to the process, or</p></li>
<li><p>through the shell (<code class="docutils literal">sh</code>), usually by calling <code class="docutils literal">system()</code>. As Linux’s
manpage for <code class="docutils literal">system(3)</code> puts it,</p>
<blockquote>
<p>The <code class="docutils literal">system()</code> library function uses <code class="docutils literal">fork(2)</code> to create a child process that executes the shell command specified in command using <code class="docutils literal">execl(3)</code> as follows:</p>
<div class="code"><pre class="code c"><a id="rest_code_ba3def1e6d134f318af0424fea199c9a-1" name="rest_code_ba3def1e6d134f318af0424fea199c9a-1" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_ba3def1e6d134f318af0424fea199c9a-1"></a><span class="n">execl</span><span class="p">(</span><span class="s">&quot;/bin/sh&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;sh&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;-c&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">command</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
</pre></div>
</blockquote>
</li>
</ul>
<p>If you go through the shell, you pass one string argument, whereas <code class="docutils literal"><span class="pre">exec*()</span></code> demands you to specify arguments separately. Let’s write a sample program to print all the arguments it receives. I’ll do it in Python to get a more readable output.</p>
<div class="code"><pre class="code python"><a id="rest_code_ce72efbf58624ae598862ad3319d5d48-1" name="rest_code_ce72efbf58624ae598862ad3319d5d48-1" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_ce72efbf58624ae598862ad3319d5d48-1"></a><span class="ch">#!/usr/bin/env python3</span>
<a id="rest_code_ce72efbf58624ae598862ad3319d5d48-2" name="rest_code_ce72efbf58624ae598862ad3319d5d48-2" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_ce72efbf58624ae598862ad3319d5d48-2"></a><span class="kn">import</span><span class="w"> </span><span class="nn">sys</span>
<a id="rest_code_ce72efbf58624ae598862ad3319d5d48-3" name="rest_code_ce72efbf58624ae598862ad3319d5d48-3" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_ce72efbf58624ae598862ad3319d5d48-3"></a><span class="nb">print</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span>
</pre></div>
<p>Let’s see what appears:</p>
<div class="code"><pre class="code text"><a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-1" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-1" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-1"></a>$ ./argv.py foo bar
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-2" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-2" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-2"></a>[&#39;./argv.py&#39;, &#39;foo&#39;, &#39;bar&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-3" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-3" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-3"></a>$ ./argv.py &#39;foo bar&#39;
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-4" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-4" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-4"></a>[&#39;./argv.py&#39;, &#39;foo bar&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-5" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-5" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-5"></a>$ ./argv.py foo\ bar baz
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-6" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-6" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-6"></a>[&#39;./argv.py&#39;, &#39;foo bar&#39;, &#39;baz&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-7" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-7" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-7"></a>
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-8" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-8" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-8"></a>$ ./argv.py $(date)
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-9" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-9" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-9"></a>[&#39;./argv.py&#39;, &#39;Sat&#39;, &#39;Sep&#39;, &#39;2&#39;, &#39;16:54:52&#39;, &#39;CEST&#39;, &#39;2017&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-10" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-10" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-10"></a>$ ./argv.py &quot;$(date)&quot;
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-11" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-11" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-11"></a>[&#39;./argv.py&#39;, &#39;Sat Sep  2 16:54:52 CEST 2017&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-12" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-12" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-12"></a>
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-13" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-13" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-13"></a>$ ./argv.py /usr/*
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-14" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-14" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-14"></a>[&#39;./argv.py&#39;, &#39;/usr/X11&#39;, &#39;/usr/X11R6&#39;, &#39;/usr/bin&#39;, &#39;/usr/include&#39;, &#39;/usr/lib&#39;, &#39;/usr/libexec&#39;, &#39;/usr/local&#39;, &#39;/usr/sbin&#39;, &#39;/usr/share&#39;, &#39;/usr/standalone&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-15" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-15" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-15"></a>$ ./argv.py &quot;/usr/*&quot;
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-16" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-16" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-16"></a>[&#39;./argv.py&#39;, &#39;/usr/*&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-17" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-17" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-17"></a>
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-18" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-18" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-18"></a>$ ./argv.py $EDITOR
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-19" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-19" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-19"></a>[&#39;./argv.py&#39;, &#39;nvim&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-20" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-20" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-20"></a>
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-21" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-21" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-21"></a>$ $PWD/argv.py foo bar
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-22" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-22" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-22"></a>[&#39;/Users/kwpolska/Desktop/blog/subprocess/argv.py&#39;, &#39;foo&#39;, &#39;bar&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-23" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-23" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-23"></a>$ ./argv.py a{b,c}d
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-24" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-24" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-24"></a>[&#39;./argv.py&#39;, &#39;abd&#39;, &#39;acd&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-25" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-25" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-25"></a>
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-26" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-26" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-26"></a>$ python argv.py foo bar | cat
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-27" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-27" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-27"></a>[&#39;argv.py&#39;, &#39;foo&#39;, &#39;bar&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-28" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-28" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-28"></a>$ python argv.py foo bar &gt; foo.txt
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-29" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-29" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-29"></a>$ cat foo.txt
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-30" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-30" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-30"></a>[&#39;argv.py&#39;, &#39;foo&#39;, &#39;bar&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-31" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-31" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-31"></a>
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-32" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-32" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-32"></a>$ ./argv.py foo; ls /usr
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-33" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-33" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-33"></a>[&#39;./argv.py&#39;, &#39;foo&#39;]
<a id="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-34" name="rest_code_8db936d5a5784d2eba2d26845ebcc5c4-34" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#rest_code_8db936d5a5784d2eba2d26845ebcc5c4-34"></a>X11@        X11R6@      bin/        include/    lib/        libexec/    local/      sbin/       share/      standalone/
</pre></div>
<p>As you can see, the following things are handled by the shell (the process is unaware of this occurring):</p>
<ul class="simple">
<li><p>quotes and escapes</p></li>
<li><p>expanding expressions in braces</p></li>
<li><p>expanding variables</p></li>
<li><p>wildcards (glob, <code class="docutils literal">*</code>)</p></li>
<li><p>redirections and pipes (<code class="docutils literal">&gt; &gt;&gt; |</code>)</p></li>
<li><p>command substitution (backticks or <code class="docutils literal"><span class="pre">$(…)</span></code>)</p></li>
<li><p>running multiple commands on the same line (<code class="docutils literal">; &amp;&amp; || &amp;</code>)</p></li>
</ul>
<p>The list is full of potential vulnerabilities. If end users are in control of
the arguments passed, and you go through the shell, they can
<strong>execute arbitrary commands</strong> or even <strong>get full shell access</strong>. Even in other
cases, you’ll have to <em>depend on the shell’s parsing</em>, which introduces an
unnecessary indirection.</p>
</section>
<section id="tl-dr-how-to-do-this-properly-in-your-language-of-choice">
<h1>TL;DR: How to do this properly in your language of choice</h1>
<p>To ensure spawning subprocess is done securely, <strong>do not use the shell in between</strong>. If you need any of the operations I listed above as part of your command — wildcards, pipes, etc. — you will need to take care of them in your code; most languages have those features built-in.</p>
<dl class="simple dl-horizontal">
<dt>In C (Unix)</dt>
<dd><p>Perform fork-exec by yourself, or use <code class="docutils literal">posix_spawn()</code>. This also lets you communicate with the process if you open a pipe and make it stdout of the child process. Never use <code class="docutils literal">system()</code>.</p>
</dd>
<dt>In Python</dt>
<dd><p>Use the subprocess module. Always pass <code class="docutils literal">shell=False</code> and give it a <em>list</em> of arguments. With asyncio, use <code class="docutils literal">asyncio.create_subprocess_exec</code> (and not <code class="docutils literal">_shell</code>), but note it takes <code class="docutils literal">*args</code> and not a list. Never use <code class="docutils literal">os.system</code> and <code class="docutils literal">os.popen</code>.</p>
</dd>
<dt>In Ruby</dt>
<dd><p>Pass arrays to <code class="docutils literal">IO.popen</code>. Pass multiple arguments to <code class="docutils literal">system()</code> (<code class="docutils literal"><span class="pre">system([&quot;ls&quot;,</span> <span class="pre">&quot;ls&quot;])</span></code> or <code class="docutils literal"><span class="pre">system(&quot;ls&quot;,</span> <span class="pre">&quot;-l&quot;)</span></code>). Never use <code class="docutils literal">%x{command}</code> or backticks.</p>
</dd>
<dt>In Java</dt>
<dd><p>Pass arrays to <code class="docutils literal">Runtime.exec</code>. Pass multiple arguments or list to <code class="docutils literal">ProcessBuilder</code>.</p>
</dd>
<dt>In PHP</dt>
<dd><p>All the standard methods go through the shell. Try <code class="docutils literal">escapeshellcmd()</code>, <code class="docutils literal">escapeshellarg()</code> — or better, switch to Python. Or anything, really.</p>
</dd>
<dt>In Go</dt>
<dd><p><code class="docutils literal">os/exec</code> and <code class="docutils literal">os.StartProcess</code> are safe.</p>
</dd>
<dt>In Node.js</dt>
<dd><p>Use <code class="docutils literal">child_process.execFile</code> or <code class="docutils literal">child_process.spawn</code> with <code class="docutils literal">shell</code> set to false.</p>
</dd>
<dt>Elsewhere</dt>
<dd><p>You should be able to specify multiple strings (using variadic arguments,
arrays, or otherwise standard data structures of your language of choice) as
the command line. Otherwise, you might be running into something
shell-related.</p>
</dd>
</dl>
</section>
<section id="the-part-where-i-pretend-i-know-something-about-windows">
<h1>The part where I pretend I know something about Windows</h1>
<p>On Windows, argument lists are always passed to processes as strings (Python
joins them semi-intelligently if it gets a list). Redirections and variables
work in shell mode, but globs (asterisks) are always left for the called
process to handle.</p>
<p>Some useful functions are implemented as shell built-ins — in that case, you
need to call it via the shell.</p>
<p>Internals: There is no <code class="docutils literal">fork()</code> on Windows. Instead, <code class="docutils literal">CreateProcess()</code>,
<code class="docutils literal">ShellExecute()</code>, or lower-level <code class="docutils literal"><span class="pre">spawn*()</span></code> functions are used. <code class="docutils literal">cmd.exe
/c</code> is called in shell calls.</p>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="footnote-1" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#footnote-reference-1">1</a><span class="fn-bracket">]</span></span>
<p>Unless your operating system does not implement copy-on-write forking — in that case, you might even run out of memory if you use too much of it.</p>
</aside>
<aside class="footnote brackets" id="footnote-2" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#footnote-reference-2">2</a><span class="fn-bracket">]</span></span>
<p>The function that does the real work is <code class="docutils literal">execve()</code>, which takes an exact path, an array of arguments, and takes environment variables as input. Other variants can also perform a <code class="docutils literal">$PATH</code> search, take argv as variadic arguments, and inherit environment from the current process. <code class="docutils literal">execl()</code> does the last two.</p>
</aside>
<aside class="footnote brackets" id="footnote-3" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#footnote-reference-3">3</a><span class="fn-bracket">]</span></span>
<p>Quoted from <code class="docutils literal">execve(2)</code> <a class="reference external" href="https://www.freebsd.org/cgi/man.cgi?query=execve&amp;sektion=2">man page</a> from FreeBSD.</p>
</aside>
<aside class="footnote brackets" id="footnote-4" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="https://chriswarrick.com/blog/2017/09/02/spawning-subprocesses-smartly-and-securely/#footnote-reference-4">4</a><span class="fn-bracket">]</span></span>
<p>An alternative is <code class="docutils literal">posix_spawn()</code>, but it usually does fork-exec, unless your platform does not support forking.</p>
</aside>
</aside>
</section>
]]></content:encoded><category>Programming</category><category>best practices</category><category>C</category><category>devel</category><category>guide</category><category>Linux</category><category>Python</category><category>security</category><category>subprocess</category><category>Unix</category></item><item><title>Gynvael’s Mission 11 (en): Python bytecode reverse-engineering</title><dc:creator>Chris Warrick</dc:creator><link>https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/</link><pubDate>Thu, 03 Aug 2017 10:45:40 GMT</pubDate><guid>https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/</guid><description>
Gynvael Coldwind is a security researcher at Google, who hosts weekly livestreams about security and programming in Polish and English). As part of the streams, he gives out missions — basically, CTF-style reverse engineering tasks. Yesterday’s mission was about Elvish — I mean Paint — I mean Python programming and bytecode.
</description><content:encoded><![CDATA[
<p>Gynvael Coldwind is a security researcher at Google, who hosts weekly livestreams about security and programming in <a class="reference external" href="https://gaming.youtube.com/user/GynvaelColdwind/live">Polish</a> and <a class="reference external" href="https://gaming.youtube.com/user/GynvaelEN/live">English</a>). As part of the streams, he gives out missions — basically, CTF-style reverse engineering tasks. Yesterday’s mission was about Elvish — I mean Paint — I mean Python programming and bytecode.</p>



<div class="code"><pre class="code text"><a id="rest_code_a8243f6403b44a13a671955d2b908777-1" name="rest_code_a8243f6403b44a13a671955d2b908777-1" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-1"></a>MISSION 011               goo.gl/13Bia9             DIFFICULTY: ██████░░░░ [6╱10]
<a id="rest_code_a8243f6403b44a13a671955d2b908777-2" name="rest_code_a8243f6403b44a13a671955d2b908777-2" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-2"></a>┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅
<a id="rest_code_a8243f6403b44a13a671955d2b908777-3" name="rest_code_a8243f6403b44a13a671955d2b908777-3" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-3"></a>
<a id="rest_code_a8243f6403b44a13a671955d2b908777-4" name="rest_code_a8243f6403b44a13a671955d2b908777-4" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-4"></a>Finally some real work!
<a id="rest_code_a8243f6403b44a13a671955d2b908777-5" name="rest_code_a8243f6403b44a13a671955d2b908777-5" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-5"></a>
<a id="rest_code_a8243f6403b44a13a671955d2b908777-6" name="rest_code_a8243f6403b44a13a671955d2b908777-6" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-6"></a>One of our field agents managed to infiltrate suspects hideout and steal a
<a id="rest_code_a8243f6403b44a13a671955d2b908777-7" name="rest_code_a8243f6403b44a13a671955d2b908777-7" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-7"></a>pendrive possibly containing important information. However, the pendrive
<a id="rest_code_a8243f6403b44a13a671955d2b908777-8" name="rest_code_a8243f6403b44a13a671955d2b908777-8" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-8"></a>actually requires one to authenticate themselves before accessing the stored
<a id="rest_code_a8243f6403b44a13a671955d2b908777-9" name="rest_code_a8243f6403b44a13a671955d2b908777-9" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-9"></a>files.
<a id="rest_code_a8243f6403b44a13a671955d2b908777-10" name="rest_code_a8243f6403b44a13a671955d2b908777-10" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-10"></a>
<a id="rest_code_a8243f6403b44a13a671955d2b908777-11" name="rest_code_a8243f6403b44a13a671955d2b908777-11" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-11"></a>We gave the pendrive to our laboratory and they managed to dump the firmware. We
<a id="rest_code_a8243f6403b44a13a671955d2b908777-12" name="rest_code_a8243f6403b44a13a671955d2b908777-12" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-12"></a>looked at the deadlisting they sent and for our best knowledge it&#39;s some form of
<a id="rest_code_a8243f6403b44a13a671955d2b908777-13" name="rest_code_a8243f6403b44a13a671955d2b908777-13" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-13"></a>Elvish. We can&#39;t read it.
<a id="rest_code_a8243f6403b44a13a671955d2b908777-14" name="rest_code_a8243f6403b44a13a671955d2b908777-14" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-14"></a>
<a id="rest_code_a8243f6403b44a13a671955d2b908777-15" name="rest_code_a8243f6403b44a13a671955d2b908777-15" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-15"></a>Here is the firmware: goo.gl/axsAHt
<a id="rest_code_a8243f6403b44a13a671955d2b908777-16" name="rest_code_a8243f6403b44a13a671955d2b908777-16" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-16"></a>
<a id="rest_code_a8243f6403b44a13a671955d2b908777-17" name="rest_code_a8243f6403b44a13a671955d2b908777-17" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-17"></a>And off you go. Bring us back the password.
<a id="rest_code_a8243f6403b44a13a671955d2b908777-18" name="rest_code_a8243f6403b44a13a671955d2b908777-18" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-18"></a>
<a id="rest_code_a8243f6403b44a13a671955d2b908777-19" name="rest_code_a8243f6403b44a13a671955d2b908777-19" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-19"></a>Good luck!
<a id="rest_code_a8243f6403b44a13a671955d2b908777-20" name="rest_code_a8243f6403b44a13a671955d2b908777-20" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-20"></a>
<a id="rest_code_a8243f6403b44a13a671955d2b908777-21" name="rest_code_a8243f6403b44a13a671955d2b908777-21" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-21"></a>---------------------------------------------------------------------------------
<a id="rest_code_a8243f6403b44a13a671955d2b908777-22" name="rest_code_a8243f6403b44a13a671955d2b908777-22" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-22"></a>
<a id="rest_code_a8243f6403b44a13a671955d2b908777-23" name="rest_code_a8243f6403b44a13a671955d2b908777-23" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-23"></a>If you decode the answer, put it in the comments under this video! If you write
<a id="rest_code_a8243f6403b44a13a671955d2b908777-24" name="rest_code_a8243f6403b44a13a671955d2b908777-24" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-24"></a>a blogpost / post your solution online, please add a link in the comments too!
<a id="rest_code_a8243f6403b44a13a671955d2b908777-25" name="rest_code_a8243f6403b44a13a671955d2b908777-25" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-25"></a>
<a id="rest_code_a8243f6403b44a13a671955d2b908777-26" name="rest_code_a8243f6403b44a13a671955d2b908777-26" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-26"></a>P.S. I&#39;ll show/explain the solution on the stream in ~two weeks.
<a id="rest_code_a8243f6403b44a13a671955d2b908777-27" name="rest_code_a8243f6403b44a13a671955d2b908777-27" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_a8243f6403b44a13a671955d2b908777-27"></a>P.S.2. Bonus points for recreating the original high-level code.
</pre></div>
<p>Here’s the firmware:</p>
<div class="code"><pre class="code text"><a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-1" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-1" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-1"></a>co_argcount 1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-2" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-2" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-2"></a>co_consts (None, &#39;4e5d4e92865a4e495a86494b5a5d49525261865f5758534d4a89&#39;, &#39;hex&#39;, 89, 255, 115, 50)
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-3" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-3" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-3"></a>co_flags 67
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-4" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-4" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-4"></a>co_name check_password
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-5" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-5" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-5"></a>co_names (&#39;decode&#39;, &#39;len&#39;, &#39;False&#39;, &#39;all&#39;, &#39;zip&#39;, &#39;ord&#39;)
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-6" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-6" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-6"></a>co_nlocals 4
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-7" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-7" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-7"></a>co_stacksize 6
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-8" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-8" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-8"></a>co_varnames (&#39;s&#39;, &#39;good&#39;, &#39;cs&#39;, &#39;cg&#39;)
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-9" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-9" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-9"></a>              0 LOAD_CONST               1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-10" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-10" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-10"></a>              3 LOAD_ATTR                0
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-11" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-11" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-11"></a>              6 LOAD_CONST               2
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-12" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-12" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-12"></a>              9 CALL_FUNCTION            1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-13" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-13" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-13"></a>             12 STORE_FAST               1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-14" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-14" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-14"></a>             15 LOAD_GLOBAL              1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-15" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-15" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-15"></a>             18 LOAD_FAST                0
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-16" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-16" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-16"></a>             21 CALL_FUNCTION            1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-17" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-17" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-17"></a>             24 LOAD_GLOBAL              1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-18" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-18" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-18"></a>             27 LOAD_FAST                1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-19" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-19" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-19"></a>             30 CALL_FUNCTION            1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-20" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-20" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-20"></a>             33 COMPARE_OP               3 (!=)
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-21" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-21" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-21"></a>             36 POP_JUMP_IF_FALSE       43
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-22" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-22" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-22"></a>             39 LOAD_GLOBAL              2
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-23" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-23" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-23"></a>             42 RETURN_VALUE
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-24" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-24" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-24"></a>        &gt;&gt;   43 LOAD_GLOBAL              3
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-25" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-25" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-25"></a>             46 BUILD_LIST               0
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-26" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-26" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-26"></a>             49 LOAD_GLOBAL              4
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-27" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-27" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-27"></a>             52 LOAD_FAST                0
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-28" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-28" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-28"></a>             55 LOAD_FAST                1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-29" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-29" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-29"></a>             58 CALL_FUNCTION            2
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-30" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-30" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-30"></a>             61 GET_ITER
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-31" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-31" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-31"></a>        &gt;&gt;   62 FOR_ITER                52 (to 117)
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-32" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-32" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-32"></a>             65 UNPACK_SEQUENCE          2
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-33" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-33" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-33"></a>             68 STORE_FAST               2
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-34" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-34" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-34"></a>             71 STORE_FAST               3
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-35" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-35" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-35"></a>             74 LOAD_GLOBAL              5
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-36" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-36" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-36"></a>             77 LOAD_FAST                2
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-37" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-37" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-37"></a>             80 CALL_FUNCTION            1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-38" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-38" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-38"></a>             83 LOAD_CONST               3
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-39" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-39" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-39"></a>             86 BINARY_SUBTRACT
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-40" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-40" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-40"></a>             87 LOAD_CONST               4
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-41" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-41" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-41"></a>             90 BINARY_AND
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-42" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-42" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-42"></a>             91 LOAD_CONST               5
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-43" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-43" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-43"></a>             94 BINARY_XOR
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-44" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-44" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-44"></a>             95 LOAD_CONST               6
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-45" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-45" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-45"></a>             98 BINARY_XOR
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-46" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-46" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-46"></a>             99 LOAD_GLOBAL              5
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-47" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-47" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-47"></a>            102 LOAD_FAST                3
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-48" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-48" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-48"></a>            105 CALL_FUNCTION            1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-49" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-49" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-49"></a>            108 COMPARE_OP               2 (==)
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-50" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-50" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-50"></a>            111 LIST_APPEND              2
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-51" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-51" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-51"></a>            114 JUMP_ABSOLUTE           62
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-52" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-52" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-52"></a>        &gt;&gt;  117 CALL_FUNCTION            1
<a id="rest_code_ee28059f26f346f09e4a2ab239ddd619-53" name="rest_code_ee28059f26f346f09e4a2ab239ddd619-53" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ee28059f26f346f09e4a2ab239ddd619-53"></a>            120 RETURN_VALUE
</pre></div>
<p>To the uninitiated, this might look like <em>Elvish</em>. In reality, this is Python bytecode — the instruction set understood by Python’s (CPython 2.7) virtual machine. Python, like many other languages, uses a compiler to translate human-readable source code into something more appropriate for computers. Python code compiles to bytecode, which is then executed by CPython’s virtual machine. CPython bytecode can be ported between different hardware, while machine code cannot. However, machine code can often be faster than languages based on virtual machines and bytecode. (Java and C# work the same way as Python, C compiles directly to machine code)</p>
<p>This is the internal representation of a Python function. The first few lines are the member variables of the <code class="docutils literal">f.__code__</code> object of our function. We know that:</p>
<ul class="simple">
<li><p>it takes 1 argument</p></li>
<li><p>it has 7 constants: None, a long string of hex digits, the string <code class="docutils literal">'hex'</code>, and numbers: 89, 255, 115, 50.</p></li>
<li><p>its <a class="reference external" href="https://docs.python.org/2.7/library/inspect.html#code-objects-bit-flags">flags</a> are set to 67 (CO_NOFREE, CO_NEWLOCALS, CO_OPTIMIZED). This is the “standard” value that most uncomplicated functions take.</p></li>
<li><p>its name is <code class="docutils literal">check_password</code></p></li>
<li><p>it uses the following globals or attribute names: <code class="docutils literal">decode</code>, <code class="docutils literal">len</code>, <code class="docutils literal">False</code>, <code class="docutils literal">all</code>, <code class="docutils literal">zip</code>, <code class="docutils literal">ord</code></p></li>
<li><p>it has 4 local variables</p></li>
<li><p>it uses a stack of size 6</p></li>
<li><p>its variables are named <code class="docutils literal">s</code>, <code class="docutils literal">good</code>, <code class="docutils literal">cs</code>, <code class="docutils literal">cg</code></p></li>
</ul>
<p>There are two ways to solve this task: you can re-assemble the <code class="docutils literal">dis</code> output with the help of the <code class="docutils literal">opcode</code> module, or try to re-create the function by hand, using the bytecode. I chose the latter method.</p>
<section id="reverse-engineering-python-bytecode-re-creating-the-function-by-hand">
<h1>Reverse-engineering Python bytecode: re-creating the function by hand</h1>
<p>I started by recreating the original firmware file. I created an empty function and wrote some code to print out <code class="docutils literal">__code__</code> contents and <code class="docutils literal">dis.dis</code> output. I also added color-coding to help me read it:</p>
<div class="code"><pre class="code python"><a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-1" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-1" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-1"></a><span class="ch">#!/usr/bin/env python2</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-2" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-2" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-2"></a><span class="kn">import</span><span class="w"> </span><span class="nn">dis</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-3" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-3" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">sys</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-4" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-4" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-4"></a>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-5" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-5" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-5"></a><span class="c1"># Write code here</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-6" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-6" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-6"></a><span class="k">def</span><span class="w"> </span><span class="nf">check_password</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-7" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-7" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-7"></a>    <span class="k">pass</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-8" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-8" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-8"></a>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-9" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-9" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-9"></a><span class="c1"># Reverse engineering the code</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-10" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-10" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-10"></a><span class="n">cnames</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;co_argcount&#39;</span><span class="p">,</span> <span class="s1">&#39;co_consts&#39;</span><span class="p">,</span> <span class="s1">&#39;co_flags&#39;</span><span class="p">,</span> <span class="s1">&#39;co_name&#39;</span><span class="p">,</span> <span class="s1">&#39;co_names&#39;</span><span class="p">,</span> <span class="s1">&#39;co_nlocals&#39;</span><span class="p">,</span> <span class="s1">&#39;co_stacksize&#39;</span><span class="p">,</span> <span class="s1">&#39;co_varnames&#39;</span><span class="p">)</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-11" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-11" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-11"></a><span class="n">cvalues</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="s1">&#39;4e5d4e92865a4e495a86494b5a5d49525261865f5758534d4a89&#39;</span><span class="p">,</span> <span class="s1">&#39;hex&#39;</span><span class="p">,</span> <span class="mi">89</span><span class="p">,</span> <span class="mi">255</span><span class="p">,</span> <span class="mi">115</span><span class="p">,</span> <span class="mi">50</span><span class="p">),</span> <span class="mi">67</span><span class="p">,</span> <span class="s1">&#39;check_password&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;decode&#39;</span><span class="p">,</span> <span class="s1">&#39;len&#39;</span><span class="p">,</span> <span class="s1">&#39;False&#39;</span><span class="p">,</span> <span class="s1">&#39;all&#39;</span><span class="p">,</span> <span class="s1">&#39;zip&#39;</span><span class="p">,</span> <span class="s1">&#39;ord&#39;</span><span class="p">),</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;s&#39;</span><span class="p">,</span> <span class="s1">&#39;good&#39;</span><span class="p">,</span> <span class="s1">&#39;cs&#39;</span><span class="p">,</span> <span class="s1">&#39;cg&#39;</span><span class="p">))</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-12" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-12" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-12"></a>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-13" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-13" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-13"></a><span class="k">for</span> <span class="n">n</span><span class="p">,</span> <span class="n">ov</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">cnames</span><span class="p">,</span> <span class="n">cvalues</span><span class="p">):</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-14" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-14" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-14"></a>    <span class="n">v</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">check_password</span><span class="o">.</span><span class="vm">__code__</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-15" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-15" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-15"></a>    <span class="k">if</span> <span class="n">v</span> <span class="o">==</span> <span class="n">ov</span><span class="p">:</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-16" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-16" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-16"></a>        <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\033</span><span class="s1">[1;32m&#39;</span><span class="p">)</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-17" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-17" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-17"></a>    <span class="k">else</span><span class="p">:</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-18" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-18" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-18"></a>        <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\033</span><span class="s1">[1;31m&#39;</span><span class="p">)</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-19" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-19" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-19"></a>    <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-20" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-20" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-20"></a>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-21" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-21" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-21"></a>    <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-22" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-22" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-22"></a>    <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-23" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-23" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-23"></a>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-24" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-24" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-24"></a>    <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\033</span><span class="s1">[0m&#39;</span><span class="p">)</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-25" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-25" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-25"></a>    <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-26" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-26" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-26"></a>
<a id="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-27" name="rest_code_db3df9519cfe4fcebd1d69fff256cc2e-27" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_db3df9519cfe4fcebd1d69fff256cc2e-27"></a><span class="n">dis</span><span class="o">.</span><span class="n">dis</span><span class="p">(</span><span class="n">check_password</span><span class="p">)</span>
</pre></div>
<p>If we run this solver, we get the following output (text in brackets added by me):</p>
<div class="code"><pre class="code text"><a id="rest_code_27f66539bcdd4afa8277aa02dff98f11-1" name="rest_code_27f66539bcdd4afa8277aa02dff98f11-1" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_27f66539bcdd4afa8277aa02dff98f11-1"></a>co_argcount 1            [OK]
<a id="rest_code_27f66539bcdd4afa8277aa02dff98f11-2" name="rest_code_27f66539bcdd4afa8277aa02dff98f11-2" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_27f66539bcdd4afa8277aa02dff98f11-2"></a>co_consts (None,)        [1/7 match]
<a id="rest_code_27f66539bcdd4afa8277aa02dff98f11-3" name="rest_code_27f66539bcdd4afa8277aa02dff98f11-3" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_27f66539bcdd4afa8277aa02dff98f11-3"></a>co_flags 67              [OK]
<a id="rest_code_27f66539bcdd4afa8277aa02dff98f11-4" name="rest_code_27f66539bcdd4afa8277aa02dff98f11-4" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_27f66539bcdd4afa8277aa02dff98f11-4"></a>co_name check_password   [OK]
<a id="rest_code_27f66539bcdd4afa8277aa02dff98f11-5" name="rest_code_27f66539bcdd4afa8277aa02dff98f11-5" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_27f66539bcdd4afa8277aa02dff98f11-5"></a>co_names ()              [0/6 match]
<a id="rest_code_27f66539bcdd4afa8277aa02dff98f11-6" name="rest_code_27f66539bcdd4afa8277aa02dff98f11-6" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_27f66539bcdd4afa8277aa02dff98f11-6"></a>co_nlocals 1             [should be 4]
<a id="rest_code_27f66539bcdd4afa8277aa02dff98f11-7" name="rest_code_27f66539bcdd4afa8277aa02dff98f11-7" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_27f66539bcdd4afa8277aa02dff98f11-7"></a>co_stacksize 1           [should be 6]
<a id="rest_code_27f66539bcdd4afa8277aa02dff98f11-8" name="rest_code_27f66539bcdd4afa8277aa02dff98f11-8" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_27f66539bcdd4afa8277aa02dff98f11-8"></a>co_varnames (&#39;s&#39;,)       [1/4 match]
<a id="rest_code_27f66539bcdd4afa8277aa02dff98f11-9" name="rest_code_27f66539bcdd4afa8277aa02dff98f11-9" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_27f66539bcdd4afa8277aa02dff98f11-9"></a>  7           0 LOAD_CONST               0 (None)
<a id="rest_code_27f66539bcdd4afa8277aa02dff98f11-10" name="rest_code_27f66539bcdd4afa8277aa02dff98f11-10" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_27f66539bcdd4afa8277aa02dff98f11-10"></a>              3 RETURN_VALUE
</pre></div>
<p>We can see (with the help of colors, not reproduced here), that we’ve got <code class="docutils literal">co_argcount</code>, <code class="docutils literal">co_flags</code>, <code class="docutils literal">co_name</code> correctly. We also have one constant (<code class="docutils literal">None</code>, in every function) and one variable name (<code class="docutils literal">s</code>, the argument name). We can also see <code class="docutils literal">dis.dis()</code> output. While it looks similar to the assignment, there are a few noticeable differences: there is no <code class="docutils literal">7</code> (line number) at the start, and <code class="docutils literal">LOAD_CONST</code> instructions in the original code did not have anything in parentheses (only comparisions and loops did).  This makes reading bytecode harder, but still possible. (I originally thought about using <code class="docutils literal">diff</code> for help, but it’s not hard to do it by hand. I did use <code class="docutils literal">diff</code> for the final checking after a manual “conversion”)</p>
<p>Let’s stop to look at the constants and names for a second. The long string is followed by <code class="docutils literal">hex</code>, and one of the constants is <code class="docutils literal">decode</code>. This means that we need to use <code class="docutils literal"><span class="pre">str.decode('hex')</span></code> to create a (byte)string of some information. Puzzle answers tend to be human-readable, and this string isn’t — so we need to do some more work.</p>
<p>So, let’s try reproducing the start of the original mission code using what we’ve just discussed. Python’s VM is based on a stack. In the bytecode above, you can see that instructions take 0 or 1 arguments. Some of them put things on the stack, others do actions and remove them. Most instruction names are self-explanatory, but the full list can be found in the <a class="reference external" href="https://docs.python.org/2/library/dis.html#python-bytecode-instructions">dis module documentation</a>.</p>
<p>Instructions like <code class="docutils literal">LOAD</code> and <code class="docutils literal">STORE</code> refer to indices in the constants/names/varnames tuples. To make it easier, here’s a “table” of them:</p>
<div class="code"><pre class="code text"><a id="rest_code_8c089b4cab264b4f851b62c686149db7-1" name="rest_code_8c089b4cab264b4f851b62c686149db7-1" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_8c089b4cab264b4f851b62c686149db7-1"></a>constants
<a id="rest_code_8c089b4cab264b4f851b62c686149db7-2" name="rest_code_8c089b4cab264b4f851b62c686149db7-2" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_8c089b4cab264b4f851b62c686149db7-2"></a> 0     1                                                       2      3   4    5    6
<a id="rest_code_8c089b4cab264b4f851b62c686149db7-3" name="rest_code_8c089b4cab264b4f851b62c686149db7-3" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_8c089b4cab264b4f851b62c686149db7-3"></a>(None, &#39;4e5d4e92865a4e495a86494b5a5d49525261865f5758534d4a89&#39;, &#39;hex&#39;, 89, 255, 115, 50)
<a id="rest_code_8c089b4cab264b4f851b62c686149db7-4" name="rest_code_8c089b4cab264b4f851b62c686149db7-4" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_8c089b4cab264b4f851b62c686149db7-4"></a>
<a id="rest_code_8c089b4cab264b4f851b62c686149db7-5" name="rest_code_8c089b4cab264b4f851b62c686149db7-5" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_8c089b4cab264b4f851b62c686149db7-5"></a>names (globals, attributes)
<a id="rest_code_8c089b4cab264b4f851b62c686149db7-6" name="rest_code_8c089b4cab264b4f851b62c686149db7-6" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_8c089b4cab264b4f851b62c686149db7-6"></a> 0         1      2        3      4      5
<a id="rest_code_8c089b4cab264b4f851b62c686149db7-7" name="rest_code_8c089b4cab264b4f851b62c686149db7-7" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_8c089b4cab264b4f851b62c686149db7-7"></a>(&#39;decode&#39;, &#39;len&#39;, &#39;False&#39;, &#39;all&#39;, &#39;zip&#39;, &#39;ord&#39;)
<a id="rest_code_8c089b4cab264b4f851b62c686149db7-8" name="rest_code_8c089b4cab264b4f851b62c686149db7-8" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_8c089b4cab264b4f851b62c686149db7-8"></a>
<a id="rest_code_8c089b4cab264b4f851b62c686149db7-9" name="rest_code_8c089b4cab264b4f851b62c686149db7-9" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_8c089b4cab264b4f851b62c686149db7-9"></a>varnames (locals, _fast)
<a id="rest_code_8c089b4cab264b4f851b62c686149db7-10" name="rest_code_8c089b4cab264b4f851b62c686149db7-10" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_8c089b4cab264b4f851b62c686149db7-10"></a> 0    1       2     3
<a id="rest_code_8c089b4cab264b4f851b62c686149db7-11" name="rest_code_8c089b4cab264b4f851b62c686149db7-11" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_8c089b4cab264b4f851b62c686149db7-11"></a>(&#39;s&#39;, &#39;good&#39;, &#39;cs&#39;, &#39;cg&#39;)
</pre></div>
<p>In order to improve readability, I will use “new” <code class="docutils literal">dis</code> output with names in parentheses below:</p>
<div class="code"><pre class="code text"><a id="rest_code_fb6ce8efa1624395b2049e1148156b1c-1" name="rest_code_fb6ce8efa1624395b2049e1148156b1c-1" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_fb6ce8efa1624395b2049e1148156b1c-1"></a> 0 LOAD_CONST               1 (&#39;4e5d4e92865a4e495a86494b5a5d49525261865f5758534d4a89&#39;)
<a id="rest_code_fb6ce8efa1624395b2049e1148156b1c-2" name="rest_code_fb6ce8efa1624395b2049e1148156b1c-2" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_fb6ce8efa1624395b2049e1148156b1c-2"></a> 3 LOAD_ATTR                0 (decode)
<a id="rest_code_fb6ce8efa1624395b2049e1148156b1c-3" name="rest_code_fb6ce8efa1624395b2049e1148156b1c-3" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_fb6ce8efa1624395b2049e1148156b1c-3"></a> 6 LOAD_CONST               2 (&#39;hex&#39;)
<a id="rest_code_fb6ce8efa1624395b2049e1148156b1c-4" name="rest_code_fb6ce8efa1624395b2049e1148156b1c-4" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_fb6ce8efa1624395b2049e1148156b1c-4"></a> 9 CALL_FUNCTION            1 # function takes 1 argument from stack
<a id="rest_code_fb6ce8efa1624395b2049e1148156b1c-5" name="rest_code_fb6ce8efa1624395b2049e1148156b1c-5" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_fb6ce8efa1624395b2049e1148156b1c-5"></a>12 STORE_FAST               1 (good)
</pre></div>
<p>As I guessed before, the first line of our function is as follows:</p>
<div class="code"><pre class="code python"><a id="rest_code_85beff924755471e8039dad55a327578-1" name="rest_code_85beff924755471e8039dad55a327578-1" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_85beff924755471e8039dad55a327578-1"></a><span class="k">def</span><span class="w"> </span><span class="nf">check_password</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
<a id="rest_code_85beff924755471e8039dad55a327578-2" name="rest_code_85beff924755471e8039dad55a327578-2" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_85beff924755471e8039dad55a327578-2"></a>    <span class="n">good</span> <span class="o">=</span> <span class="s1">&#39;4e5d4e92865a4e495a86494b5a5d49525261865f5758534d4a89&#39;</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;hex&#39;</span><span class="p">)</span>  <span class="c1"># new</span>
</pre></div>
<p>If we run the solver again, we’ll see that the first 12 bytes of our bytecode match the mission text. We can also see that <code class="docutils literal">varnames</code> is filled in half, we’ve added two constants, and one name.  The next few lines are as follows:</p>
<div class="code"><pre class="code text"><a id="rest_code_506359919e4b49e6b2cd2712f3301286-1" name="rest_code_506359919e4b49e6b2cd2712f3301286-1" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_506359919e4b49e6b2cd2712f3301286-1"></a>15 LOAD_GLOBAL              1
<a id="rest_code_506359919e4b49e6b2cd2712f3301286-2" name="rest_code_506359919e4b49e6b2cd2712f3301286-2" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_506359919e4b49e6b2cd2712f3301286-2"></a>18 LOAD_FAST                0
<a id="rest_code_506359919e4b49e6b2cd2712f3301286-3" name="rest_code_506359919e4b49e6b2cd2712f3301286-3" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_506359919e4b49e6b2cd2712f3301286-3"></a>21 CALL_FUNCTION            1
<a id="rest_code_506359919e4b49e6b2cd2712f3301286-4" name="rest_code_506359919e4b49e6b2cd2712f3301286-4" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_506359919e4b49e6b2cd2712f3301286-4"></a>24 LOAD_GLOBAL              1
<a id="rest_code_506359919e4b49e6b2cd2712f3301286-5" name="rest_code_506359919e4b49e6b2cd2712f3301286-5" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_506359919e4b49e6b2cd2712f3301286-5"></a>27 LOAD_FAST                1
<a id="rest_code_506359919e4b49e6b2cd2712f3301286-6" name="rest_code_506359919e4b49e6b2cd2712f3301286-6" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_506359919e4b49e6b2cd2712f3301286-6"></a>30 CALL_FUNCTION            1
<a id="rest_code_506359919e4b49e6b2cd2712f3301286-7" name="rest_code_506359919e4b49e6b2cd2712f3301286-7" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_506359919e4b49e6b2cd2712f3301286-7"></a>33 COMPARE_OP               3 (!=)
<a id="rest_code_506359919e4b49e6b2cd2712f3301286-8" name="rest_code_506359919e4b49e6b2cd2712f3301286-8" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_506359919e4b49e6b2cd2712f3301286-8"></a>36 POP_JUMP_IF_FALSE       43
<a id="rest_code_506359919e4b49e6b2cd2712f3301286-9" name="rest_code_506359919e4b49e6b2cd2712f3301286-9" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_506359919e4b49e6b2cd2712f3301286-9"></a>39 LOAD_GLOBAL              2
<a id="rest_code_506359919e4b49e6b2cd2712f3301286-10" name="rest_code_506359919e4b49e6b2cd2712f3301286-10" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_506359919e4b49e6b2cd2712f3301286-10"></a>42 RETURN_VALUE
</pre></div>
<p>We can see that we’re putting a global object on stack and calling it with one argument. In both cases, the global has the index 1, that’s <code class="docutils literal">len</code>. The two arguments are <code class="docutils literal">s</code> and <code class="docutils literal">good</code>. We put both lengths on stack, then compare them. If the comparison fails (they’re equal), we jump to the instruction starting at byte 43, otherwise we continue execution to load the second global (False) and return it.  This wall of text translates to the following simple code:</p>
<div class="code"><pre class="code python"><a id="rest_code_ff50cca92832460f9863e211d84e2c63-1" name="rest_code_ff50cca92832460f9863e211d84e2c63-1" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ff50cca92832460f9863e211d84e2c63-1"></a><span class="k">def</span><span class="w"> </span><span class="nf">check_password</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
<a id="rest_code_ff50cca92832460f9863e211d84e2c63-2" name="rest_code_ff50cca92832460f9863e211d84e2c63-2" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ff50cca92832460f9863e211d84e2c63-2"></a>    <span class="n">good</span> <span class="o">=</span> <span class="s1">&#39;4e5d4e92865a4e495a86494b5a5d49525261865f5758534d4a89&#39;</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;hex&#39;</span><span class="p">)</span>
<a id="rest_code_ff50cca92832460f9863e211d84e2c63-3" name="rest_code_ff50cca92832460f9863e211d84e2c63-3" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ff50cca92832460f9863e211d84e2c63-3"></a>    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">good</span><span class="p">):</span>  <span class="c1"># new</span>
<a id="rest_code_ff50cca92832460f9863e211d84e2c63-4" name="rest_code_ff50cca92832460f9863e211d84e2c63-4" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_ff50cca92832460f9863e211d84e2c63-4"></a>        <span class="k">return</span> <span class="kc">False</span>         <span class="c1"># new</span>
</pre></div>
<p>Let’s take another look at our names. We can see we’re missing <code class="docutils literal">all</code>, <code class="docutils literal">zip</code>, <code class="docutils literal">ord</code>. You can already see a common pattern here: we will iterate over both strings at once (using <code class="docutils literal">zip</code>), do some math based on the character’s codes (<code class="docutils literal">ord</code>), and then check if <code class="docutils literal">all</code> results (of a comparison, usually) are truthy.</p>
<p>Here’s the bytecode with value annotations and comments, which explain what happens where:</p>
<div class="code"><pre class="code text"><a id="rest_code_600c535191904a66bf231f1cfe9efc77-1" name="rest_code_600c535191904a66bf231f1cfe9efc77-1" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-1"></a>&gt;&gt;   43 LOAD_GLOBAL              3 (all)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-2" name="rest_code_600c535191904a66bf231f1cfe9efc77-2" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-2"></a>     46 BUILD_LIST               0
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-3" name="rest_code_600c535191904a66bf231f1cfe9efc77-3" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-3"></a>     49 LOAD_GLOBAL              4 (zip)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-4" name="rest_code_600c535191904a66bf231f1cfe9efc77-4" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-4"></a>     52 LOAD_FAST                0 (s)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-5" name="rest_code_600c535191904a66bf231f1cfe9efc77-5" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-5"></a>     55 LOAD_FAST                1 (good)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-6" name="rest_code_600c535191904a66bf231f1cfe9efc77-6" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-6"></a>     58 CALL_FUNCTION            2           # zip(s, good)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-7" name="rest_code_600c535191904a66bf231f1cfe9efc77-7" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-7"></a>     61 GET_ITER                             # Start iterating: iter()
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-8" name="rest_code_600c535191904a66bf231f1cfe9efc77-8" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-8"></a>&gt;&gt;   62 FOR_ITER                52 (to 117)  # for loop iteration start (if iterator exhausted, jump +52 bytes to position 117)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-9" name="rest_code_600c535191904a66bf231f1cfe9efc77-9" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-9"></a>     65 UNPACK_SEQUENCE          2           # unpack a sequence (a, b = sequence)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-10" name="rest_code_600c535191904a66bf231f1cfe9efc77-10" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-10"></a>     68 STORE_FAST               2 (cs)      # cs = item from s
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-11" name="rest_code_600c535191904a66bf231f1cfe9efc77-11" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-11"></a>     71 STORE_FAST               3 (cg)      # cg = item from good
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-12" name="rest_code_600c535191904a66bf231f1cfe9efc77-12" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-12"></a>     74 LOAD_GLOBAL              5 (ord)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-13" name="rest_code_600c535191904a66bf231f1cfe9efc77-13" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-13"></a>     77 LOAD_FAST                2 (cs)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-14" name="rest_code_600c535191904a66bf231f1cfe9efc77-14" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-14"></a>     80 CALL_FUNCTION            1           # put ord(cs) on stack
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-15" name="rest_code_600c535191904a66bf231f1cfe9efc77-15" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-15"></a>     83 LOAD_CONST               3 (89)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-16" name="rest_code_600c535191904a66bf231f1cfe9efc77-16" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-16"></a>     86 BINARY_SUBTRACT                      # - 89   [subtract 89 from topmost value]
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-17" name="rest_code_600c535191904a66bf231f1cfe9efc77-17" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-17"></a>     87 LOAD_CONST               4 (255)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-18" name="rest_code_600c535191904a66bf231f1cfe9efc77-18" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-18"></a>     90 BINARY_AND                           # &amp; 255  [bitwise AND with topmost value]
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-19" name="rest_code_600c535191904a66bf231f1cfe9efc77-19" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-19"></a>     91 LOAD_CONST               5 (115)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-20" name="rest_code_600c535191904a66bf231f1cfe9efc77-20" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-20"></a>     94 BINARY_XOR                           # ^ 115  [bitwise XOR with topmost value]
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-21" name="rest_code_600c535191904a66bf231f1cfe9efc77-21" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-21"></a>     95 LOAD_CONST               6 (50)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-22" name="rest_code_600c535191904a66bf231f1cfe9efc77-22" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-22"></a>     98 BINARY_XOR                           # ^ 50   [bitwise XOR with topmost value]
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-23" name="rest_code_600c535191904a66bf231f1cfe9efc77-23" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-23"></a>     99 LOAD_GLOBAL              5 (ord)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-24" name="rest_code_600c535191904a66bf231f1cfe9efc77-24" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-24"></a>    102 LOAD_FAST                3 (cg)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-25" name="rest_code_600c535191904a66bf231f1cfe9efc77-25" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-25"></a>    105 CALL_FUNCTION            1           # put ord(cs) on stack
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-26" name="rest_code_600c535191904a66bf231f1cfe9efc77-26" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-26"></a>    108 COMPARE_OP               2 (==)      # compare the two values on stack
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-27" name="rest_code_600c535191904a66bf231f1cfe9efc77-27" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-27"></a>    111 LIST_APPEND              2           # append topmost value to the list in topmost-1; pop topmost (append to list created in comprehension)
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-28" name="rest_code_600c535191904a66bf231f1cfe9efc77-28" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-28"></a>    114 JUMP_ABSOLUTE           62           # jump back to start of loop
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-29" name="rest_code_600c535191904a66bf231f1cfe9efc77-29" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-29"></a>&gt;&gt;  117 CALL_FUNCTION            1           # after loop: call all([list comprehension result])
<a id="rest_code_600c535191904a66bf231f1cfe9efc77-30" name="rest_code_600c535191904a66bf231f1cfe9efc77-30" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_600c535191904a66bf231f1cfe9efc77-30"></a>    120 RETURN_VALUE                         # return value returned by all()
</pre></div>
<p>We can now write the full answer.</p>
<p><a class="reference external" href="link://listing/listings/gynvaels-mission-11-en/mission11.py">listings/gynvaels-mission-11-en/mission11.py</a>  <a class="reference external" href="link://listing_source/listings/gynvaels-mission-11-en/mission11.py">(Source)</a></p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_937a75fde167457d96c306bb9cde4520-1"><code data-line-number="1"></code></a></td><td class="code"><code><a id="rest_code_937a75fde167457d96c306bb9cde4520-1" name="rest_code_937a75fde167457d96c306bb9cde4520-1"></a><span class="k">def</span><span class="w"> </span><span class="nf">check_password</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_937a75fde167457d96c306bb9cde4520-2"><code data-line-number="2"></code></a></td><td class="code"><code><a id="rest_code_937a75fde167457d96c306bb9cde4520-2" name="rest_code_937a75fde167457d96c306bb9cde4520-2"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">good</span> <span class="o">=</span> <span class="s1">&#39;4e5d4e92865a4e495a86494b5a5d49525261865f5758534d4a89&#39;</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;hex&#39;</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_937a75fde167457d96c306bb9cde4520-3"><code data-line-number="3"></code></a></td><td class="code"><code><a id="rest_code_937a75fde167457d96c306bb9cde4520-3" name="rest_code_937a75fde167457d96c306bb9cde4520-3"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">good</span><span class="p">):</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_937a75fde167457d96c306bb9cde4520-4"><code data-line-number="4"></code></a></td><td class="code"><code><a id="rest_code_937a75fde167457d96c306bb9cde4520-4" name="rest_code_937a75fde167457d96c306bb9cde4520-4"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="kc">False</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_937a75fde167457d96c306bb9cde4520-5"><code data-line-number="5"></code></a></td><td class="code"><code><a id="rest_code_937a75fde167457d96c306bb9cde4520-5" name="rest_code_937a75fde167457d96c306bb9cde4520-5"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_937a75fde167457d96c306bb9cde4520-6"><code data-line-number="6"></code></a></td><td class="code"><code><a id="rest_code_937a75fde167457d96c306bb9cde4520-6" name="rest_code_937a75fde167457d96c306bb9cde4520-6"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nb">all</span><span class="p">([</span><span class="nb">ord</span><span class="p">(</span><span class="n">cs</span><span class="p">)</span> <span class="o">-</span> <span class="mi">89</span> <span class="o">&amp;</span> <span class="mi">255</span> <span class="o">^</span> <span class="mi">115</span> <span class="o">^</span> <span class="mi">50</span> <span class="o">==</span> <span class="nb">ord</span><span class="p">(</span><span class="n">cg</span><span class="p">)</span> <span class="k">for</span> <span class="n">cs</span><span class="p">,</span> <span class="n">cg</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">good</span><span class="p">)])</span>
</code></td></tr></table></div><p>In the end, our <code class="docutils literal">dis.dis()</code> output matches the mission text (except the removed values, but their IDs do match), our <code class="docutils literal">co_*</code> variables are all green, and we can get to work on solving the puzzle itself!</p>
<p><strong>Side note:</strong> this task uses a list comprehension. You might want to optimize it, remove the brackets, and end up with a generator expression. This would make the task harder, since would require working with the internal generator code object as well:</p>
<div class="code"><pre class="code text"><a id="rest_code_2486792360754ae283652134d687e43c-1" name="rest_code_2486792360754ae283652134d687e43c-1" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_2486792360754ae283652134d687e43c-1"></a>co_consts (None, &#39;4e5d4e92865a4e495a86494b5a5d49525261865f5758534d4a89&#39;, &#39;hex&#39;, &lt;code object &lt;genexpr&gt; at 0x104a86c30, file &quot;mission11-genexpr.py&quot;, line 11&gt;)
<a id="rest_code_2486792360754ae283652134d687e43c-2" name="rest_code_2486792360754ae283652134d687e43c-2" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_2486792360754ae283652134d687e43c-2"></a>
<a id="rest_code_2486792360754ae283652134d687e43c-3" name="rest_code_2486792360754ae283652134d687e43c-3" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_2486792360754ae283652134d687e43c-3"></a>46 LOAD_CONST               3 (&lt;code object &lt;genexpr&gt; at 0x104a86c30, file &quot;mission11-genexpr.py&quot;, line 11&gt;)
</pre></div>
<p><code class="docutils literal">BINARY_*</code> and <code class="docutils literal">ord</code> disappeared from the new listing. You can see the <a class="reference external" href="https://chriswarrick.com/listings/gynvaels-mission-11-en/mission11-genexpr.py.html">modified code</a> (which differs by two bytes) and <a class="reference external" href="https://chriswarrick.com/listings/gynvaels-mission-11-en/mission11-genexpr.txt.html">solver output</a>.</p>
</section>
<section id="solving-the-real-puzzle">
<h1>Solving the real puzzle</h1>
<p>I solved the extra credit part of the puzzle. The <em>real</em> aim of the puzzle was to recover the password — the text for which <code class="docutils literal">check_password()</code> will return True.</p>
<p>This part is pretty boring. I built a dictionary, where I mapped every byte (0…255) to the result of the calculation done in the <code class="docutils literal">check_password()</code> function’s loop. Then I used that to recover the original text.</p>
<div class="code"><pre class="code python"><a id="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-1" name="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-1" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_0f5c1acbabcc4365a8eccce0dc13f132-1"></a><span class="n">pass_values</span> <span class="o">=</span> <span class="p">{}</span>
<a id="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-2" name="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-2" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_0f5c1acbabcc4365a8eccce0dc13f132-2"></a><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">256</span><span class="p">):</span>
<a id="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-3" name="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-3" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_0f5c1acbabcc4365a8eccce0dc13f132-3"></a>    <span class="n">result</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">89</span> <span class="o">&amp;</span> <span class="mi">255</span> <span class="o">^</span> <span class="mi">115</span> <span class="o">^</span> <span class="mi">50</span>
<a id="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-4" name="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-4" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_0f5c1acbabcc4365a8eccce0dc13f132-4"></a>    <span class="n">pass_values</span><span class="p">[</span><span class="n">result</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span>
<a id="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-5" name="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-5" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_0f5c1acbabcc4365a8eccce0dc13f132-5"></a>
<a id="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-6" name="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-6" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_0f5c1acbabcc4365a8eccce0dc13f132-6"></a><span class="n">good</span> <span class="o">=</span> <span class="s1">&#39;4e5d4e92865a4e495a86494b5a5d49525261865f5758534d4a89&#39;</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;hex&#39;</span><span class="p">)</span>
<a id="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-7" name="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-7" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_0f5c1acbabcc4365a8eccce0dc13f132-7"></a><span class="n">password</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
<a id="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-8" name="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-8" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_0f5c1acbabcc4365a8eccce0dc13f132-8"></a><span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">good</span><span class="p">:</span>
<a id="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-9" name="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-9" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_0f5c1acbabcc4365a8eccce0dc13f132-9"></a>    <span class="n">password</span> <span class="o">+=</span> <span class="nb">chr</span><span class="p">(</span><span class="n">pass_values</span><span class="p">[</span><span class="nb">ord</span><span class="p">(</span><span class="n">c</span><span class="p">)])</span>
<a id="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-10" name="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-10" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_0f5c1acbabcc4365a8eccce0dc13f132-10"></a>
<a id="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-11" name="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-11" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_0f5c1acbabcc4365a8eccce0dc13f132-11"></a><span class="nb">print</span><span class="p">(</span><span class="n">password</span><span class="p">)</span>
<a id="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-12" name="rest_code_0f5c1acbabcc4365a8eccce0dc13f132-12" href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_0f5c1acbabcc4365a8eccce0dc13f132-12"></a><span class="nb">print</span><span class="p">(</span><span class="n">check_password</span><span class="p">(</span><span class="n">password</span><span class="p">))</span>
</pre></div>
<p><strong>The password is:</strong> <code class="docutils literal">huh, that actually worked!</code>.</p>
</section>
<section id="what-was-that-paint-thing-about">
<h1>What was that Paint thing about?</h1>
<blockquote>Yesterday’s mission was about Elvish — <strong>I mean Paint</strong> — I mean Python programming.<footer>yours truly in this post’s teaser</footer></blockquote><p>Most of my readers were probably puzzled by the mention of Paint. Long-time viewers of Gynvael’s streams in Polish remember the Python 101 video he posted on April Fools last year. See <a class="reference external" href="https://www.youtube.com/watch?v=7VJaprmuHcw">original video</a>, <a class="reference external" href="http://gynvael.coldwind.pl/?id=599">explanation</a>, <a class="reference external" href="https://github.com/gynvael/stream/tree/master/007-python-101">code</a> (video and explanation are both Polish; you can get the gist of the video without hearing the audio commentary though.) <strong>Spoilers ahead.</strong></p>
<p>In that prank, Gynvael taught Python basics. The first part concerned itself with writing bytecode by hand. The second part (starts around 12:00) was about drawing custom Python modules. In Paint. Yes, Paint, the simple graphics program included with Microsoft Windows. He drew a custom Python module in Paint, and saved it using the BMP format. It looked like this (zoomed PNG below; <a class="reference external" href="https://chriswarrick.com/pub/gynvaels-mission-11-en/gynmod.bmp">download gynmod.bmp</a>):</p>
<img alt="/images/gynvaels-mission-11-en/gynmod-zoom.png" class="align-center" src="https://chriswarrick.com/images/gynvaels-mission-11-en/gynmod-zoom.png">
<p>How was this done? There are three things that come into play:</p>
<ul class="simple">
<li><p>Python can import modules from a ZIP file (if it’s appended to sys.path). Some tools that produce <code class="docutils literal">.exe</code> files of Python code use this technique; the old <code class="docutils literal">.egg</code> file format also used ZIPs this way.</p></li>
<li><p>BMP files have their header at the start of a file.</p></li>
<li><p>ZIP files have their header at the end of a file.</p></li>
<li><p>Thus, one file can be a valid BMP and ZIP at the same time.</p></li>
</ul>
<p>I took the code of <code class="docutils literal">check_password</code> and put it in <code class="docutils literal">mission11.py</code> (which I already cited above). Then I compiled to <code class="docutils literal">.pyc</code> and created a <code class="docutils literal">.zip</code> out of it.</p>
<p><a class="reference external" href="link://listing/listings/gynvaels-mission-11-en/mission11.py">listings/gynvaels-mission-11-en/mission11.py</a>  <a class="reference external" href="link://listing_source/listings/gynvaels-mission-11-en/mission11.py">(Source)</a></p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_bec3e104bb384b61922373724d0100a2-1"><code data-line-number="1"></code></a></td><td class="code"><code><a id="rest_code_bec3e104bb384b61922373724d0100a2-1" name="rest_code_bec3e104bb384b61922373724d0100a2-1"></a><span class="k">def</span><span class="w"> </span><span class="nf">check_password</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_bec3e104bb384b61922373724d0100a2-2"><code data-line-number="2"></code></a></td><td class="code"><code><a id="rest_code_bec3e104bb384b61922373724d0100a2-2" name="rest_code_bec3e104bb384b61922373724d0100a2-2"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">good</span> <span class="o">=</span> <span class="s1">&#39;4e5d4e92865a4e495a86494b5a5d49525261865f5758534d4a89&#39;</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;hex&#39;</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_bec3e104bb384b61922373724d0100a2-3"><code data-line-number="3"></code></a></td><td class="code"><code><a id="rest_code_bec3e104bb384b61922373724d0100a2-3" name="rest_code_bec3e104bb384b61922373724d0100a2-3"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">good</span><span class="p">):</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_bec3e104bb384b61922373724d0100a2-4"><code data-line-number="4"></code></a></td><td class="code"><code><a id="rest_code_bec3e104bb384b61922373724d0100a2-4" name="rest_code_bec3e104bb384b61922373724d0100a2-4"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="kc">False</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_bec3e104bb384b61922373724d0100a2-5"><code data-line-number="5"></code></a></td><td class="code"><code><a id="rest_code_bec3e104bb384b61922373724d0100a2-5" name="rest_code_bec3e104bb384b61922373724d0100a2-5"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_bec3e104bb384b61922373724d0100a2-6"><code data-line-number="6"></code></a></td><td class="code"><code><a id="rest_code_bec3e104bb384b61922373724d0100a2-6" name="rest_code_bec3e104bb384b61922373724d0100a2-6"></a>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="nb">all</span><span class="p">([</span><span class="nb">ord</span><span class="p">(</span><span class="n">cs</span><span class="p">)</span> <span class="o">-</span> <span class="mi">89</span> <span class="o">&amp;</span> <span class="mi">255</span> <span class="o">^</span> <span class="mi">115</span> <span class="o">^</span> <span class="mi">50</span> <span class="o">==</span> <span class="nb">ord</span><span class="p">(</span><span class="n">cg</span><span class="p">)</span> <span class="k">for</span> <span class="n">cs</span><span class="p">,</span> <span class="n">cg</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">good</span><span class="p">)])</span>
</code></td></tr></table></div><p>Since I’m not an expert in any of the formats, I booted my Windows virtual machine and blindly copied the <a class="reference external" href="http://gynvael.coldwind.pl/img/secapr16_3.png">parameters used by Gynvael</a> to open the ZIP file (renamed <code class="docutils literal">.raw</code>) in IrfanView and saved as <code class="docutils literal">.bmp</code>. I changed the size to 83×2, because my ZIP file was 498 bytes long (3 BPP * 83 px * 2 px = 498 bytes) — by doing that, and through sheer luck with the size, I could avoid adding comments and editing the ZIP archive. I ended up with this (PNG again; <a class="reference external" href="https://chriswarrick.com/pub/gynvaels-mission-11-en/mission11.bmp">download mission11.bmp</a>):</p>
<img alt="/images/gynvaels-mission-11-en/mission11-zoom.png" class="align-center" src="https://chriswarrick.com/images/gynvaels-mission-11-en/mission11-zoom.png">
<p>The <code class="docutils literal">.bmp</code> file is runnable! We can use this code:</p>
<p><a class="reference external" href="link://listing/listings/gynvaels-mission-11-en/ziprunner.py">listings/gynvaels-mission-11-en/ziprunner.py</a>  <a class="reference external" href="link://listing_source/listings/gynvaels-mission-11-en/ziprunner.py">(Source)</a></p>
<div class="code"><table class="codetable"><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_2c416583ffef4a72a08094db61f84464-1"><code data-line-number="1"></code></a></td><td class="code"><code><a id="rest_code_2c416583ffef4a72a08094db61f84464-1" name="rest_code_2c416583ffef4a72a08094db61f84464-1"></a><span class="ch">#!/usr/bin/env python2</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_2c416583ffef4a72a08094db61f84464-2"><code data-line-number="2"></code></a></td><td class="code"><code><a id="rest_code_2c416583ffef4a72a08094db61f84464-2" name="rest_code_2c416583ffef4a72a08094db61f84464-2"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_2c416583ffef4a72a08094db61f84464-3"><code data-line-number="3"></code></a></td><td class="code"><code><a id="rest_code_2c416583ffef4a72a08094db61f84464-3" name="rest_code_2c416583ffef4a72a08094db61f84464-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">sys</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_2c416583ffef4a72a08094db61f84464-4"><code data-line-number="4"></code></a></td><td class="code"><code><a id="rest_code_2c416583ffef4a72a08094db61f84464-4" name="rest_code_2c416583ffef4a72a08094db61f84464-4"></a><span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;mission11.bmp&quot;</span><span class="p">)</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_2c416583ffef4a72a08094db61f84464-5"><code data-line-number="5"></code></a></td><td class="code"><code><a id="rest_code_2c416583ffef4a72a08094db61f84464-5" name="rest_code_2c416583ffef4a72a08094db61f84464-5"></a>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_2c416583ffef4a72a08094db61f84464-6"><code data-line-number="6"></code></a></td><td class="code"><code><a id="rest_code_2c416583ffef4a72a08094db61f84464-6" name="rest_code_2c416583ffef4a72a08094db61f84464-6"></a><span class="kn">import</span><span class="w"> </span><span class="nn">mission11</span>
</code></td></tr><tr><td class="linenos linenodiv"><a href="https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/#rest_code_2c416583ffef4a72a08094db61f84464-7"><code data-line-number="7"></code></a></td><td class="code"><code><a id="rest_code_2c416583ffef4a72a08094db61f84464-7" name="rest_code_2c416583ffef4a72a08094db61f84464-7"></a><span class="nb">print</span> <span class="s2">&quot;Result:&quot;</span><span class="p">,</span> <span class="n">mission11</span><span class="o">.</span><span class="n">check_password</span><span class="p">(</span><span class="s1">&#39;huh, that actually worked!&#39;</span><span class="p">)</span>
</code></td></tr></table></div><p>And we get this:</p>
<img alt="/images/gynvaels-mission-11-en/running-bmp.png" class="align-center" src="https://chriswarrick.com/images/gynvaels-mission-11-en/running-bmp.png">
</section>
<section id="resources">
<h1>Resources</h1>
<ul class="simple">
<li><p><a class="reference external" href="https://chriswarrick.com/listings/gynvaels-mission-11-en/mission11-solver.py.html">mission11-solver.py (full solver code)</a></p></li>
<li><p><a class="reference external" href="https://chriswarrick.com/listings/gynvaels-mission-11-en/mission11-genexpr.py.html">mission11-genexpr.py</a>, <a class="reference external" href="https://chriswarrick.com/listings/gynvaels-mission-11-en/mission11-genexpr.txt.html">mission11-genexpr.txt</a> (used for side note regarding generator expressions vs. list comprehensions)</p></li>
<li><p><a class="reference external" href="https://chriswarrick.com/listings/gynvaels-mission-11-en/mission11.py.html">mission11.py code, used in BMP file</a></p></li>
<li><p><a class="reference external" href="https://chriswarrick.com/listings/gynvaels-mission-11-en/ziprunner.py.html">ziprunner.py, file that runs the BMP/ZIP module</a> (adapted from Gynvael’s)</p></li>
<li><p><a class="reference external" href="https://chriswarrick.com/pub/gynvaels-mission-11-en/gynmod.bmp">gynmod.bmp</a></p></li>
<li><p><a class="reference external" href="https://chriswarrick.com/pub/gynvaels-mission-11-en/mission11.bmp">mission11.bmp</a></p></li>
<li><p><a class="reference external" href="https://docs.python.org/2/library/dis.html#python-bytecode-instructions">dis module documentation</a>.</p></li>
</ul>
<p>Thanks for the mission (and BMP idea), Gynvael!</p>
</section>
]]></content:encoded><category>Python</category><category>BMP</category><category>Gynvael Coldwind</category><category>hacking</category><category>Paint</category><category>Python</category><category>Python hackery</category><category>Python internals</category><category>reverse engineering</category><category>writeup</category></item></channel></rss>