13

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```
varun krishna
  • 173
  • 1
  • 2
  • 9

10 Answers10

35

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

Satz
  • 307
  • 3
  • 19
Shivachandra
  • 451
  • 4
  • 2
  • 4
    If you have installed uvicorn used pip3 and you are using python3 then change the command to 'python3 -m uvicorn main:app --reload' – Satz Feb 03 '22 at 13:39
11

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 
skbr
  • 151
  • 1
  • 5
6

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.

wim
  • 338,267
  • 99
  • 616
  • 750
3

I used

python3 -m uvicorn main:app --reload

and it worked , as I had multiple versions of python in my pc.

2

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

Sushanth
  • 21
  • 1
2

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
sareno
  • 576
  • 8
  • 10
1

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:\Programme\Python\Python37
D:\Programme\Python\Python37\Scripts
abhilb
  • 5,639
  • 2
  • 20
  • 26
1

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
Biku Shah
  • 107
  • 2
  • 5
  • .bashrc and .bash_profile are bash config files(bash shell script) that bash runs whenever it is started interactively.It initialize an interactive(non-login) shell session and the config is read from these files $HOME/.bashrc – Biku Shah Nov 06 '21 at 01:57
0

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
0

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

Chuan
  • 429
  • 5
  • 16