'ModuleNotFoundError when install script with poetry

Here's what my project tree looks like

.
├── project
│   ├── __init__.py
│   ├── db.py
│   └── main.py
├── poetry.lock
├── pyproject.toml

in main.py

from db import DB

in pyproject.toml

[tool.poetry.scripts]
algoex = "project.main:app"

Here's what i've done to install

$ poetry install

Two Questions

  1. When i run project from root dir, it cds into the project dir, is this expected?
$root> ls
project poetry.lock pyproject.toml

$root> project 
$project> ls
__init__.py db.py main.py
  1. When I run project from projectt dir, it throws the error ModuleNotFoundError
$project> project
ModuleNotFoundError: No module named 'db'


Solution 1:[1]

Q1: That's super weird and not expected at all. If I try to recreate this issue following your lead, I get:

bash: project: command not found

The only thing that occurs to me is that you have a project script that is being run instead? Perhaps try:

which project

If there are any executable project paths, that should show them.

Q2: You need to run the app via poetry, otherwise it doesn't inherit it's virtual env (hence the ModuleNotFoundError). Try this:

poetry run python3 project/main.py

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeremy Davis