Now that we understand the role of web frameworks in creating APIs for our models, let's get Flask, our chosen tool, set up in our development environment. Flask is a microframework for Python, meaning it provides the basics for building web applications without imposing a lot of structure or dependencies. This makes it a great choice for creating straightforward prediction services.
Before installing Flask, ensure you have Python installed on your system. Flask requires Python 3.7 or newer. Along with Python, you should have pip
, the standard package installer for Python. You can typically check your Python version by opening a terminal or command prompt and running:
python --version
# or maybe python3 --version
And check your pip version with:
pip --version
# or maybe pip3 --version
If you have Python, pip
usually comes bundled with it.
It's highly recommended to install Flask and other project dependencies within a virtual environment. A virtual environment provides an isolated Python environment for your project. This means that the packages you install for this project won't interfere with packages installed globally or for other projects, preventing potential version conflicts.
Create a Virtual Environment:
Navigate to your project directory in your terminal or command prompt. Then, run the following command to create a virtual environment. We'll name our environment venv
(a common convention), but you can choose a different name.
python -m venv venv
This command creates a venv
folder in your project directory containing a copy of the Python interpreter and a place to install project-specific libraries.
Activate the Virtual Environment: Before installing Flask, you need to activate the environment. The activation command differs slightly depending on your operating system:
On macOS and Linux:
source venv/bin/activate
On Windows (Command Prompt):
.\venv\Scripts\activate
On Windows (PowerShell):
.\venv\Scripts\Activate.ps1
(You might need to adjust your PowerShell execution policy: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
)
Once activated, your terminal prompt will usually change to show the name of the active environment (e.g., (venv) your-prompt$
), indicating that commands like pip
will now operate within this isolated environment.
With your virtual environment activated, installing Flask is straightforward using pip
:
pip install Flask
This command tells pip
to download Flask and its required dependencies (like Werkzeug for handling HTTP requests and responses, and Jinja2 for templating, though we won't focus on templating here) from the Python Package Index (PyPI) and install them into your active venv
.
You can quickly verify that Flask was installed correctly. One way is to ask Flask for its version:
python -m flask --version
Alternatively, you can start a Python interpreter within your activated virtual environment and try importing Flask:
python
Then, inside the Python interpreter:
>>> import flask
>>> print(flask.__version__)
# This should print the installed Flask version, e.g., 2.3.2
>>> exit()
If these commands run without errors and show a version number, Flask is successfully installed in your virtual environment and ready to use.
Now that Flask is installed, we have the foundation needed to build our first simple web application, which will eventually serve predictions from our saved machine learning model. In the next section, we'll write the basic code for a Flask application.
© 2025 ApX Machine Learning