繁体   English   中英

如何在 vscode 上使用 Python 扩展在运行时调试?

[英]How to debugging on runtime using Python extension on vscode?

首先,我已经开始使用 react+python 来学习和进步。 我来自 .Net MVC 背景,非常了解 Visual Studio 如何在开发环境中提供调试。 但是,在这里我对同一部分有点困惑。 我正在使用带有 python 扩展名的 vscode,如官方线程中所建议那样。 该项目using uvicorn main:app --reload运行良好,但我无法在此处调试代码,因为它永远不会 go 回到 vs 代码。

这是我的项目结构:

LearningPy <folder name>  
    |-- apis <folder name>     
         |--modelservice <folder name>  
             |--dataprovider.py  
             |--persondetails.py
             |--examtemplate.py  
         |--main.py    
         |--config.py  

这是我的 main.py:

import uvicorn
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from datetime import datetime

from apis import config
from apis.modelservice import dataprovider,examtemplate

app = FastAPI(debug=True)
def get_application() -> FastAPI:
    application = FastAPI(title="PersonProfile", description="Learning Python CRUD",version="0.1.0")
    origins = [
        config.API_CONFIG["origin_local_ip"],
        config.API_CONFIG["origin_local_url"]
    ]
    application.add_middleware(
        CORSMiddleware,
        allow_origins=origins,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    #application.include_router(processA.router)
    return application

app = get_application()

@app.get("/")
def read_root():
    return {"main": "API Server " + datetime.now().strftime("%Y%m%d %H:%M:%S")}

@app.get("/dbcheck")
def read_root():
    try:
        dataprovider.get_db().get_collection("Person")
    except Exception as e:
        return {"failed":e}
    else:
        return { "connected":True}

 @app.post("/upload")
 def read_excel():    
    try:   
    #call here from readexcel.py
        examtemplate.read_excel_data()
    except Exception as e:
            return {"failed":e}

理解上述问题的原因:

如果我在一页中编写所有代码,那么它运行良好,我得到 output,但是当我使用文件夹结构时,它不会按预期工作。

让我知道我错过了什么。

提前致谢

您需要在“launch.json”中添加:

"env": {
        "PYTHONPATH": "${workspaceFolder}"
       },

解释:python 解释器找不到 'apis' package,因为 'LearningPy' 不在PYTHONPATH变量中,这会导致这些语句出错:

from apis import config
from apis.modelservice import dataprovider,examtemplate

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM