TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Boring Python: dependency management (2022)

114 pointsby bruh2over 1 year ago

17 comments

irjustinover 1 year ago
With the move to pyproject.toml in 2022[0], Poetry has become our goto method.<p>With the lock file being default, we don&#x27;t worry about different installs in different envs.<p>Having come from the Rails world, Bundler system was solved for... a decade? So I was surprised it was such a mess in Python until so recently.<p>At the core, the thing that makes Poetry and Bundler system so predictable is 1, lock file. 2, ability to install different versions in diff locations and referencing the version you need to load. Each alone isn&#x27;t enough.<p>npm had the same problem pip suffered from, you may have a version installed different what the req.txt, proj.js or even lockfile says but because it exists inside the location, it gets loaded. It wasn&#x27;t until yarn2 did node_modules finally get moved out such that side-by-side versions wasn&#x27;t awkward.<p>[EDIT]<p>If you&#x27;re not using Poetry + Docker for deployment yet, I 100% recommend it as the &quot;boring&quot; method.<p><pre><code> RUN curl -sSL https:&#x2F;&#x2F;install.python-poetry.org | python - RUN &#x2F;root&#x2F;.local&#x2F;bin&#x2F;poetry config virtualenvs.create false COPY poetry.lock pyproject.toml .&#x2F; RUN --mount=type=secret,id=gh_priv_key,target=&#x2F;root&#x2F;.ssh&#x2F;id_rsa \ &#x2F;root&#x2F;.local&#x2F;bin&#x2F;poetry install --without dev --no-root </code></pre> [0] <a href="https:&#x2F;&#x2F;packaging.python.org&#x2F;en&#x2F;latest&#x2F;tutorials&#x2F;packaging-projects&#x2F;" rel="nofollow">https:&#x2F;&#x2F;packaging.python.org&#x2F;en&#x2F;latest&#x2F;tutorials&#x2F;packaging-p...</a>
评论 #39256967 未加载
评论 #39258069 未加载
评论 #39259033 未加载
评论 #39258770 未加载
评论 #39256662 未加载
评论 #39259800 未加载
评论 #39258671 未加载
评论 #39259913 未加载
评论 #39256839 未加载
评论 #39260887 未加载
评论 #39275867 未加载
mattbillensteinover 1 year ago
The python core team endorses pip, but pip solves something like 80% of the problem - we need them to endorse the other 20% of the solution.<p>I&#x27;ve been using pip-tools for some time, I think it solves the other 20% of the problem for me in a simple way that I like. Poetry et al seem to be trying to do too much - ymmv.<p>The iterations on packaging that don&#x27;t really seem to ever get it right are, I think, frustrating to the community where the core likes to advertise a &quot;Zen of Python&quot; &quot;one way to do things&quot; mantra, but can never really get 100% of the packaging problem sorted out in a clean way in spite of several communities seeming to figure it out.
评论 #39257021 未加载
评论 #39259552 未加载
sebraover 1 year ago
For me it feels like people have always been over-engineering dependency management where the practical issues from not doing it are pretty much non-existent.<p>My approach is to just use Docker, no virtualenvs. I get that you might run into the multiple interpreters issue in theory but across multiple projects in the past 5 years I haven&#x27;t seen that. Also, this might no longer be true but avoid using Alpine. If you&#x27;re deploying Django there is no reason to optimize image size and Alpine has a lot of things which are missing (i.e. at least a couple of years ago, wheels where not supported leading very slow build times).<p>I only do a single requirements.txt. Anything which makes your prod and local environment differ is a bad thing in my opinion. Fine black might make my image a couple of mbs larger but why would it matter? On the other hand attempting to figure out why something which works on my machine does not work on prod is always a nightmare.<p>Setting requirements as a range in requirements.txt allows me to automatically get bugfixes without spending time on it (e.g. django&gt;=4.2.3,&lt;4.2.99 django-ninja&gt;=1.0.1,&lt;1.0.99) Again, I might have run into 1-2 issues over the past couple of years from this and I&#x27;ve saved a lot of time.<p>Getting a project running locally should not take more than 1 minute (a couple of .env vars + docker-compose up -d should be enough).<p>The biggest practical issue in dependency management in python is dependencies not pinning their dependencies correctly.
评论 #39260667 未加载
globular-toastover 1 year ago
Here&#x27;s my &quot;boring&quot; workflow:<p>1. Start project (mkdir, git init),<p>2. Make virtualenv using virtualenvwrapper,<p>3. Write project.toml file for setuptools,<p>4. pip install -e .<p>5. To add deps, add them to pyproject.toml and repeat step 4. <i>Do not pip install deps directly</i>. Do not pin deps to any particular version, but if you have to you can add constraints like &gt;=5 (I need a feature introduced in v5).<p>6. If you are writing a package to be pip installed by others then you&#x27;re done. Read setuptools docs for how to build etc.<p>7. If you also want to build an <i>environment</i> to run your code (e.g. docker image for deployment or serverless deployment etc) use pip-tools to pin your dependencies. (This is the only reason you need requirements.txt).<p>8. For test dependencies (e.g. pytest) or dev dependencies (e.g. test server) leverage optional dependencies in the pyproject.toml file. This plays very nicely with tools like tox, which you should use. Use pre-commit for linting etc.
Humphreyover 1 year ago
I&#x27;ve landed on Poetry after having tried many different options over the years. Being able to specify my relatively open dependencies (Eg &quot;django==4.0.*&quot;) but having the exact version of every subdependency versioned has proved to be very reliable and reproducable. Docker multistage builds allow me to ship a production container without Poetry installed.
评论 #39257328 未加载
djsteinover 1 year ago
I’m happy others are writing on this subject! I appreciate your enthusiasm for trying to do the most “basic” things in Python. While I personally enjoy a bit more management with tools like Poetry, I believe all Python programmers should know how pip and setuptools work before trying their supersets.<p>To add to this discussion, I recently wrote this less wordy guide on macOS Python setup <a href="https:&#x2F;&#x2F;steins.studio&#x2F;technical&#x2F;01-python-setup" rel="nofollow">https:&#x2F;&#x2F;steins.studio&#x2F;technical&#x2F;01-python-setup</a>
评论 #39266876 未加载
posix_monadover 1 year ago
The &quot;boring&quot; set of tools described here does not enable reproducible artefacts. This is a huge weakness of the ecosystem.
评论 #39190478 未加载
评论 #39256207 未加载
dagwover 1 year ago
I&#x27;m personally afraid that the python community has bifurcated so far that a single &#x27;correct&#x27; solution is doomed to fail. The needs of people writing and deploying HTTP&#x2F;REST servers is just so different from the people writing PyTorch models and numerical simulations that no tool will ever satisfy the very different needs of both camps. The worst part is that many people developing these tools don&#x27;t seem to realise this and blindly claim that their tool is The Tool! without having any deep insights into the needs of the other camps.
评论 #39259120 未加载
atoavover 1 year ago
The biggest problem about Pythons dependency managment in 2024 is that it stil feels like an afterthought. It is just not straightforward and it is not <i>pythonic</i>. I would go as far as saying that dependency managment with Python is likely more complicated than anything you would normally encounter within the language.<p>As of 2024 poetry is the best solution we have, but even it can come to its limits at times. I work in a position where I develope with poetry and have to deploy without it (using venv), and I do not wish the journey of learning how to do that on anybody.
ildjarnover 1 year ago
Has anyone figured out to how to “cross-compile” Python?<p>By this I mean creating an app bundle that contains the dependencies but for another platform than the one we are bundling on.
评论 #39258922 未加载
EdwardDiegoover 1 year ago
The biggest problem in Python dep management is Pip. When upgrading pip breaks tools wrapping&#x2F;integrating with it, it&#x27;s a bad time.<p>I&#x27;ve had to pin pip itself a few times due to resolution that used to work failing, and sometimes there&#x27;s breaking API changes at the module level.<p>Oh and also because setup.pys in packages are somehow tied to pip apis.<p>It&#x27;s a weak foundation to build from.
taraparoover 1 year ago
Coming from Java&#x2F;Maven I was amazed to see the obscure mess in Python world regarding dependency management. After trying all available tools I finally settled on pdm. I also found it to be more intuitive than poetry.
评论 #39259453 未加载
yijiaheover 1 year ago
The author mentions using pip freeze or pip-tools pip-compile as a solution to the indirect dependencies which are reliant on the Python environment, i.e. the platform and Python version.<p>But from what I understand pip cross-environment usage needs the requirements.txt file to be generated on the environment it is going to be run on. The solution of copying in the same requirements text for installing the packages locally might not work in the container.
评论 #39257119 未加载
nurettinover 1 year ago
pip install, pip freeze, let&#x27;s return to our roots and whatnot. But what if there are dependency constraints?
评论 #39259240 未加载
ktbwrestlerover 1 year ago
This is great! Does anyone know of a similar resource for ruby?
评论 #39257326 未加载
评论 #39260197 未加载
quickthrower2over 1 year ago
Of course you can tame your snake with a container.
nicornkover 1 year ago
Pixi is great, highly recommended!
评论 #39272392 未加载
评论 #39270351 未加载