ModuleNotFoundError-运行uvicorn时在Python中导入无法解决

问题描述

我确实有一个简单的FastAPI应用,并且使用 pip venv 来管理环境。当我通过安装了uvicorn的软件包运行它时,导入不可见。它可以在python终端中工作。

简化的文件夹结构: / 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

在python shell中,我将激活环境,然后导入fastapi_sqlalchemy即可:

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
>>> 

但是当我将通过uvicorn运行fastapi时,应用程序会在 fastapi_sqlalchemy 导入时引发错误。

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,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

我不知道如何在运行uvicorn / fastapi应用的上下文中使该库可见。

解决方法

最后...我确实重新启动了计算机,它可以正常工作?‍♂️

感谢所有浏览此文档的人。

,
import fastapi_sqlalchemy 

应该是

import FastAPI-SQLAlchemy

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...