Skip to main content

How to Solve "ModuleNotFoundError: No module named 'fastapi'" in Python

The error ModuleNotFoundError: No module named 'fastapi' in Python indicates that the fastapi library is not installed in the Python environment you're using, or that there's a problem with how your environment is configured.

This guide provides a complete explanation for resolving this issue, covering installation, environment setup, and common troubleshooting steps.

Understanding the Error

The ModuleNotFoundError means that Python can not find the fastapi package when you try to import it. This usually happens because:

  • FastAPI is not installed: You haven't installed the library in your current Python environment.

  • Wrong Environment: You installed fastapi in a different Python environment (e.g., a virtual environment) than the one your script or IDE is using.

  • Typo: You have a typo in your import statement (though this would usually be ModuleNotFoundError: No module named 'fasttapi', for instance - misspelling the name).

  • Shadowing: You have a local module named the same as the package you want to install, and the interpreter tries to import from the local module instead.

Installation and Basic Setup

Installing fastapi

The standard way to install fastapi is using pip:

pip install fastapi

# OR, for Python 3 (might be pip3, pip3.10, etc. on your system)
pip3 install fastapi

# OR, if pip is not in your PATH
python -m pip install fastapi
python3 -m pip install fastapi

# OR, for Windows 'py' launcher
py -m pip install fastapi

Choose the command that works correctly on your system. If you have multiple Python versions, be mindful of which pip you use.

Installing an ASGI Server (Uvicorn)

FastAPI is an ASGI (Asynchronous Server Gateway Interface) framework. It requires an ASGI server to run. uvicorn is the recommended server for FastAPI. Install it like this:

pip install "uvicorn[standard]"

# OR (adapt as needed for your system)
pip3 install "uvicorn[standard]"

# OR, if pip is not in your PATH
python -m pip install "uvicorn[standard]"
python3 -m pip install "uvicorn[standard]"

# OR, for Windows 'py' launcher
py -m pip install "uvicorn[standard]"

A Minimal FastAPI Example

Create a file named main.py with the following code:

# main.py
from typing import Union
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}

To run this example:

uvicorn main:app --reload --port 8500
  • uvicorn main:app: This starts the Uvicorn server. main refers to the main.py file, and app refers to the FastAPI instance within that file.
  • --reload: This enables automatic reloading of the server when you change your code – very useful during development.
  • --port 8500: Specifies the port.

Open your web browser and go to http://localhost:8500. You should see {"Hello": "World"}. You can also visit http://localhost:8500/items/5?q=somequery to see the other endpoint in action.

Troubleshooting "ModuleNotFoundError"

If you still get the error after installation, follow these steps:

Check Installation

Verify that fastapi is installed in the correct environment:

pip show fastapi
# OR
python -m pip show fastapi

This command should show you the package information, including the installation location. If it says "Package(s) not found", then either the package is genuinely not installed, or you're checking in the wrong environment.

Virtual Environments

Are you using a virtual environment? If so, make absolutely sure you've activated it. The commands to activate vary depending on your OS and shell. Common activation commands:

  • Linux/macOS (bash/zsh): source venv/bin/activate
  • Windows (Command Prompt): venv\Scripts\activate.bat
  • Windows (PowerShell): venv\Scripts\Activate.ps1

Once activated, pip show fastapi should work correctly.

IDE Configuration (VS Code, PyCharm)

If you're using an IDE, ensure it's using the correct Python interpreter:

  • VS Code: Use the "Python: Select Interpreter" command from the Command Palette (Ctrl+Shift+P or Cmd+Shift+P). Choose the interpreter associated with your virtual environment (if any) or the correct system-wide Python installation.

  • PyCharm: Go to File > Settings > Project > Python Interpreter (or PyCharm > Preferences > Project > Python Interpreter on macOS). Select the correct interpreter.

Naming Conflicts

Make absolutely sure you don't have a file or directory named fastapi.py (or just fastapi) in your project directory or anywhere else on your PYTHONPATH. This will "shadow" the installed fastapi package. Rename your file if this is the case.

Restarting the Kernel/IDE

Sometimes, especially with Jupyter Notebooks or IDEs, a restart of the Python kernel or the entire IDE is necessary for changes to be recognized.

Installation Instructions for Specific Environments

Anaconda

conda install -c conda-forge fastapi
conda install -c conda-forge uvicorn

Jupyter Notebook

!pip install fastapi
!pip install "uvicorn[standard]"
  • You have to add the ! exclamation mark before your pip calls to indicate that you are using shell commands.

Conclusion

The ModuleNotFoundError: No module named 'fastapi' error is almost always an installation or environment issue.

By systematically checking your installation, ensuring you're using the correct Python environment, and avoiding naming conflicts, you can quickly resolve this problem and start building amazing APIs with FastAPI.

Remember that you also need to install the ASGI server uvicorn to actually run your fastapi application.