ModuleNotFoundError - import in Python while running uvicorn is not being resolved

9.5k views Asked by At

I do have simple FastAPI app and I have used pip and venv to manage environment. When I run it via uvicorn installed package my import is not visible. It works in python terminal.

simplified folder structure: /venv /src main.py .. requirements.txt ..

requirements.txt

alembic==1.4.3
click==7.1.2
fastapi==0.61.1
FastAPI-SQLAlchemy==0.2.1
pydantic==1.6.1
SQLAlchemy==1.3.20
uvicorn==0.12.2
python-dotenv==0.14.0

In python shell after I will activate environment importing fastapi_sqlalchemy works:

Python 3.8.5 (default, Aug 25 2020, 14:19:38) 
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import fastapi_sqlalchemy
>>> 

But when I will run fastapi via uvicorn the app will throw error on fastapi_sqlalchemy import.

uvicorn src.main:app --reload                                                                                                                                                                                                     ✔  16:58:54
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [40567] using statreload
3.8.5 (default, Aug 25 2020, 14:19:38) 
[Clang 11.0.3 (clang-1103.0.32.62)]
Process SpawnProcess-1:
Traceback (most recent call last):
(... stacktrace here ...)

 File "./src/main.py", line 8, in <module>
import fastapi_sqlalchemy
ModuleNotFoundError: No module named 'fastapi_sqlalchemy'

src/main.py

import uvicorn
from fastapi import FastAPI
from dotenv import load_dotenv

import sys
print(sys.version)

import fastapi_sqlalchemy

I do not know how to make this library visible into context of running uvicorn/fastapi app.

2

There are 2 answers

0
BlueConga On

In the end... I did restarted my computer and it worked ‍♂️

Thank everyone who took a look into this one.

0
Moha On
import fastapi_sqlalchemy 

should be

import FastAPI-SQLAlchemy