fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

[Question] Why ModuleNotFoundError: No module found

Open miriam-z opened this issue 4 years ago • 7 comments

Example

from fastapi import FastAPI
import pandas.io.sql as sqlio
import psycopg2

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}

uvicorn main:app --reload

Description

import psycopg2

ModuleNotFoundError: No module named 'psycopg2'

Environment

  • OS: [e.g. Linux / Windows / macOS]:

  • Big Sur 11.4

  • FastAPI Version [e.g. 0.3.0]:

To know the FastAPI version use:

python -c "import fastapi; print(fastapi.__version__)"

0.66.0

  • Python version:

To know the Python version use:

python --version

3.9.5

Additional context

pip3 list
Package Version


aiofiles 0.7.0 fastapi 0.66.0 numpy 1.21.0 pandas 1.2.5 pip 21.1.2 psycopg2 2.9.1 pydantic 1.8.2 rope 0.19.0 setuptools 52.0.0.post20210125 six 1.16.0 starlette 0.14.2 toml 0.10.2 tornado 6.1 traitlets 5.0.5 typing-extensions 3.10.0.0 uvicorn 0.14.0

miriam-z avatar Jul 05 '21 17:07 miriam-z

You need to do pip install psycopg2-binary as well

ArcLightSlavik avatar Jul 05 '21 20:07 ArcLightSlavik

Sorry I didnt make it clear, it is not just psycopg2 but pandas also not found as well

miriam-z avatar Jul 05 '21 21:07 miriam-z

Have you use virtual environment? Maybe you can provide with stack traceback

hail-pas avatar Jul 06 '21 01:07 hail-pas

@miriam-z This isn't a FastAPI issue, you need to make use of virtual environments or something like pipenv.

aminalaee avatar Jul 06 '21 14:07 aminalaee

hey! i have the same problem. is it solved? if so can you explain it please?

mehmetavnicelik avatar Feb 15 '22 09:02 mehmetavnicelik

I have the same problem too. How can I deal with it?

tqd9563 avatar Jul 21 '22 05:07 tqd9563

This is not a FastAPI issue but an import issue. It’s impossible to provide a general solution, as there can be many causes.

JarroVGIT avatar Jul 21 '22 05:07 JarroVGIT