<?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 PDM</title><link>https://chriswarrick.com/</link><atom:link href="https://chriswarrick.com/blog/tags/pdm.xml" rel="self" type="application/rss+xml" /><description>A rarely updated blog, mostly about programming.</description><lastBuildDate>Mon, 15 Jan 2024 18:50:00 GMT</lastBuildDate><generator>https://github.com/Kwpolska/YetAnotherBlogGenerator</generator><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></channel></rss>