Poetry: How to add poetry to an existing project

Created on 16 Apr 2018  路  21Comments  路  Source: python-poetry/poetry

I have an already existing project and would like to switch from pip to poetry. Basically I want to initialize an already exsting folder.

How should I do it? It seems poetry doesn't support this.

CLI Feature Setup

Most helpful comment

% poetry init
% poetry add `cat requirements.txt`

All 21 comments

There is no way currently to migrate a project to poetry without building your pyproject.toml file by hand.

And, to be honest, I don't think it's a feature I will add since there are multiple ways to declare your dependencies in Python at the moment (which I want to standardized with poetry): in a requirements.txt file, in setup.py, in setup.cfg.

So, hopefully, you do not have too many dependencies and it should be a quick process with the poetry add command.

@sdispater I didn't mean to migrate a project. I meant to init poetry in an existing directory. Similar to what "git init" or "npm init" do.

Currently, no, it's not possible but that's something I'd like to add.

I will see if I can add it in the next feature release.

@sdispater OK, thank you.

I was just about to ask the same question. Being turned down by pipenv multiple times now (it still has not earned even the 1.0 release stamp), id like to try poetry, but i dont want to create the toml file by hand. I wont bother calling poetry add a few times, but i dont wanna mess with the files syntax by hand. Looking forward to that feature!

This. So much this. This is absolutely needed for a CLI like this.

+1

Release 0.10.0 adds a new init command which covers this use case.

Awesome .. gonna try this tomorrow!

Hope that CLI could support reading package list from stdin to install, something like cat requirements.txt | poetry add -

% poetry init
% poetry add `cat requirements.txt`

This works for me in bash:

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

FOR /F "usebackq delims==" %i IN (cat requirements.txt) DO poetry add %i
on Windows

FOR /F "usebackq delims==" %i IN (cat requirements.txt) DO poetry add %i

This doesn't add packages with version pinning.

ex: it adds Django instead of Django==1.11.20

I would suggest removing the "usebackq delims=="

I happened to have an existing pyproject.toml file and it just throws an error that it already exists. meaning i manually need to add the poetry stuff. Small thing, but some feedback nonetheless

FOR /F "usebackq delims==" %i IN (cat requirements.txt) DO poetry add %i
on Windows

This worked for me.
for /F "tokens=*" %i in (requirements.txt) do poetry add %i

Yeah init command was added but what I would expect is installing all stocks components - .toml, foo_projectname/, test_foo_projectanme/, README.rst. Current init option prompts an empty .toml file, which doesn't seem necessary.

Are we really supposed to put all requirements in poetry (pip freeze output)? Or are we only supposed to add build dependencies?

@cglacet

Build dependencies go into the requires field of thebuild-system section. Typically for a poetry project the only build dependency is poetry itself or poetry-core for more recent projects:

[build-system]
requires = ['poetry-core>=1.0.0']
# ...

Install dependencies are the things that you want to be installed alongside your project, typically because your code imports them at run time. So the recommendation is to list only the libraries your code actually imports directly. Those belong in the [tool.poetry.dependencies] section.
And you definitely should not add the dependencies of your dependencies to that section.

Development dependencies are typically the tools you use while working on your code, so things like _pytest_, _pylint_, _black_, etc. and they belong in the tool.poetry.dev-dependencies section.

pip freeze would give you everything that is currently installed in the environment, so it contains your dependencies but also the dev dependencies and the dependencies of your direct dependencies and the dependencies of your dev dependencies. So pip freeze is definitely not the thing that you want.

@sinoroc Thanks for the very quick and precise answer. I was questioning the answers proposing to directly add everything from requirements.txt which I think in practice often contains the whole pip freeze output.

@cglacet I didn't read the older messages in details, but yes, seems like potentially "_dangerous_" advice.

It depends what requirements.txt contains exactly. Sometimes it is the raw output of pip freeze sometimes it is a meaningful list. There is no rule. People can put whatever they want in it.

If it is a carefully curated list of direct top level install dependencies (i.e. import dependencies), then it is all good. Otherwise, it could lead to issues down the line.

For sure do not add the output of pip freeze to your pyproject.toml.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

EdgyEdgemond picture EdgyEdgemond  路  3Comments

thmo picture thmo  路  3Comments

ulope picture ulope  路  3Comments

probablykasper picture probablykasper  路  3Comments