This video is available to students only

How to Install Flask and Python 3 With Virtual Environments

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

Getting started

Installation

We'll need just a few things to get started with building Flask applications. Crucially we'll need Python 3, a terminal, and a text editor. Many systems come built in with Python 3. If you run into issues, Appendix A has some common issues and resolutions, including installation instructions for Windows.

tip

If you need a text editor, we use Visual Studio Code. Like Flask it's free, and powerful out of the box but can be customized for power users.

Let's start by creating a folder for our Flask projects. This is the folder where we will place each of the example applications we build throughout this book.

mkdir fullstack-flask
cd fullstack-flask

Then let's create a folder for our first Flask application.

mkdir 'first-flask-app'
cd 'first-flask-app'

Now that we have Python 3, we'll want to install Flask. Flask is packaged as a library that we can install using pip, a package manager for Python. Before we do that, we should set up an environment within our project folder where we can specify exactly which version of Python and libraries we would like to use. Python 3 comes built in with a tool to create "virtual environments" called virtualenv. What this will do is reference a version of Python and libraries installed within the specific project folder. Using virtualenv makes it easier to manage multiple projects that may have conflicting dependencies.

When we work without virtual environments and have multiple projects, we can quickly run into conflicts between versions of underlying libraries and Python versions.

Projects will use the system default version of Python and libraries
Projects will use the system default version of Python and libraries

When we use virtual environments, we can isolate the version of Python and which version the project uses.

Virtual environment allows specifying versions on a project level
Virtual environment allows specifying versions on a project level

In order to make virtual environments work, we need to "activate" each one before running our application so that our terminal session will use the correct version before each

Within the first-flask-app folder, we are going to want to create a folder for the virtual environment which we can call env. We'll only need to do this once. By running this command, we create a folder and set up a virtualenv tied to the version of Python we are using. The -m venv flag tells Python to run the built in virtualenv module.

python3 -m venv env

Now that the environment is configured, we'll need to make sure our terminal session uses virtualenv's configuration for Python instead of the defaults. To do so, we run a script to activate the virtual environment.

source env/bin/activate

This will prefix your terminal prompt with (env) to let you know that it has been activated. Now we can install Flask.

pip install flask

Hello World: Dice

Our goal for this part is to have a page that loads in our browser that displays a random number every time we load the page.

In order to have a page that displays a random number, we're going to have write some logic that we can later tell Flask to serve to the browser. Let's start by writing a function that returns the result of a dice roll. We're going to need to create a file to write the function in; we can use the same file to run the Flask web server as well. We'll call this file hello_world.py.

This lesson preview is part of the Fullstack Flask: Build a Complete SaaS App with Flask course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

Unlock This Course

Get unlimited access to Fullstack Flask: Build a Complete SaaS App with Flask with a single-time purchase.

Thumbnail for the \newline course Fullstack Flask: Build a Complete SaaS App with Flask