uvicorn is not working when called from the terminal

Question:

I tried installing uvicorn on the system via pip3 which worked, however i am unable to run the same from the command line. Any pointers as to how to resolve this?

Requirement already satisfied: uvicorn in /home/vhawk19/.local/lib/python3.7/site-packages (0.10.8)
Requirement already satisfied: uvloop>=0.14.0; sys_platform != "win32" and sys_platform != "cygwin" and platform_py
thon_implementation != "pypy" in /home/vhawk19/.local/lib/python3.7/site-packages (from uvicorn) (0.14.0)
Requirement already satisfied: websockets==8.* in /home/vhawk19/.local/lib/python3.7/site-packages (from uvicorn)
(8.1)
Requirement already satisfied: click==7.* in /home/vhawk19/.local/lib/python3.7/site-packages (from uvicorn) (7.0
)
Requirement already satisfied: h11==0.8.* in /home/vhawk19/.local/lib/python3.7/site-packages (from uvicorn) (0.8
.1)
Requirement already satisfied: httptools==0.0.13; sys_platform != "win32" and sys_platform != "cygwin" and platform
_python_implementation != "pypy" in /home/vhawk19/.local/lib/python3.7/site-packages (from uvicorn) (0.0.13)
vhawk19@api-server:~/api-server$ uvicorn
-bash: uvicorn: command not found```
Asked By: varun krishna

||

Answers:

It looks like your bin dir is not on $PATH.

Execute it directly:

/home/vhawk19/.local/bin/unvicorn

Or just add to path first:

export PATH=$PATH:$HOME/.local/bin

You can put this in your shell rc file.

Answered By: wim

Check your PATH environment variable. It should include the path to unicorn package too.

Not sure about the linux paths, but in windows you need to have these two in your Path environment variable.

D:ProgrammePythonPython37
D:ProgrammePythonPython37Scripts
Answered By: abhilb

After creating new virtual environment if you get
Requirement already satisfied

forcefully uninstall all the packages by executing:
pip freeze > requirements.txt

pip uninstall -r requirements.txt -y

Now check the packages using:
pip list

if uvicorn is present in requirements.txt, commands such as uvicorn app.main:app –reload
will work.

If not install uvicorn using
pip install uvicorn

& then try executing uvicorn app.main:app –reload

Now you should be able to find No packages, or some default packages such as pip, wheel, etc.

Reinstall all the packages from requirements:
pip install -r requirements.txt

Answered By: Sushanth

You can use the command below directly from the cmd. The path is fine for my case, but I still get the error using uvicorn command.

python -m uvicorn 
Answered By: skbr

I ran into the same problem.
I am using pyenv-win for managing Python versions.

The following command managed to fix the issue

$ pyenv rehash
Answered By: sareno

1.First know where the uvicorn is located. You can know this by typing command

$locate uvicorn

Now,select the path that looks something like

/home/username/.local/bin/uvicorn 

2.Then,type command

$ls -a          

here, you can see a hidden file named .bashrc and .bash_profile. Now,we have to add our uvicorn path in ths .bashrc file or in .bash_profile . For this we use nano text editor and write the following in .bashrc and saved it

$nano .bashrc

export PATH:$PATH:/home/username/.local/bin
Answered By: Biku Shah

i recently install fastapi,uvicorn and tried to run

uvicorn main:app --reload

I am using zsh (shell type does not matter) and pyenv for virtual environment

got the same trouble (zsh: uvicorn command not found)

Solution which worked for me

python -m uvicorn main:app --reload

Why it worked

its because when we install uvicorn it install some system binaries which we may have to add to the path for the terminal to catch

else, we need to let the the terminal know about the binaries through python

Answered By: Shivachandra

One very common cause of this issue is that the uvicorn server is not being run from root. Do the following steps to solve this :

  1. Login as root user using command :

sudo -i

  1. Navigate to you virtual env location and activate it
  2. Now restart the uvicorn server
Answered By: Arghya Bhattacharya

sudo pip install and pip install might be in two different locations.

Answered By: Chuan

Fisrt step tried to install uvicorn:

pip3 install uvicorn

then run this command:

python3 -m uvicorn main:app --reload

works.

Answered By: Dolphin
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.