Moving from Pipenv to Poetry
We recently moved from pipenv to poetry. So far we’ve mostly had a good experience with poetry, but here are some things that I would have like to have known beforehand, and some of the reasoning behind the decision.
Why switch?
- pipenv was slow (poetry can also be slow too, but it’s easier to debug why)
- pipenv had issues for multiple members of our team:
- It didn’t work on WSL (Windows Subsystem for Linux)
- Another team member had consistent crashes, hangs, and cases where it could just not install the packages
How did we do the switch?
We used dephell to convert our dependencies from pipenv to Poetry:
dephell deps convert --from=Pipfile --to-format=poetry --to-path=pyproject.toml
dephell deps convert --from=Pipfile.lock --to-format=poetrylock --to-path=poetry.lock
After that you have to add and fill out the attributes name
, version
, description
, and authors
under tool.poetry
in pyproject.toml
:
[tool.poetry]
name = "eduflow"
version = "1.0.0"
description = "A flexible, lightweight, and easy-to-use LMS"
authors = ["Peergrade Inc."]
Generally installing and updating packages has been much faster for us with poetry, compared to pipenv (barring pathological cases like “Why is poetry install
taking forever?” below).
Poetry 101
- How do I activate the virtual environment?
While it is documented, it wasn’t in the quickstart. Luckily, it’s the same as in pipenv – just do:poetry shell
. I useCtrl-D
to get out. - The equivalent of
pipenv install <package>
ispoetry add <package>
.
The “bare” versionpipenv install
is stillpoetry install
poetry update <package>
will not update the package beyond the version stated inpyproject.toml
.
To update a package beyond what’s inpyproject.toml
you can dopoetry install <package>@latest
.- Why is
poetry install
taking forever?
Stop (Ctrl-C
), and try doingpoetry install -vvv
instead –– you’ll probably see that it’s going through a lot of versions for a particular package. For us, that package wasawscli
. We hadboto3
set at1.10.*
whereasawscli
was just*
. Since theboto3
version 1.10 requires3transfer (>=0.2.0,<0.3.0)
and the recent versions ofawscli
requiress3transfer (>=0.3.0,<0.4.0)
poetry was probing hundreds ofawscli
versions (starting from the most recent and going backwards) to find one that was compatible withs3transfer (>=0.2.0,<0.3.0)
. The fix is to not use*
forawscli
but setting a meaningful version range.
This leads me to the next point: *
-requirements are bad
If you’re coming from Pipenv you might have a lot of those — especially as dev-dependencies. In my opinion, there’s mostly no reason to have those. At least lock it down to a major version like1.*.*
. Even though having*
on dev-dependencies won’t hurt you in production, it’ll still stump your team whenpytest
is silently updated from version 4 to 5, or a different backwards-incompatible package update breaks your usual write-test-debug cycle. An added bonus – and a big one I would say – is that it’ll be much faster for Poetry to resolve dependencies and produce the lockfile.- Running
poetry shell
tells meVirtual environment already activated: <path to virtual environment>
This is most likely because you’ve rundeactivate
. Runningdeactivate
won’t work, instead, you have to quit the shell that was started bypoetry shell
: You can do this by doing Ctrl-D on an empty line.