I have a question related to FastAPI with uvicorn in pycharm. My project is having following structure:
LearningPy <folder name>
|
|-- apis <folder name>
-----|--modelservice <folder name>
---------|--dataprovider.py
---------|--main.py
---------|--persondetails.py
-----|--config.py
First I was using following path : D:\Learnings\apis and ran following code : uvicorn main:app --reload then it was giving error :
Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
Started reloader process [23445]
Error loading ASGI app. Could not import module "apis".
However, after reading suggestion from here, I have changed the path to D:\Learnings\apis\modeservice and above error gone but now it started throwing a different error : ModuleNotFoundError: No module named 'apis'
Here are my main.py and config.py code files :
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
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}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)
And here is config.py--
API_CONFIG = {
"origin_local_ip": "http://127.0.0.1:3000",
"origin_local_url": "http://localhost:3000"
}
This project is being built on React+Mongo+python (pymongo for connecting mongodb).
Thanks in advance.