What made it work for me is to create a folder in your root with the same name of the package in the pyproject.toml
file.
If you don't do this, poetry will not find your project and will not install it in editable mode.
You also need to let Python know that a folder is a package or sub-package by placing an __init__.py
file in it.
my_project/
├─ pyproject.toml
├─ my_project/
├─ __init__.py
├─ module.py
├─ scripts/
├─ __init__.py
├─ main.py
When you initialize the folder you should see that the project is installed.
...> poetry install
Installing dependencies from lock file
Installing the current project: my_project (0.1.0)
Last thing, you need to change the import in main.py
.
# main.py
from my_project.module import my_function
print("my_function imported")
You can now activate the virtual environment (if it's not already active) and run your script.
...> poetry shell
Spawning shell within: C:\...\my_project\.venv
...
...> python my_project\scripts\main.py
my_function imported
PS
As @sinoroc pointed out in the comments, it is possible (and preferable according to him) to run the module with python -m my_project.scripts.main
.
I am new to the topic, but according to this SO answer:
__package__ is set to the immediate parent package in <modulename> [when running python via command line with the -m
option]
This means that relative imports like the following magically work.
# main.py
from ..module import my_function
# ..module tells python to look in the parent directory for module.py
print("my_function imported")
...> python -m my_project.scripts.main
my_function imported
...> python my_project\scripts\main.py
Traceback (most recent call last):
...
ImportError: attempted relative import with no known parent package