How to Solve "ModuleNotFoundError: No module named 'flask'" in Python
The ModuleNotFoundError: No module named 'flask'
error in Python indicates that the flask
library is not installed in the Python environment you're currently using, or that Python can not find the installation.
This guide explain how to install Flask, troubleshoot common installation problems, and configure your IDE (VS Code, PyCharm) and Jupyter Notebook to use the correct environment.
Installing Flask (The Basic Solution)
The standard way to install Flask is using pip
, Python's package installer:
pip install Flask
-
Multiple Python Versions: If you have multiple Python versions, you might need to use
pip3
(or a more specific version likepip3.9
,pip3.10
):pip3 install Flask
-
Permissions Errors: If you get a permissions error (common on Linux/macOS), you can try using
sudo
:sudo pip3 install Flask # Use with caution!
A better solution is to install into a user site-packages directory:
pip install Flask --user # Installs for current user only
-
python -m pip
: Ifpip
isn't directly in yourPATH
, use:python -m pip install Flask # Or python3 -m pip
- You can also use
py
in Windows
py -m pip install Flask
- You can also use
-
Conda: If you are using Anaconda use the following command to install Flask.
conda install -c anaconda flask
Verifying the Installation
After installation, verify that Flask is accessible:
from flask import Flask
print(Flask.__version__) # Verify the version.
# Basic Flask Application
app = Flask(__name__) # Minimal Flask app
@app.route("/")
def hello():
return "<p>Hello, World</p>"
if __name__ == '__main__':
app.run(debug=True) # Runs the application
If this script runs without a ModuleNotFoundError
, Flask is installed correctly. If you still get the error, proceed to troubleshooting.
Troubleshooting ModuleNotFoundError
Virtual Environments (Essential)
Always use virtual environments. They isolate project dependencies, preventing conflicts and ensuring reproducibility.
-
Create:
python3 -m venv venv # Or python -m venv venv, or py -m venv venv
-
Activate:
- Linux/macOS:
source venv/bin/activate
- Windows (cmd.exe):
venv\Scripts\activate.bat
- Windows (PowerShell):
venv\Scripts\Activate.ps1
- If you are getting the error
Activate.ps1 can not be loaded because running scripts is disabled on this system
, you will have to set the execution policy by usingSet-ExecutionPolicy RemoteSigned -Scope CurrentUser
in the powershell.
- If you are getting the error
- Linux/macOS:
-
Install:
pip install Flask
(inside the activated environment)
Your terminal prompt should change (e.g., (venv) $
). This indicates the environment is active.
Multiple Python Versions
- Check Python Version:
python --version
(orpython3 --version
) - Use the Correct
pip
: Usepip
,pip3
,pip3.x
, orpython -m pip
corresponding to the correct Python version.
IDE Configuration (VS Code, PyCharm)
Your IDE must be configured to use the correct interpreter (and virtual environment).
-
VS Code:
- Command Palette (Ctrl+Shift+P / Cmd+Shift+P).
- "Python: Select Interpreter"
- Choose the correct environment.
-
PyCharm:
- "File" -> "Settings" -> "Project" -> "Python Interpreter".
- Select/add the correct interpreter.
Jupyter Notebook
In Jupyter, install within a cell:
!pip install Flask
- This installs to the Jupyter kernel environment.
Naming Conflicts
Never name your Python file or a directory in your project flask.py
. This will conflict with the actual flask
module. Rename your file.
Reinstalling Flask
As a last resort, try:
pip uninstall Flask
pip install Flask
Installation Instructions for Specific Environments
The instructions for installation are very similar for most platforms:
- Windows: Use the Command Prompt.
- Linux/macOS: Use the terminal
- Anaconda: Use
conda install -c anaconda flask
, or Anaconda Navigator. - Jupyter: Use
!pip install Flask
Handling "Import 'flask' could not be resolved from source Pylance" (VS Code)
- Check the interpreter path by using the Select Interpreter command.
- You can also try restarting VS Code.
- You can disable the warning by adding
# type: ignore
to the import statement.import flask # type: ignore
print(flask)
Conclusion
The ModuleNotFoundError: No module named 'flask'
error is usually straightforward to resolve.
- The key is to install Flask in the correct Python environment and ensure your IDE or script is using that environment.
- Virtual environments are essential for good Python development practices.
By following these steps, you can quickly fix the error and get your Flask application running.