首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >应用程序使用uvicorn运行,但找不到模块(没有名为“app”的模块)

应用程序使用uvicorn运行,但找不到模块(没有名为“app”的模块)
EN

Stack Overflow用户
提问于 2022-11-30 07:19:54
回答 1查看 22关注 0票数 0
代码语言:javascript
运行
复制
.
├── __pycache__
│   └── api.cpython-310.pyc
├── app
│   ├── __pycache__
│   │   └── main.cpython-310.pyc
│   ├── api_v1
│   │   ├── __pycache__
│   │   │   └── apis.cpython-310.pyc
│   │   ├── apis.py
│   │   └── endpoints
│   │       ├── __pycache__
│   │       │   └── message_prediction.cpython-310.pyc
│   │       └── message_prediction.py
│   ├── config.py
│   ├── main.py
│   └── schemas
│       ├── Messages.py
│       └── __pycache__
│           └── Messages.cpython-310.pyc
├── app.egg-info
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   ├── dependency_links.txt
│   └── top_level.txt
├── build
│   └── bdist.macosx-12.0-arm64
├── data
│   ├── processed
│   │   ├── offers_big.csv_cleaned.xlsx
│   │   └── requests_big.csv_cleaned.xlsx
│   ├── processed.dvc
│   ├── raw
│   │   ├── offers.csv.old
│   │   ├── offers_big.csv
│   │   ├── requests.csv.old
│   │   └── requests_big.csv
│   ├── raw.dvc
│   ├── validated
│   │   ├── validated_offers.xlsx
│   │   └── validated_requests.xlsx
│   └── validated.dvc
├── dist
│   └── app-0.1.0-py3.10.egg
├── model.pkl
├── model.py
├── notebooks
│   └── contact-form.ipynb
├── requirements.in
├── requirements.txt
├── setup.py
└── test_api.py
代码语言:javascript
运行
复制
# main.py
import os
from fastapi import FastAPI
import uvicorn
from app.api_v1.apis import api_router

# create the app
messages_classification_app = FastAPI()

messages_classification_app.include_router(api_router)

if __name__ == '__main__':
    uvicorn.run("app.main:messages_classification_app", host=os.getenv("HOST", "0.0.0.0"), port=int(os.getenv("PORT", 8000)))
代码语言:javascript
运行
复制
# requirements.in
fastapi
uvicorn
-e file:.#egg=app

试图使用fastAPI运行python应用程序,结果是错误的:

代码语言:javascript
运行
复制
 py[learning]  ~/r/v/contact-form-classification   master ±  python app/main.py
Traceback (most recent call last):
  File "/Users/xxxxx/repos/visable/contact-form-classification/app/main.py", line 4, in <module>
    from app.api_v1.apis import api_router
ModuleNotFoundError: No module named 'app'

使用uvicorn直接运行它可以:

代码语言:javascript
运行
复制
 py[learning]  ~/r/v/contact-form-classification   master ±  uvicorn app.main:messages_classification_app
INFO:     Started server process [53665]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

知道为什么吗?研究类似的问题,似乎不适用于我的问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-30 07:23:55

python app/main.py将使app/成为sys.path中的第一个条目,因此sys.path内部的app导入将无法工作。

执行python -m app.main操作,将app/main.py作为一个模块运行,而不需要Python sys.path

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74624111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档