I’m trying to run a FastAPI application using uvicorn
in a virtual environment created with Anaconda on Windows, but I’m facing a CommandNotFoundException
error. Here is what I’ve done so far:
-
Environment Setup:
- Created a virtual environment using Anaconda named
fastapi
. - Activated the environment in anaconda prompt.
- Created a virtual environment using Anaconda named
-
Installed Packages:
-
Installed
FastAPI
anduvicorn
usingpip
:pip install fastapi uvicorn
-
-
Code in
main.py
:- Created a simple FastAPI application in
main.py
:from fastapi import FastAPI app = FastAPI() @app.get("/ping") async def ping(): return "Hello there!"
- Created a simple FastAPI application in
-
Command Used to Run the App:
-
I tried to run the app on vs code terminal using the following command:
uvicorn main:app --reload
-
However, I got the following error:
PS C:\Users\HP\Anaconda_3\envs\fastapi\fastapi_projects> & C:/Users/HP/Anaconda_3/envs/fastapi/python.exe C:/Users/HP/Anaconda_3/envs/fastapi/fastapi_projects/main.py PS C:\Users\HP\Anaconda_3\envs\fastapi\fastapi_projects> uvicorn main:app --reload uvicorn : The term 'uvicorn' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + uvicorn main:app --reload + ~~~~~~~ + CategoryInfo : ObjectNotFound: (uvicorn:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
-
What I’ve Tried:
- Verified that the environment is activated.
- Reinstalled
uvicorn
usingpip install uvicorn
. - Checked the installation with
pip show uvicorn
, which confirmsuvicorn
is installed. - Tried running
uvicorn
directly withuvicorn main:app --reload
, but still got the error.
Environment Details:
- Operating System: Windows 11
- Python Version: Python 3.12.4
- Conda Version: conda 24.1.2
- Virtual Environment: Created using Conda
Question:
What could be causing uvicorn
not to be recognized as a command, and how can I resolve this issue so that I can run my FastAPI application successfully?
I was running the code on VS Code editor after creating a folder fastapi_projects within the fastapi envs folder.