Flask Installation and Usage Guide: Building Simple Applications

Tutorial on installing and using Flask

Flask is one of the most popular web application frameworks written in Python. It is a micro-framework designed for a simple and quick start-up. Scaling with tools and libraries adds more functionality to Flask for more complex projects.

How does Flask install and use? This article explains how to install Flask in a virtual test environment and create a simple Flask application.

Prerequisite

  • Python 2.7 or Python 3.5 and later installed
  • CLI with administrator privileges

Note: Python 2 has entered the Lifecycle Maintenance state. It is no longer supported because we are in one of the follow-up guides of Python 3 in 2020: how to install Python 3 on CentOS 7, how to install Python 3 on CentOS 8, how to install Python 3 on Ubuntu, how to install Python on Windows.

Step 1: Install the virtual environment

How do I install and use Flask? Install Flask in a virtual environment to avoid library conflict issues. Check the Python version before you begin:

  • Python 3 comes pre-installed with a virtual environment module called venvIf you have Python 3 installed, skip to step 2.
  • Python 2 users must install the virtualenv module. If you have Python 2, follow the instructions outlined in step 1.

Install virtualenv on Linux

The package manager on Linux provides virtualenv.

  • For Debian/Ubuntu:

1. Start by opening the Linux terminal.

2. For apt installation of virtualenv on Debian, Ubuntu and other related distributions:

sudo apt install python-virtualenv
  • For CentOS/Fedora/Red Hat:

1. Open the Linux terminal.

2. Used to install virtualenv on yum on CentOS, Red Hat, Fedora and related distributions:

sudo yum install python-virtualenv

Install virtualenv on MacOS

1. Open Terminal.

2. Install virtualenv on Mac using pip:

sudo python2 -m pip install virtualenv

How does Flask install and use? Install virtualenv on Windows

1. Open the command line with administrator privileges.

2. For installing virtualenv on Windowspip:

py -2 -m pip install virtualenv

Note: To install pip on Windows, follow our guide on how to install pip on Windows.

Step 2: Create an environment

1. Tutorial on Installing and Using Flask – Create a separate directory for your project:

mkdir <project name>

2. Go to the table of contents:

cd <project name>

3. In that directory, create a virtual environment for Flask. When you create an environment, a new folder appears in the project directory with the name of the environment.

Create environments in Linux and MacOS

  • For Python 3:

How do I install and use Flask? To create a virtual environment for Python 3, use the venv module and give it a name:

python3 -m venv <name of environment>
  • For Python 2:

For Python 2, use the virtualenv module to create a virtual environment and name it:

python -m virtualenv <name of environment>

Use the ls command to list the directory structure to display the newly created environment:

How does Flask install and use? Create an environment in Windows

  • For Python 3:

Create and name a virtual environment in Python 3:

py -3 -m venv <name of environment>
  • For Python 2:

For Python 2, use the virtualenv module to create a virtual environment:

py -2 -m virtualenv <name of environment>

Use the following dir command to list the folder structure:

dir *<project name>*

The project directory shows the newly created environment:

Step 3: Activate the environment

Tutorial on installing and using Flask: Activate your virtual environment before installing Flask. Once activated, the name of the activated environment is displayed in the CLI.

Activate the environment on Linux and MacOS

To activate a virtual environment in Linux and MacOS:

. <name of environment>/bin/activate

Activate the environment on Windows

For Windows, use the following command to activate the virtual environment:

<name of environment>\Scripts\activate

Step 4: Install Flask

Use pip to install Flask in the active environment with the following command:

pip install Flask

Flask automatically installs all dependencies.

Note: pip is a Python package manager. To install PIP our guide follows one of the following: how to install an idea on CentOS 7, how to install an idea on CentOS 8, how to install an idea on Debian, or how to install an idea on Ubuntu.

Step 5: Test your development environment

How do I install and use Flask? The detailed steps are as follows:

1. Create a simple Flask application to test the newly created development environment.

2. Create a file named hello.py in the Flask project folder.

3. How does Flask install and use? Edit the file with a text editor and add the following code to make a print “Hello world!“. Applications:

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello world!'

Note: Choose any name for your project, except for flask.py. The Flask library is located in flask.py file.

4. Save the file and close it.

5. Using the console, use the cd command to navigate to the project folder.

6. Set FLASK_APP environment variables.

  • For Linux and Mac:
export FLASK_APP=hello.py
  • For Windows:
setx FLASK_APP "hello.py"

Installation and Use of Flask Tutorial Note: Windows users must restart the console to set environment variables. Learn more about setting environment variables by reading one of our guides: how to set environmet variables in Linux, how to set environment variables on MacOS, how to set environment variables in Windows.

7. Run the Flask app:

flask run

The output prints out a confirmation message and address.

8. Copy and paste the address into your browser to see the project in action:

Conclusion

How do I install and use Flask? The Flask web application is easy to configure and run. It is one of the most popular web application frameworks for Python.

Read about the best Python IDEs and code editors to choose the best environment to use Flask for further web development.