Uvicorn can't find modules – Uvicorn

by
Maya Patel
fastapi modulenotfounderror python python-venv uvicorn

Quick Fix: Ensure that you are running uvicorn from within your virtual environment, which contains the necessary dependencies for your project. If not, activate your virtual environment and try executing uvicorn again.

The Problem:

Uvicorn, a popular ASGI server for Python, is unable to locate the application modules, resulting in a "module not found" error. This issue arises when attempting to use Uvicorn to run a Python script (main.py) configured as a FastAPI application. While the script runs successfully with the command "python main.py", using "uvicorn main:app –reload" causes the error. The user has installed FastAPI and Uvicorn in their virtual environment and configured the script to use the FastAPI framework, but Uvicorn is unable to find the required dependencies from within the virtual environment.

The Solutions:

Solution 1: Specify the Path to Virtual Environment

The issue arises when uvicorn is installed outside the virtual environment where your dependencies are located. As a result, when you run uvicorn main:app --reload, it attempts to utilize the global installation of uvicorn instead of the one within your virtual environment. Consequently, it fails to find the necessary dependencies.

To resolve this problem, explicitly specify the path to your virtual environment’s uvicorn installation when executing the command. This ensures that the correct version of uvicorn is used, which includes the required dependencies.

./venv/bin/uvicorn main:app --reload

Replace ./venv/ with the actual path to your virtual environment folder.

By utilizing this approach, you can guarantee that the uvicorn command utilizes the appropriate installation, eliminating the module not found errors and allowing your FastAPI application to run smoothly.

Solution 2: Activate Virtual Environment

To resolve the issue of Uvicorn not finding modules, ensure that you have properly activated your virtual environment before running your Python file.

To activate the virtual environment on Linux:

  1. Navigate to the directory containing your virtual environment.
  2. Run the following command:
source your_project_location/venv/bin/activate

On Windows, you can activate the virtual environment by running the following command:

.\your_project_location\venv\Scripts\activate.bat

Once the virtual environment is activated, Uvicorn should be able to find the necessary modules and dependencies installed within the virtual environment.

It’s important to note that any dependencies you install using pip should be installed while the virtual environment is activated. This ensures that the dependencies are installed within the virtual environment and not the system’s global Python environment.

By activating the virtual environment and installing the required dependencies within it, Uvicorn should be able to successfully locate and import the necessary modules when running your Python file.

Solution 3: Use the Correct Path to Pip

The issue was related to the path used for pip. Specifically, when executing pip commands, the /usr/local path was being used instead of the venv path. This resulted in uvicorn not being able to locate the dependencies installed in the venv.

To rectify this problem:

  • Identify the path to the venv’s pip executable. This can typically be found by running which pip within the venv.
  • Utilize this path when installing uvicorn and other dependencies. For instance, instead of typing pip install uvicorn, use the full path to the venv’s pip, such as /path/to/venv/bin/pip install uvicorn.
  • Verify that the correct path is being used by running which uvicorn. It should now display the path to the venv’s uvicorn executable.

This approach ensures that uvicorn and its dependencies are correctly installed within the venv, resolving the module not found errors.

Q&A

Why uvicorn does not detect the dependencies despite being installed in the venv?

Uvicorn might not be installed inside the venv.

How to ensure uvicorn is running from the venv and not a global installation?

Create a new venv, make it active before installing any module, update pip, then install uvicorn and FastAPI within the venv.

What can be done if Uvicorn is still not working?

Make sure the venv is activated properly and that there aren’t any syntax errors in the code.

Video Explanation:

The following video, titled "Live Coding: Refactoring Docker Related Files in a FastAPI App ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... uvicorn CMD with docker-compose exec 36:15 -- Removing the reference ... Your browser can't play this video. Learn more.