:wave: hey folks……I have a python project with re...
# general
s
👋 hey folks……I have a python project with requirements captured in
pyproject.toml
(in the poetry style — as that is what the repo was configured to use pre-pants). I would like to add a new package. How do I go about finding the correct version? I used to use
poetry add $PACKAGE
, but I can’t seem to find a similar goal (or subtarget) for pants. thank you (in advance)!
b
You can add a
python_requirement
target if you want to manage that new package through Pants, you’ll just have to make sure it belongs to the same resolve as the requirements that you’re pulling in through Poetry
s
But what would the command be to say add
requests
?
pants add requests
?
As I imagine
pants
needs to figure out a version that will work, and I’m not sure what version will work with all my other dependencies
b
Ah, I don’t think such a command exists; you’d have to add a
python_requirement
target to your build file and then regenerate the PEX lockfile using
pants generate-lockfiles
Alternatively, if you want to keep all your dependency declarations in one place, you can still use
poetry add
and Pants will pick up your new package if you’ve defined a
poetry_requirements
target pointing at your
pyproject.toml
file
s
@better-van-82973 ok, makes sense. I’ll give that a whirl
Thank you!
b
Note that in the second case, you would still have to run
pants generate-lockfiles
to ensure that your new dependency makes it into the lockfile
s
yeah, so it’ll be using poetry to figure out the version of the package, but I will continue to use the pants lockfile for the universe of depdencies
h
Well, there is no magic here in Poetry or any tool. You can provide some input to constrain the version range your code is compatible with, and then the lockfile contains one specific version in that range that is also compatible with everything else in the lockfile. If you specify no constraints at all to poetry then that is equivalent to putting an unconstrained input request in your pyproject.toml
So basically you must hand-edit pyproject.toml, possibly with constraints, but the actual version picked comes from the lockfile you regenerate
Not sure I’m making sense here, but my point is that
poetry add
is just a convenience for hand-editing pyproject.toml
s
@breezy-twilight-65275 but the
poetry add requests
will determine a version that works, or maybe it just uses the latest version. So if it’s the later case, then I guess yeah, just always hand edit until you find a version that works
b
Indeed, Poetry just uses the latest version and then pins the range via caret requirement: https://python-poetry.org/docs/dependency-specification/#caret-requirements It looks like Poetry’s docs suggest that it will try to find a compatible version: https://python-poetry.org/docs/cli/#add
s
TIL 😄 thanks folks!
h
To clarify - Pants generating a lockfile is what finds a version that works. You don’t need to do that by hand editing. You can put an unconstrained
requests
in the inputs to that lockfile (e.g. in pyproject.toml), and you shouldn’t need to do any manual trial and error. However you may sometimes want to manually constraint that input, to prevent the lockfile selecting a version that doesn’t have a feature you need.
s
@happy-kitchen-89482 oh….can you show me what it would look like to have an unconstrained version of requests in pyproject.toml? I’ve never seen that before
h
Isn’t it something like
requests = "*"
? I’m no poetry expert though, maybe that doesn’t work for some reason
In a
requirements.txt
you would just put
requests
on a line by itself
👍 1