【问题标题】:How to debugging on runtime using Python extension on vscode?如何在 vscode 上使用 Python 扩展在运行时调试?
【发布时间】:2020-07-23 11:10:56
【问题描述】:

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

理解上述问题的原因:

如果我在一页中编写所有代码,那么它运行良好并且我得到了输出,但是当我使用文件夹结构时,它就无法正常工作。

让我知道我错过了什么。

提前致谢

【问题讨论】:

    标签: python python-3.x visual-studio-code


    【解决方案1】:

    你需要在“launch.json”中添加这个:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      • 2022-07-12
      • 2020-07-12
      • 1970-01-01
      • 2019-03-07
      相关资源
      最近更新 更多