如何使用诗歌从现有项目导入 requments.txt

我正在一个现有的项目中试用 诗歌。它最初使用了 pyenv 和 Virtual env,因此我有一个带有项目依赖项的 requirements.txt文件。

我想使用 诗歌导入 requirements.txt文件,这样我就可以第一次加载依赖项。我看了诗歌的文档,但还没找到解决的办法。你能帮我吗?

我知道我可以手动添加所有的软件包,但我希望有一个更自动化的过程,因为有很多软件包..。

56954 次浏览

poetry doesn't support this directly. But if you have a handmade list of required packages (at best without any version numbers), that only contain the main dependencies and not the dependencies of a dependency you could do this:

$ cat requirements.txt | xargs poetry add

The best method I've found is this one:

$ for item in $(cat requirements.txt); do poetry add "${item}"; done

One liner:

cat requirements.txt | grep -E '^[^# ]' | cut -d= -f1 | xargs -n 1 poetry add

I don't have enough reputation to comment but an enhancement to @Liang's answer is to omit the echo and call poetry itself.

cat requirements.txt | grep -E '^[^# ]' | cut -d= -f1 | xargs -n 1 poetry add

In my case, this successfully added packages to the pyproject.toml file.

For reference this is a snippet of my requirements.txt file:

pytz==2020.1  # https://github.com/stub42/pytz
python-slugify==4.0.1  # https://github.com/un33k/python-slugify
Pillow==7.2.0  # https://github.com/python-pillow/Pillow

and when calling cat requirements.txt | grep -E '^[^# ]' | cut -d= -f1 (note the omission of xargs -n 1 poetry add for demonstration) it will output the following:

pytz
python-slugify
Pillow
# NOTE: this will install the latest package - you may or may not want this.

Adding dev dependencies is as simple as adding the -D or --dev argument.

# dev dependancies example
cat requirements-dev.txt | grep -E '^[^# ]' | cut -d= -f1 | xargs -n 1 poetry add -D

Lastly, if your dev requirements install from a parent requirements file, for example:

-r base.txt


package1
package2

Then this will generate errors when poetry runs, however, it will continue past the -r base.txt line and install the packages as expected.

Tested on Linux manjaro with poetry installed as instructed here.

I appreciate this might be a bit late but you can just use

poetry add $( cat requirements.txt )

Just use the plain requirements.txt and filter out version numbers with awk:

awk -F '==' '{print $1}' requirements.txt | xargs -n1 poetry add

-F specifies a filter or split point. $1 is the first argument in the split. The input file comes as last argument. Afterwards you can pipe it to poetry add using xargs -n 1 to call poetry add with each line consecutively and not with a space separated string at once. If you want to consume all entries at once just ommit -n 1. Also make sure that a poetry environment is already present.

To just consume the requirements.txt omit the filter and use

awk '{print $1}' requirements.txt | xargs -n1 poetry add

But other tools like cat are fine for that case as well.

For Powershell:

$reqs = @(cat requirements.txt)
for($i = 0; $i -lt $reqs.length; $i++){poetry add $reqs[i]}

Note this won't ignore comments or anything else in the requirements file. This is strictly taking it as raw text so it expects every line to be a package.

I found none of these answers sufficed so I created one of my own:

https://github.com/src-r-r/python-stanza

It's a new baby, so contributions welcome, but so far it's very cookiecutter-friendly:

  • automatically detects a setup.py and fetches project info
  • allows multiple requirements.txt files to be specified for either dev dependencies or normal dependencies
  • allows the name and version to be overwritten.
  • also adds referenced requirements (e.g. -r ./special-requirements.txt) if it's included in a requirements file.

Here's one that works if you have # comments (at the start of a line or at the end of a line) in your requirements file:

poetry add $(sed -e 's/#.*//' -e '/^$/ d' < requirements.txt)

https://www.grymoire.com/Unix/Sed.html#uh-30

I made a tool poetry-add-requirements.txt just for this. Code

Install it with pipx install poetry-add-requirements.txt,

then run poeareq.

Usage

Run poetry-add-requirements.txt, optionally specify your requirements.txt files and --dev for dev dependencies.

poeareq is provided is an alias to poetry-add-requirements.txt.

$ poeareq --help


usage: poeareq [-h] [-D] [requirements.txt files ...]


Add dependencies specified in requirements.txt to your Poetry project


positional arguments:
requirements.txt file(s)
Path(s) to your requirements.txt file(s) (default: requirements.txt)


options:
-h, --help            show this help message and exit
-D, --dev             Add to development dependencies (default: False)

Features

  • Auto detect charset of requirements.txt file(s) and feed normalized dependency specs to poetry.
  • Stop on first poetry add error.

A one-liner for Windows PowerShell users:

    @(cat requirements.txt) | %{&poetry add $_}

For more about piping arguments with PowerShell see this useful answer.