As of Poetry version 1.8.3, neither @dino's answer nor @mmsilviu's answer worked for me. The reason for this was that I kept getting the error explained in this issue. This comment hints that Poetry expects packages to be in a specific format, which is what fixed the problem for me in the end.
I've split my answer up into a "Short version" section, where I briefly summarize the directory structure you need, and a "Long version" section, where I explain how to build up this directory structure using Poetry.
Short version
To summarize, your complete directory structure should be as follows:
src/
|-- pyproject.toml
|-- scripts/
|-- |-- main.py
|-- my_package/
|-- |-- my_package/
|-- |-- |-- __init__.py
|-- |-- |-- hello_world.py
|-- |-- tests/
|-- |-- |-- __init__.py
|-- |-- pyproject.toml
|-- |-- README.md
where the contents of each file are as follows (I included a comment at the top of each code snippet to indicate the file I'm referring to):
# src/pyproject.toml
[tool.poetry]
name = "src"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]
package-mode = false
[tool.poetry.dependencies]
python = "^3.10"
my-package = { path = "my_package", develop = true }
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
# src/scripts/main.py
from my_package.hello_world import hello_world
hello_world()
# src/my_package/my_package/__init__.py
from .hello_world import hello_world
# src/my_package/my_package/hello_world.py
def hello_world():
print("Hello World!")
# src/my_package/tests/__init__.py
# this file is empty
# src/my_package/pyproject.toml
[tool.poetry]
name = "my-package"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.10"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[comment]: # (src/my_package/README.md)
[comment]: # (this file is empty but can be populated as needed.)
Long version
More specifically, suppose you start with the following directory structure:
src/
|-- scripts/
|-- |-- main.py
where the content of the scripts/main.py
file is
# scripts/main.py
from my_package.hello_world import hello_world
hello_world()
So far, the my_package
package does not yet exist. We will use Poetry to first create a top-level pyproject.toml
file in the src
directory, and then use Poetry to create the my_package
package so that it has the correct structure. We do so using the following steps:
While in the src
directory, run poetry init
. Press "Enter" for each option and enter "no" when asked to specify dependencies interactively. This will create a pyproject.toml
file under the src
directory.
Open the pyproject.toml
file that you just created and add the line package-mode = false
under the section [tool.poetry]
. Also, add the line my-package = { path = "my_package", develop = true }
under the section [tool.poetry.dependencies]
.
While in the src
directory, run poetry new my_package
. This will create the directory my_package
and the desired sub-directories.
Under the directory src/my_package/my_package
, create the file hello_world.py
and put in it the following code:
# src/my_package/my_package/hello_world.py
def hello_world():
print("Hello World!")
Under the directory src/my_package/my_package
, create the file __init__.py
and put in it the following code:
# src/my_package/my_package/__init__.py
from .hello_world import hello_world
You should now be ready to run poetry install
. While in the src
directory, run poetry install
, and the my_package
package should install successfully.
Finally, while in the src
directory, run poetry shell
, and then run python scripts/main.py
. You should then see Hello World!
at the output, which indicates that the scripts/main.py
file ran successfully.
mypackage
using poetry. From what I understand this adds the path to the root of your project to the PYTHONPATH. Have you tried runningpoetry install
and making sure thatmypackage
gets installed inside the virtual environment of your project and running the script again? – Chordate