-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Importing/ exporting from poetry in pip format. #663
Comments
+1 for the ability to export to requirements.txt. My specific use case is Github's automated vulnerability scanner, which currently only supports requirements.txt. |
The PR for exporting to a As for importing, I am not sure this is a direction I want to go in. I understand that if you have a lot of dependencies this might be somewhat a painful process but I don't want to blot the CLI for cases that can be handled by hand or via a simple script. |
@sdispater please at least support pip format for |
I'm looking to move a project from Is there another onboarding/migration process that will make this move easier? |
@meshy and what's about the one-line import script I provided? |
@buriy it gets me some of the way there, thanks. It doesn't work for comments, or places where we've done |
one-liner import scriptJust quoting piece of OP @buriy 's post, in freestanding comment.
👇
|
I will see if I can use pip's requirements parsing like this other post to make a script that handles edge-cases too. Although That's totally reasonable looking at a project continuing to use UPDATE: Interestingly Specifically I may use the Again I still agree with thread above that it makes sense for will report back soon |
Hmm. I had some false assumptions and they were making me think this was more important than it really is. The approach noted above may still be relevant so that various edge cases and more complicated syntax can be dealt with (git/etc) ... but lower urgency since now I realize I/we can translate most things in most requirements.txt files, with that one-liner. |
I think this feature request overlooks one important aspect of a requirements file: although useful in some cases, it doesn't make much sense to do a Converting from the format of a requirements file into the Poetry format for However, I can't properly do is to convert a
What I do now is to pin to exact versions from my To import for locking, it doesn't matter that parsing a requirements file is very complex. If Poetry just parses the bare minimal ( In terms of implementation in Poetry, I don't know the code base, but I guess it requires calling the dependency resolver with the constraints specified in |
You might think that you could import existing requirements.txt easily with ... except that a requirement like |
@hangtwenty I found that @buriy 's script was totally failing on my requirements file, so I wrote a short Python script to do the import. #!python3
sourceFile = "./requirements.txt"
import re
import os
if not os.path.exists("./pyproject.toml"):
os.system("poetry init")
with open(sourceFile) as fh:
requirements = fh.read()
noComments = re.sub("^#.*$", "", requirements, 0, re.IGNORECASE | re.MULTILINE)
bareRequirements = re.sub("\n+", "\n", noComments, 0, re.IGNORECASE | re.MULTILINE).strip()
pipPoetryMap = {
">": "^",
"=": ""
}
reqList = list()
for line in bareRequirements.splitlines():
package, match, version = re.sub(r"^(.*?)\s*([~>=<])=\s*v?([0-9\.\*]+)", r"\1,\2,\3", line, 0, re.IGNORECASE | re.MULTILINE).split(",")
try:
poetryMatch = pipPoetryMap[match]
except KeyError:
poetryMatch = match
poetryLine = f"{package}:{poetryMatch}{version}"
reqList.append(poetryLine)
print("Found dependencies:")
print(reqList)
for req in reqList:
os.system(f"poetry add {req}") |
The
|
I'd use @pnpnpn suggestion, with a minor differences (Notice I removed the |
The beta releases of the |
If someone comes around and wants to convert from Pipenvs Pipfile from pathlib import Path
import json
import toml
Pipfile = toml.load(Path("Pipfile").open())
Pipfile_lock = json.load(Path("Pipfile.lock").open())
for package in Pipfile["packages"]:
try:
version = Pipfile_lock["default"][str(package)]["version"]
print(f"poetry add {package}={version.replace('==', '')}")
except KeyError:
pass
for package in Pipfile["dev-packages"]:
try:
version = Pipfile_lock["develop"][str(package)]["version"]
print(f"poetry add --dev {package}={version.replace('==', '')}")
except KeyError:
pass |
@sdispater I think it would increase the adoption rate if an import command was built into Poetry. I like using Poetry, but having to do manual work (or rely on scripts) when moving every legacy project to Poetry is an adoption barrier. If export already exists, I think it'd be consistent in the CLI if import existed as well. |
This one is working very fine. |
Windows user here - can't run these commands natively, and don't have WSL2 or working linux env. on my home pc. Not a huge deal for a small project, but most companies still make devs use windows, so shell scripts are a high barrier for us. Although it wasn't obvious at a glance but easy enough to figure out that we can just add packages as a list instead of one by one:
This makes the manual conversion pretty easy... Big fan of poetry by the way. |
+1 for an export command from poetry.lock to requirements.txt format. Would help a lot for deployments. |
@bolinocroustibat Export is already supported: https://python-poetry.org/docs/cli/#export |
to do this I have good luck running |
@sdispater, thank you for your work on Poetry, I love it! I'm always looking out for projects to convert, which means I really would find an It seems to me, that If export is a valid use case, it's obvious that import is also a valid use case. I think this is a feature that many in the community, especially newbies, would really value. Perhaps this feature should be "up for grabs"? I think that the positives outweigh the cli interface bloat negatives. Edit: Wording. I wrote this in a hurry and it sounded rude, apologies. Re-written. |
it is just one-liner in bash |
@TalyGin does that work if you have pinned your requirements to specific versions in requirements.txt? Poetry uses a different format, |
Isn't it something that dephell can do? |
Yes, it works, in input file |
It works for == but not < nor > , nor for git deps.
…On Fri, Nov 27, 2020 at 11:55 PM Vitaly Shulgin ***@***.***> wrote:
@TalyGin <https://github.com/TalyGin> does that work if you have pinned
your requirements to specific versions in requirements.txt?
Poetry uses a different format, poetry add 'requests:<=3.0'
Yes, it works, in input file ipaddress==1.0.22 in pyproject.toml I've got ipaddress
= "1.0.22"
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#663 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAFFBDR7XEIMXE2ZKF3UXLSR7KZNANCNFSM4GF5PDLQ>
.
--
Best regards, Yuri V. Baburov, Skype: yuri.baburov
|
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hi Sébastien,
Now the poetry works great as a package manager (I'm happy to use it!).
So, I think, maybe it's time to revisit export/import to requirements.txt for the projects that can't rely on poetry being installed on the target environment, but still wanted to be managed by poetry (some people prefer
pipenv
utility, other people use plainpip
, etc).Also, I'm considering use cases when you need to import the pip list of the existing packages to poetry.
The export workflow looks pretty simple:
you just export the requirements, similar to how "pip freeze" does (now this can be only done with
poetry run pip freeze
) , or add some option, maybe the good syntax ispoetry show --requirements
. A proposed implementation is here: #100 (comment)As for the import workflow, right now the challenges are:
I consider the following command line options (naming can be different, of course):
poetry add --requirements requirements.txt
andpoetry add --pip poetry>0.10
,poetry add --pip lxml<4.2.6 poetry>0.10
(Or maybe you can just support this format without this option)
I'm not sure how complex is to handle properly the pip format, but even the minimal support would cover 99% of the requirements: "package", "package{comparison op}version". They now work if you add a colon, like this: "package:{comparison op}version". So I believe it's easy to support those -- that would be a great help!
Also, to prevent
poetry add
from breaking, I would consider adding:poetry add --save
which only saves the updates, then,poetry add --lock
which updates the lock file, but doesn't install the package(s) (similar to how poetryupdate --lock
). I would also addpoetry remove --save
andpoetry remove --lock
to have the inverse operations.I would also change
poetry add
behavior to ignore already added requirements (though a warning might be displayed). Otherwise afterpoetry add package1 package2 package3
, if it breaks at package2 you not only need to change package2 options but also remember to remove package1 from the list -- otherwise, you'll get a "Package package1 is already installed" error.Currently I have to use the following script to import the requirements from a file:
cat requirements.txt | perl -pe 's/([<=>]+)/:$1/g' | xargs -n 1 echo poetry add
Exporting the list is also non-trivial now, so it's great that
pip freeze
works fine.I believe these features might significantly increase poetry adoption speed.
The text was updated successfully, but these errors were encountered: