12

I'm running Python 3.9.1 and i have successfully installed poetry version 1.1.4. When I am trying to add requests ($ poetry add requests) I am facing

RuntimeError
Poetry could not find a pyproject.toml file in C:\...

I have just installed it and I am not sure if I have missed something. Could anyone advise please?

Leonardo Scotti
  • 1,069
  • 8
  • 21
24beads
  • 161
  • 1
  • 2
  • 9

2 Answers2

16

You have to create a pyproject.toml first. Go into your project folder, run poetry init and follow the instructions. As an alternative you can run poetry new myproject to create a basic folder structure and a pyproject.toml. Also have a look into the docs.

finswimmer
  • 10,896
  • 3
  • 34
  • 44
2

In my case, I was in a Docker container of a legacy Python version (3.6). I had to use pip instead of conda and therefore, I installed Poetry to keep the dependencies right.

bash-4.4# docker exec -it MY_CONTAINER bash

starts the command prompt of the container.

Now turning to answer the question, which is not a Docker question.

In the next command, you might need to write /usr/local/bin/poetry instead of just poetry instead.

bash-4.4# poetry init

This command will guide you through creating your pyproject.toml config.

Package name []:  test
Version [0.1.0]:  1.0.0
Description []:  test
Author [None, n to skip]:  n
License []:
Compatible Python versions [^3.6]:

Would you like to define your main dependencies interactively? (yes/no) [yes] no
Would you like to define your development dependencies interactively? (yes/no) [yes] no
Generated file

[tool.poetry]
name = "test"
version = "0.1.0"
description = "test"
authors = ["Your Name <you@example.com>"]

[tool.poetry.dependencies]
python = "^3.6"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"


Do you confirm generation? (yes/no) [yes]

Very easy side remark which should be clear to the most: If you press Enter at a field of filled brackets, it will just enter what is written in the brackets. For example, if you press Enter at Version [0.1.0]:, you will make it the version 0.1.0, unless you enter your own. That is also to say that those brackets [] do not mean that you have to enter a list, it is just to show what is entered when you just press Enter.

After this, I could run:

bash-4.4# poetry add pandas

Another Docker side note: It turned out that apk (Alpine containers) on legacy Python 3.6 cannot handle basic packages well enough, with or without Poetry, see Installing pandas in docker Alpine. I had to switch to a newer Python version. And you can install Poetry already by means of the Dockerfile and not just in the container bash, see Integrating Python Poetry with Docker

Wrapping up:

It was strange to me that I had to enter things that I would only use when I published a self-written single package (see: Package name []), although I was expecting a general setup of a package manager of many packages as a whole. In the end, I just followed the menu by entering some irrelevant placeholders. The right Python version as the only important core of the pyproject.toml file was already automatically suggested. This was all that was needed.

questionto42
  • 7,175
  • 4
  • 57
  • 90