Last modified: Aug 11, 2026

Fix Flask ModuleNotFoundError: Quick Guide

Encountering ModuleNotFoundError: No module named 'flask' is a common hurdle for Python developers. This error stops your web app before it even starts.

The good news? The fix is usually straightforward. This guide walks you through every solution, from basic checks to advanced environment setups.

By the end, you will run your Flask application without errors. Let's dive in.

What Causes This Error?

This error means Python cannot find the Flask library in your current environment. It's not a code syntax problem. It's an environment or installation issue.

Common reasons include Flask not being installed, using the wrong Python interpreter, or working outside a virtual environment.

Let's look at a typical error output to understand it better.


$ python app.py
Traceback (most recent call last):
  File "app.py", line 1, in <module>
    from flask import Flask
ModuleNotFoundError: No module named 'flask'

The traceback points to the import statement. Python searches its known paths for the module. When missing, it raises the error.

Solution 1: Install Flask with pip

The most direct fix is installing Flask using pip. Open your terminal or command prompt.

Run the following command:


pip install flask

For Python 3, you might need to use pip3 instead. This depends on your system's configuration.


pip3 install flask

If you're on macOS or Linux, you may need sudo for system-wide installs. However, using a virtual environment is better practice.

After installation, verify it worked. Run a quick Python command to check the version.


import flask
print(flask.__version__)

2.3.3

If you see a version number, the installation succeeded. Try running your app again.

Solution 2: Use a Virtual Environment

Global installations can cause conflicts. Virtual environments isolate your project dependencies. This is the recommended approach.

First, create a virtual environment in your project directory.


python -m venv venv

This creates a folder named venv. Next, activate it. The command differs by operating system.

On Windows, use:


venv\Scripts\activate

On macOS and Linux, use:


source venv/bin/activate

You should see (venv) appear in your terminal prompt. Now install Flask inside this environment.


pip install flask

Now run your application. The error should be gone. If you still have issues, check your Python interpreter.

Solution 3: Check Your Python Interpreter

Sometimes your IDE or editor uses a different Python than your terminal. This mismatch causes the error.

In VS Code, press Ctrl+Shift+P and select "Python: Select Interpreter". Choose the one from your virtual environment.

In PyCharm, go to Settings > Project > Python Interpreter. Select the correct one.

To verify which Python your terminal uses, run:


which python

/usr/bin/python

If it's not pointing to your virtual environment, you're using the wrong one. Activate the environment first.

This simple check resolves many installation headaches.

Solution 4: Upgrade pip and Setuptools

Outdated pip can fail to install packages correctly. Upgrading pip and setuptools often fixes hidden issues.

Run these commands in your terminal:


pip install --upgrade pip setuptools wheel

After upgrading, try installing Flask again.


pip install flask

This solves many dependency-related problems. It's a good practice to do this regularly.

If you still see the error, check for typos in your import statement.

Solution 5: Check for Typos in Your Code

Ensure you wrote from flask import Flask correctly. A simple typo like flask vs Flask matters.

The module name is lowercase flask. The class is uppercase Flask. Mixing them up causes errors.

Here's a correct example:


from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask!"

if __name__ == '__main__':
    app.run(debug=True)

Save this as app.py and run it. If it works, your issue was a typo.

Solution 6: Reinstall Flask Completely

A broken installation can cause persistent errors. Uninstalling and reinstalling Flask often fixes corrupted files.

First, uninstall Flask.


pip uninstall flask

Confirm the removal when prompted. Then reinstall it fresh.


pip install flask

This clears up any incomplete installations. It's a reliable last resort before deeper troubleshooting.

If the error persists, check your PATH variables or consider using a requirements file.

Solution 7: Use a Requirements File

For larger projects, list all dependencies in a requirements.txt file. This ensures consistency across environments.

Create a file named requirements.txt with this content:


flask==2.3.3

Then install all dependencies from this file.


pip install -r requirements.txt

This approach prevents version mismatches. It's essential for team projects and deployments.

If you're still stuck, the problem might be deeper. Let's explore advanced fixes.

Advanced: Check Your PATH and Environment Variables

Sometimes, Python's script directory isn't in your PATH. This prevents pip from working correctly.

You can add Python's Scripts folder to your PATH manually. On Windows, it's usually:


C:\Python39\Scripts

On macOS/Linux, it's typically in /usr/local/bin. Check your system's documentation.

Alternatively, run pip as a module to ensure you're using the right Python.


python -m pip install flask

This guarantees pip installs to the same Python you're using to run your script.

This method is highly recommended for multi-Python setups.

Common Mistakes to Avoid

Avoid installing Flask globally if you work on multiple projects. It leads to version conflicts.

Always use a virtual environment for each project. This isolates dependencies.

Don't ignore the error message. Read the full traceback; it often gives clues.

Check your working directory. Ensure you're in the right folder when running commands.

If you're still facing issues, you might find this guide helpful: How To Solve ModuleNotFoundError: No module named in Python. It covers general solutions for all modules.

Testing Your Flask Setup

After fixing the error, test your setup with a simple app. This confirms everything works.

Create a minimal Flask app and run it.


# app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run()

Run it with:


python app.py

You should see output similar to this:


 * Serving Flask app 'app'
 * Debug mode: off
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)

Open your browser at http://127.0.0.1:5000. You should see "Hello, World!".

This confirms your installation is perfect.

Conclusion

The ModuleNotFoundError: No module named 'flask' error is frustrating but solvable. Start with installing Flask using pip. Then, verify your environment.

Use virtual environments to avoid future conflicts. Check your Python interpreter in your IDE. Upgrade pip regularly to prevent subtle issues.

If problems persist, reinstall Flask or use a requirements file. For broader Python import errors, refer to our comprehensive guide on ModuleNotFoundError.

Remember, the key is to match the Python interpreter with the pip installation. Once aligned, Flask will run smoothly.

Now you're ready to build amazing web applications with Flask. Happy coding!