How to import requirements.txt from an existing project using Poetry How to import requirements.txt from an existing project using Poetry python python

How to import requirements.txt from an existing project using Poetry


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


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

poetry add `cat requirements.txt`


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/pytzpython-slugify==4.0.1  # https://github.com/un33k/python-slugifyPillow==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:

pytzpython-slugifyPillow# 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 examplecat 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.txtpackage1package2

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.