Yaovi
Yaovi

Reputation: 41

Module not found in poetry using poetry run

I’m working on a FastAPI project where I’m managing dependencies with Poetry. Here is my current pyproject.toml file:

[tool.poetry]
name = "navigo-backend"
version = "0.1.0"
description = ""
authors = ["Se"]
readme = "README.md"
packages = [{include = "src"}]

[tool.poetry.dependencies]
python = "^3.12"
fastapi = "^0.115.4"
pydantic-settings = "^2.6.1"
sqlalchemy = "^2.0.36"
python-jose = "^3.3.0"
passlib = "1.6.5"
docling = "^2.4.2"
psycopg2-binary = "^2.9.10"
bcrypt = "2.0.0"

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

My FastAPI app is located in a main.py file under the src directory. To run the app, I use this command:

poetry run uvicorn src.main:app --reload --port 5000

Problem:

When I run this command, I get the following error:

from docling.datamodel.pipeline_options import PdfPipelineOptions ModuleNotFoundError: No module named 'docling'

Additional Details: 1. Dependencies: I’ve added docling as a dependency in my pyproject.toml file under [tool.poetry.dependencies] with version "^2.4.2". 2. Installation: I’ve run poetry install to ensure that all dependencies are installed. 3. Project Structure: My src directory contains the main.py file, which is supposed to launch the app.

Attempts to Resolve: 1. Dependency Check: I ran poetry show docling to confirm that the docling package is installed, and it appears in the list. 2. Python Path: I tried explicitly adding src to PYTHONPATH to ensure that the source directory is recognized.

Question:

Why does Python fail to find docling even though it’s listed as an installed dependency?

Upvotes: 0

Views: 147

Answers (1)

Yaovi
Yaovi

Reputation: 41

The issue is likely due to Poetry managing virtual environments in a way that’s causing conflicts or making it difficult for your environment to access the installed dependencies. Here’s the solution that worked for me:

  1. Disable Poetry’s Virtual Environments:

Run the following command to prevent Poetry from creating virtual environments:

poetry config virtualenvs.create false
  1. Remove Existing Virtual Environments:

If you’ve already installed dependencies in virtual environments, remove them by running:

poetry env remove --all
  1. Reinstall Dependencies:

Finally, reinstall your dependencies without the virtual environment:

poetry install

After this, your dependencies should be available directly to your environment, and the ModuleNotFoundError for docling (or any other dependency) should be resolved.

Now you can run:

poetry run uvicorn src.main:app --reload --port 5000

Upvotes: 0

Related Questions