加载 ASGI 应用程序时出错。无法导入模块 "src.main"
Error loading ASGI app. Could not import module "src.main"
我看到解决方案正在改变 main
-> src.main
所以我试过了,但是即使我使用 cd src
移动到 src 和 运行 uvicorn main:app --reload
,错误继续。
我需要什么?
我正在使用虚拟环境,但我尝试了 运行没有虚拟机的 ning uvicorn,但我遇到了同样的问题。
代码结构
.
|__ Alembic/
|__ venv/
|__ src/
|__ main.py
|__ services.py
|__ database.py
|__ models.py
|__ __init__.py
我认为问题来自 VSCode Python 解释器
试试这个按此顺序:
- 在 VSCode 终端中,转到您的项目根文件夹
myProjectRootFolder
|__ ...
|__ venv/
|__ src/
|__ ...
|__ main.py
|__ __init__.py
- 激活名为
venv
的虚拟环境
source venv/bin/activate
- 安装所需的依赖项
pip install fastapi uvicorn
- 现在! Select Python 解释器 Ctrl+Shift+P (macOS: Cmd+Shift+P)
- 停用你的venv(如果你不停用你的venv,你会遇到你之前提到的同样的错误)
deactivate
- 再次激活
source venv/bin/activate
- 运行 你的代码
# if you are in the root folder
uvicorn src.main:app
# or
# if you are in the src folder
uvicorn main:app
所以我成功了,但我不知道怎么做。但是我所做的是将所有导入更改为模块导入。
所以而不是
import database
或
from . import database
我做到了:
from src import database
或
import src.database
我看到解决方案正在改变 main
-> src.main
所以我试过了,但是即使我使用 cd src
移动到 src 和 运行 uvicorn main:app --reload
,错误继续。
我需要什么?
我正在使用虚拟环境,但我尝试了 运行没有虚拟机的 ning uvicorn,但我遇到了同样的问题。
代码结构
.
|__ Alembic/
|__ venv/
|__ src/
|__ main.py
|__ services.py
|__ database.py
|__ models.py
|__ __init__.py
我认为问题来自 VSCode Python 解释器
试试这个按此顺序:
- 在 VSCode 终端中,转到您的项目根文件夹
myProjectRootFolder
|__ ...
|__ venv/
|__ src/
|__ ...
|__ main.py
|__ __init__.py
- 激活名为
venv
的虚拟环境
source venv/bin/activate
- 安装所需的依赖项
pip install fastapi uvicorn
- 现在! Select Python 解释器 Ctrl+Shift+P (macOS: Cmd+Shift+P)
- 停用你的venv(如果你不停用你的venv,你会遇到你之前提到的同样的错误)
deactivate
- 再次激活
source venv/bin/activate
- 运行 你的代码
# if you are in the root folder
uvicorn src.main:app
# or
# if you are in the src folder
uvicorn main:app
所以我成功了,但我不知道怎么做。但是我所做的是将所有导入更改为模块导入。
所以而不是
import database
或
from . import database
我做到了:
from src import database
或
import src.database