How to Install Flask and Python 3 With Virtual Environments
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.
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.
When we use virtual environments, we can isolate the version of Python and which version the project uses.
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
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
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.
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