I have my code starting as such:
if __name__ == "__main__":
uvicorn.run(
app,
host="0.0.0.0",
port=3001, # Desired port.
)
I'm starting my Uvicorn/FastAPI app by running with no port:
uvicorn main:app
But the default port when starting is 8000
; default when no port specified.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
It seems like my port=3001
is ignored in the actual Python code.
From what I understand, the code is specified when no port is specified in the terminal command.
I get the desired port when I specify port in the terminal:
uvicorn main:app --port 3001
Since you are mentioning uvicorn run command in your python file itself, you can simple just run the file and uvicorn will launch the file for you, you dont need to run it like this: uvicorn main:app --port 3001
.
Simply run: python <your_main_file_name.py>
If you execute Python file this way, the port=3001
argument in uvicorn.run()
will be honored.
Otherwise when you do uvicorn main:app --port 3001
, it directly imports your app instance.