PDM: A Modern Python Package Manager
pdm.fming.dev
pdm.fming.dev
- PEP582 which it is based on is still in draft, and some tools (VS Code) won't fully support it until it's accepted.
- If you want to develop or test using different Python versions, you still need to use a virtual environment. PDM does handle this for you though.
IMO, the Python packaging ecosystem has been a dumpster fire for a long time, but the flurry of recent PEPs that provide a standard way of defining a package with pyproject.toml have made it so much better. Now it's just a matter of the tools catching up.
But really, it's a hard problem, between cross-platform support, backwards compatibility, security concerns, hosting, most authors being volunteers and so on.
And still, even with "just" pip or even conda I am enjoying the Python experience more than some other packaging solutions I've seen.
I mean, just look at this thread. Someone asks "So in light of this, what should I use for python packaging?", and they get two dozen different answers loaded with weirdness like pyenv, python-venv, virtualenvwrapper, etc... If I wasn't using python, I would've thought this is some cruel python in-joke that outsiders don't get.
Just looking at those names, I'm already confused as to what the hell each are doing and why do I need them?
But let's go back to pip and conda. Conda is unbearably slow. Pip is not entirely reliable in resolving versions properly. There's also not entirely cooperative interaction between conda and pip. If you use conda, you should not use pip (or just minimally) because it'll result in a mess.
Yes, packaging is hard, but it feels like python has managed to solve (or not solve) it in a uniquely obtuse and bad way so far.
Hopefully the slew of new PEPs will finally bring some clarity to this mess.
Labeling something a "dumpster fire" is a value judgement. It can't be "objective", unless there is a physical dumpster that is burning. It's not like the Python community hasn't solved this problem because it's lazy or incompetent. It's just a hard problem.
It must be true because people say something like this any time a "python packaging sucks" thread comes along ... but I can't recall ever experiencing that in 10 years of Python-ing
The people having problems always seem to mention Conda though
I guess the parts that have problems are primarily where FFI and compilation or linking of non-Python code is happening?
So I stick with this setup:
- Use pyenv to manage different Python versions and virtual environments.
- Use the standard PEP621 specification as a high-level dependency description: https://www.python.org/dev/peps/pep-0621/#example
- Use pip freeze > requirements.txt as a "lockfile".
The pip docs also suggest using pip-tools to create lock files. Pip-tools is only for creating lock files (it’s not trying to fix virtualenvs like poetry is), and it works great.
Maybe I will just try first. pip-tools seems to acknowledging PEP621
https://pip-tools.readthedocs.io/en/latest/changelog/?highli...
the pip team has introduced strict performance checks to ensure the new resolver was not significantly slower than the old. they solicited requirements files to weed out situations exactly like the one you describe. for example https://github.com/pypa/pip/issues/8664
if you want it improved, please report it.
Putting that aside though: yes, 100% pip-tools or an equivalent (which pyenv is not). It's the only sane way to both freeze dependencies for reproducibility, and maintain upgradability. I've used pip-tools for years on many large, complex projects, and it has been a huge benefit every single time. And it routinely revealed significant library-incompatibility problems that teams had only luckily dodged due to not using feature X yet, because pip's resolver has been so braindead for forever.
$ poetry init
$ poetry add django
$ poetry shell
It's pretty simple. Check in the lock file, and then run $ poetry install
to replicate it.> - Use pip freeze > requirements.txt as a "lockfile".
There's lots of reasons to not do this anymore, and Dependency Hell is real, and has been for 25 years with RedHat RPM's, etc.
Even if you don't want to rely upon poetry for building in prod, poetry can still export a requirements.txt file for you, so you're not locked into using poetry, but you still get to specify the high level packages you want, and let it solve the dep graph for you.
If you want to be relaxed about dependencies, you can use "pip-chill".
pip install pip-chill
pip-chill > requirements.txt
And, if you are even more relaxed, pip-chill --no-version > requirements.txtThis is not and has never ever been correct. It makes it infinitely harder to install an application vs the standard `pip install -e .` which works on every package manager and avoids PYTHONPATH issues, as well as being able to publish your application to PyPI for easy installation (as simple as pip install --user app or pipx install app).
Should I just upgrade to Poetry or should I just dive headfirst into PDM? Keep myself at Pipenv? I'm at a loss.
Thanks in advance!
The issues happen because poetry is strict about version constraints. Pip used to be lax, so some packages could get away with poor/overly restrictive constraints..
Now pip is strict as well, and poetry is also getting a lot more common, so maintainers have had pressure to fix most of these issues.
poetry add ingredients # most recent version: 1.0
poetry add cake # requires ingredients, but <1.0
So here, poetry would add ingredients to your pyproject.toml with version = "^1.0.0" in the first step. In the second step, when you tried to add cake as a new dependency, it would say it can't find a version of cake compatible with your pyproject.toml's requirement for ingredients.The solution is to change pyproject.toml to be more permissive about the version of ingredients, resulting in it downgrading to a compatible version. Or submit an issue and/or PR with the maintainers of cake to bump up the supported versions.
I've had one or two fundamental version conflicts with a 5+ year old application with 100+ dependencies and a decent amount of legacy stuff. They were a pain in the ass, and the sdispater's stance on not allowing overrides is a pain in the ass. We ended up forking the upstream libraries to resolve the version conflict.
With all of that, poetry is amazing and a huge step forward. I'd advocate it wholeheartedly.
Out of frustration, I decided to try pdm instead, and although pdm doesn't handle pushing to pypi repositories, Twine works fine for it, and I have had a pain-free experience using pdm for dependency management.
I like the idea of poetry, but it's unusable if I can't get bugs fixed, and I gave up after nearly a year of trying.
Yeah, I think that's the worst thing about private infrastructure in 2021 is getting those certs correctly installed and seen by the software and both sides.
I'm thinking that tailscale or something like it may be a better alternative in the future where authorization is at the link layer before the tcp connect can happen.
If one of your dependencies won't work with 3.9, the dependency graph cannot resolve. The author of Poetry said that this is because Poetry is meant for libraries, where this makes more sense.
If you're building an application, use Poetry. If you're building a library, use Flit. Use PEP621 metadata in pyproject.toml regardless.
Poetry is much more focused on managing dependencies for applications than dealing with libraries that have to be used by other libraries or applications. See this deep discussion for some timely/relevant examples: https://iscinumpy.dev/post/bound-version-constraints/
I tried to use project.dependencies and project.optional-dependencies fields in pyproject.toml defined by PEP621. However Poetry seems to ignore it and it tries to stick with its own tools.poetry section.
Python packaging is again in a chaos since 2021 because of the rise of standardized PEPs.
I was working actively with Python (and loving it) between 2012-2015 (during which the 2 vs 3 transition/conflicts were raging) and the couple of times I've tried to get back into the groove since then (in side projects) I keep hitting walls and getting confused about how to even get the basic things running.
I've heard good things about Poetry but never tried it. PDM being closer to npm/yarn sounds like an attractive option (since they're very familar), just to tinker around (not to build a complex application with specific requirements). Will give it a shot!
My package is a Python module and a set of command-line tools based on that module.
Is my package a library? Or an application? Because it seems to be "both".
It also includes several C extensions, using either the Python/C API manually or Cython. I would love to switch to something more modern than setup.py, but none of these new back-ends seem to support this. For example, I just checked - flit explicitly focuses on "pure Python packages with no build steps" "and leaves the hard things up to other tools".
If the only expected users of your API is your own project, it's an application.
And yes, if you need to build native extensions, setuptools is still your only real option. It's not great, and results in hundreds of packages that all find their own definition of insanity. But given just how crazy the C/C++ toolchain ecosystem is, especially when you have to deal with Windows, macOS, and the diversity of Linux, no one else has been will to tackle that problem because setuptools already exists.
But quoting PEP 517 "Much experience suggests that large successful projects often originate as quick hacks".
I have another project which started as a command-line tool only, meant for the end-user to build, maintain, search, and update a data set. It was not designed to be called by another program, neither through import nor popen. Following the guideline, I should use poetry.
It then acquired a limited Python API so search functionality could be incorporated in a web app. One of my users paid for that feature. I don't know if others use it.
This turned my package into a library. Does that mean I should switch from poetry to flit?
There seem to be several people who have tackled that problem, including emscons, PyQt-builder, and mesonpep517, which all defer to an existing third-party build system. I don't use or have experience with any of the underlying build systems, and version numbers like 0.28.0 and 0.2 make me wary.
Are there others? Finding such modules on PyPI is not easy! A search for "517" is almost useless. Could there be a new trove category for such packages?
The linked article is quite long. Do you have a quick example? I've built lots of apps and libraries using Poetry and haven't run into any dependency issues, and you can specify versions constraints in the same way as Flit, so it's hard to see how they'd be different in that regard.
Edit: I think I see what the issue is--Poetry sets upper version bounds by default via its ^X.Y.Z and ~X.Y.Z syntax and advocates for always specifying upper bounds in their docs.
So, it's not that Poetry can't be used for a library, but you'll have to know that you can, and perhaps should, use different version syntax in some cases (i.e., the standard >=X.Y syntax).
I mostly code on JavaScript and (obviously) use NPM a lot, and it makes me wonder.
To be fair, I'm not saying there's anything wrong with virtualenvwrapper, just that I've never used it and for my purposes the above solution works well.
Maybe 3.11 can make python packaging less of a beautiful disaster.
This will mark me out as a Luddite but I am still quite happy with a 5 line setup.py and “pip install .”
https://news.ycombinator.com/item?id=29446715
$ cat setup.py
from setuptools import setup
setup(
install_requires=['arrow'],
packages=['dogclock'],
scripts=['scripts/dogclock'])python3 -m venv venv && source venv/bin/activate && pip install -r requirements.txt
Python standard library is great and its a nice languate if you like the syntax but aside from a few constants like Django, Flask, and Pandas, the ecosystem feels like it is slowly turning into a fragmented mess.
... bless my `zsh` shell history for these incantations. I don't think I have any hope of remembering it -- probably because of all the old virtualenv incantations!
Kind of agree with pipenv though. It's painfully slow, but it abstracts away having to worry about various requirements files (eg: dev vs prod) and the .lock keeps things consistent.
It's very handy for having different environments but doesn't lend itself to a lockfile.
I get versioned requirements files for the project base requirements, and also for each (version, implementation) of python, in case they are changes, and this has proven to be reliable for me.
It's all about finding the minimal-but-complete convenience / ergonomic solution over the, err, inconvenience of packaging. I also marvel at when I attempt to explain these things to experienced programmers, I only manage to convince them 50% of the time at most.
I use the same solution to have multiple versions of Ansible installed.
If you need to run multiple version of Python, then virtualenvs might not be enough, but that's honestly not a problem I have. New version of Python, great, let's me just rebuild my virtualenv and get back to work.
One of the most important rules I have regarding working in Python is: Never, never ever, install ANYTHING with the global pip. Everything goes in to virtualenvs.
I’d just use pip and requirements files if you can. It’s doubtful that your requirements are sufficiently complex as to require a more complex resolver, although that depends on your ML needs.
You can regard __pypackages__ as a virtualenv WITHOUT the interpreter, it can easily work with any python interpreter you choose as long as it has the same major.minor version as the packages folder.
$ poetry init # to init the pyproject.toml file.
$ poetry add <packages>
$ poetry shell # to activate inside the virutalenv.
Lock in the poetry.lock file after any change to your project dependencies, and other people can duplicate the project using after doing a git pull. $ poetry installQuoting the GitHub page[0]:
> PDM is meant to be a next generation Python package management tool. It was originally built for personal use. If you feel you are going well with Pipenv or Poetry and don't want to introduce another package manager, just stick to it. But if you are missing something that is not present in those tools, you can probably find some goodness in pdm.
Having used PDM a bit, its ambition in my opinion may not be to replace existing tools, but rather to experiment and implement the most recent PEPs related to packaging.
While you can argue about PEP 582[1] implementation (which is still in draft), PDM doesn't prevent anyone from using virtual environments, and even provides a plugin[2] to support that. PDM also implements PEP 631[3], which most other package managers have been relunctant to support or slow to adopt.
[0]: https://github.com/pdm-project/pdm
[1]: https://www.python.org/dev/peps/pep-0582/
EDIT: Oh, I should say: If it's meant to take over the world, say so, as well!
But as PDM becomes mature, it is acknowleged by the Python packaging people, I also work hard to make PDM fit more people's workflow, fortunately, it has a strong plugin system. You can add virtualenv support(pdm-venv), publish command(pdm-publish) and more. In the future, I would like to see it can eventually push the iteration of PEP 582 and make it finalized.
> This PEP proposes to add to Python a mechanism to automatically recognize a __pypackages__ directory and prefer importing packages installed in this location over user or global site-packages. This will avoid the steps to create, activate or deactivate "virtual environments". Python will use the __pypackages__ from the base directory of the script when present.
Thus, the idea of PDM is that it will create a directory, called `__pypackages__` in the root of your project and in that folder it'll populate all the dependencies for that project. Then, when you run scripts in the root folder of your project, your Python install will see that there's a `__pypackages__` folder and use that folder to look up dependencies.
This style of "dependencies inside the project directory" is similar to how npm of the Javascript ecosystem works, where it creates a `node_modules/` folder in the root of your project and fills that folder with the dependencies for your project. This style of dependency management is different from other package managers such as Poetry (Python), Pip (Python), go (Golang), and cargo (Rust), all of which instead have a sort of "secret folder acting as cache of dependencies at particular versions", a folder that's usually pretty hidden out of the way, in which the package manager automatically manages the acquisition, storage, and versioning/version resolution (Poetry, Go, Cargo, all do this but Pip does not).
That's a very fast and probably wrong rundown on what makes this package manager different from others.
It seems functionally similar to venv but has the benefit of standardizing the location of dependencies to __pypackages__/3.x/*. With venv the developer selects some arbitrarily named directory that is sometimes but not always .venv/*.
I have not come across PEP 582, thank you for linking.
Also this will just pollute your source directories with generated directories and files that shouldn't be there.
> Also this will just pollute your source directories with generated directories and files that shouldn't be there.
I don't see why it is a problem, node.js also has node_modules in the project. And __pypackages__ is created with a .gitignore file so you won't commit it accidentally.
Ill still use Poetry, but this could be paving the way for Poetry to work without virtualenvs as well one day.
Scripts, Plugins etc for Blender are currently distributed in a very ad-hoc way, and it is hard to get adoption with plugins that require more elaborate dependencies, especially binary modules.
https://github.com/dephell/dephell
Dephell is a converter for python packaging systems. It can turn poetry files into requirements.txt, or setuptools' setup.py into pipenv's Pipfile etc.
Python Packaging: There is More Than One Way to Do It
If there's one reason to use PDM: the maintainer fixes bugs fairly quickly, not like Poetry, where many bugs are left open and nobody is taking care of it.
I use python almost every day and I find it a cluster fuck anytime i have to install something using a specific version or update some packages. If it wasn't for containerization i would've lost hope very early on in the python packing ecosystem.
It doesn’t replace your setup.py or pyproject.toml as a maintainer, though. For that I uses poetry with pyproject.toml. As a maintainer, you need to release to PyPI first and then release it to anaconda or conda-forge (where I prefer the later.) Releasing on conda-forge seems more troublesome at first, and that process makes you trust conda-forge more than PyPI where it virtually has no barrier to entry.
As an end user, most packages you can pip install with can be conda/mamba install with. For those which doesn’t exist, you can use pip install inside a conda environment. (Which is like virtualenv.) dependencies already installed via conda won’t be repeated.
Conda is designed to be multi platform and multi language as well. Npm, Julia, ffmpeg, etc can be installed via conda. It is designed for scientific stack where there’s a lot of non pure Python dependencies. In this aspect, it is more like a package manager other than the dIstro provided ones, which should be compared to homebrew, Macports, nix, pkg-src, etc.
Every extension proposal should be required to be accompanied by a kidney. People would submit only serious proposals, and nobody would submit more than two. — Jim Waldo
Also when deploying to kubernetes or whatever it works the same way
Perhaps this is more of an issue with larger projects, but I also noticed that 'no need to use virtualenv' as a positive.
> When deploying to kubernetes it works the same way
Can you elaborate on this?
Pipenv is only focused on solving one problem, deploying complete packages you wrote to a server you have complete control over. It doesn't really care about the problem of developing applications and libraries for distribution and installation by third parties and on platforms running different python version/OS etc. than what it was developed on.
* Please don't confuse `conda` with Anaconda, Miniconda & Conda-Forge.
* You can use `conda` to manage your environment and it can download and install ALL your packages from `pypi.org` and not Anaconda.
[1] https://docs.conda.io/projects/conda/en/latest/user-guide/ta...
If not, then I'd stick with minimal pip3 or fullblown anaconda3
that's what PDM is