21.2. Install Flask

Note

If you are using Replit you do not need to install Flask or create a virtual environment.

Starting a new Flask project begins with the same basic steps. These are the housekeeping tasks we must complete before diving into our actual code.

In the sections below, you will complete the setup for your first Flask application.

Note

Several of the pages in this chapter include video summaries at the bottom of the page. The clips are intended to offer support. They do NOT serve as a replacement for the reading.

21.2.1. Create a Project Directory

Every application we develop should have its own directory on our machine. This allows us to keep our work organized and track each project with its own Git repository.

  1. On your device, create a new directory called flask_projects. Inside this folder, create a sub-directory called hello_flask.

  2. Launch Visual Studio Code and open the hello_flask folder.

  3. In the terminal pane, use the pwd command to verify that you are in the hello_flask directory.

  4. Use git init to initialize a new repository.

21.2.2. Create a Virtual environment

To run a Flask application, we must create a virtual environment inside the project directory. Think of this like a collection of tools needed to make the application work.

Note

We won’t go into detail about why we need a virtual environment. For now, we just need to know how to set one up.

  1. In the terminal, type the following command.

    Mac Users:
    $ python3 -m venv hello-env
    
    Windows Users:
    $ py -3 -m venv hello-env
    
  2. Be patient! The command takes some time to finish its work, and you won’t see much happen in the terminal while its running. The process is done when the terminal prompt ($) reappears. Also, the folder hello-env shows up inside the project directory.

    The file tree now shows the hello-env directory.

    The hello-env directory contains the tools needed to make our web application run in our browser.

  3. The venv keyword creates a new Virtual ENVironment. In this case, the environment is called hello-env.

Each Flask application you create will have its own virtual environment directory. You can name these environments whatever you want, but a good practice is to use something like project_name-env.

21.2.3. Add Flask

  1. The next step is to activate the virtual environment. In the terminal, make sure you are in the hello-flask directory, then enter the command:

    Mac Users:
    $ . hello-env/bin/activate
    (hello-env) $
    
    Windows Users:
    $ . hello-env/Scripts/activate
    (hello-env) $
    
  2. (hello-env) now appears to the left of the terminal prompt. This indicates which virtual environment is currently active.

  3. Now install Flask with the command:

    (hello-env) $ pip install Flask
    

    Unlike venv, this command produces LOTS of text in the terminal panel.

  4. Once the installation is done, check for success by using the command flask --version.

    (hello-env) $ flask --version
    Python 3.9.5
    Flask 2.0.1
    Werkzeug 2.0.1
    

    Note

    Software updates occur frequently, so the version numbers you see might be slightly different. As long as you have a Python version at or above 3.6, you should be fine.

  5. To exit an environment, just enter the command deactivate in the terminal.

    (hello-env) $ deactivate
    $
    

21.2.4. Ready to Go!

OK, the virtual environment is set up, and Flask is installed. We can now add some Python code and link it to a webpage.

21.2.5. Video Summary

The clip below provides a walkthrough for installing Flask on a Macintosh computer. However, the process is very similar for other operating systems.