emonier
emonier

Reputation: 633

How poetry knows my package is located in the src folder?

I have a simple question.

I used to create a poetry project with my package at root.

project.toml
mypackage
  +- __init__.py
  +- mypackage.py
  +- test_mypackage.py

I recently moved my tests in another directory, so that the folder now looks like.

project.toml
src
  +- mypackage
      +- __init__.py
      +- mypackage.py
tests
  +- test_mypackage.py

Nothing changed for poetry which still work fine when I make poetry build. How does it search for the package root folder? Does it serach a folder with the package name in project.toml?

Thanks for your help.

Upvotes: 35

Views: 33059

Answers (3)

Arne
Arne

Reputation: 20217

Having a folder called src to contain the package code is just a pre-defined standard that poetry recognizes without being told. It works via the packages section in your project file, which by default scans for mypackage and src/mypackage. If you provide your own value, it will stop auto-detecting those two.

Upvotes: 24

mchristos
mchristos

Reputation: 1909

For anyone that has source modules contained directly in a src directory, without a my_package directory, and where you want imports to look something like

from my_module import foo 
from utils import bar 
from constants import FOOBAR

instead of

from src.my_module import foo
from src.utils import bar
from src.constants import FOOBAR

You can do the following in your pyproject file:

packages = [{include = "*", from="src"}]

Upvotes: 18

HHest
HHest

Reputation: 831

As of Poetry ver 1.6.1, the following spec in pyproject.toml works

[tool.poetry]
packages = [{include = "mypackage", from="src"}]

Upvotes: 18

Related Questions