Chapter 1. Installation
Flask is a small framework by most standards—small enough to be called a “micro-framework,” and small enough that once you become familiar with it, you will likely be able to read and understand all of its source code.
But being small does not mean that it does less than other frameworks. Flask was designed as an extensible framework from the ground up; it provides a solid core with the basic services, while extensions provide the rest. Because you can pick and choose the extension packages that you want, you end up with a lean stack that has no bloat and does exactly what you need.
Flask has three main dependencies. The routing, debugging, and Web Server Gateway Interface (WSGI) subsystems come from Werkzeug; the template support is provided by Jinja2; and the command-line integration comes from Click. These dependencies are all authored by Armin Ronacher, the author of Flask.
Flask has no native support for accessing databases, validating web forms, authenticating users, or other high-level tasks. These and many other key services most web applications need are available through extensions that integrate with the core packages. As a developer, you have the power to cherry-pick the extensions that work best for your project, or even write your own if you feel inclined to. This is in contrast with a larger framework, where most choices have been made for you and are hard or sometimes impossible to change.
In this chapter, you will learn how to install Flask. The only requirement is a computer with Python installed.
Note
The code examples in this book have been verified to work with Python 3.5 and 3.6. Python 2.7 can also be used if desired, but given that this version of Python is not going to be maintained after the year 2020, it is strongly recommended that you use the 3.x versions.
Note
If you plan to use a Microsoft Windows computer to work with the example code, you have to decide if you want to use a “native” approach based on Windows tools, or set up your computer in a way that allows you to adopt the more mainstream Unix-based toolset. The code presented in this book is largely compatible with both approaches. In the few cases where the approaches differ, the Unix solution is followed, and alternatives for Windows are noted.
If you decide to follow a Unix workflow, you have a few options. If you are using Windows 10, you can enable the Windows Subsystem for Linux (WSL), which is an officially supported feature that creates an Ubuntu Linux installation that runs alongside the native Windows interface, giving you access to a bash shell and the complete set of Unix-based tools. If WSL is not available on your system, another good option is Cygwin, an open-source project that emulates the POSIX subsystem used by Unix and provides ports of a large number of Unix tools.
Creating the Application Directory
To begin, you need to create the directory that will host the example code, which is available in a GitHub repository. As discussed in “How to Work with the Example Code”, the most convenient way to do this is by checking out the code directly from GitHub using a Git client. The following commands download the example code from GitHub and initialize the application to version 1a
, which is the initial version you will work with:
$ git clone https://github.com/miguelgrinberg/flasky.git $ cd flasky $ git checkout 1a
If you prefer not to use Git and instead manually type or copy the code, you can simply create an empty application directory as follows:
$ mkdir flasky $ cd flasky
Virtual Environments
Now that you have the application directory created, it is time to install Flask. The most convenient way to do that is to use a virtual environment. A virtual environment is a copy of the Python interpreter into which you can install packages privately, without affecting the global Python interpreter installed in your system.
Virtual environments are very useful because they prevent package clutter and version conflicts in the system’s Python interpreter. Creating a virtual environment for each project ensures that applications have access only to the packages that they use, while the global interpreter remains neat and clean and serves only as a source from which more virtual environments can be created. As an added benefit, unlike the system-wide Python interpreter, virtual environments can be created and managed without administrator rights.
Creating a Virtual Environment with Python 3
The creation of virtual environments is an area where Python 3 and Python 2 interpreters differ. With Python 3, virtual environments are supported natively by the venv
package that is part of the Python standard library.
Note
If you are using the stock Python 3 interpreter on an Ubuntu Linux system, the standard venv
package is not installed by default. To add it to your system, install the python3-venv
package as follows:
$ sudo apt-get install python3-venv
The command that creates a virtual environment has the following structure:
$ python3 -m venv virtual-environment-name
The -m venv
option runs the venv
package from the standard library as a standalone script, passing the desired name as an argument.
You are now going to create a virtual environment inside the flasky directory. A commonly used convention for virtual environments is to call them venv, but you can use a different name if you prefer. Make sure your current directory is set to flasky, and then run this command:
$ python3 -m venv venv
After the command completes, you will have a subdirectory with the name venv inside flasky, with a brand-new virtual environment that contains a Python interpreter for exclusive use by this project.
Creating a Virtual Environment with Python 2
Python 2 does not have a venv
package. In this version of the Python interpreter, virtual environments are created with the third-party utility virtualenv.
Make sure your current directory is set to flasky, and then use one of the following two commands, depending on your operating system. If you are using Linux or macOS, the command is:
$ sudo pip install virtualenv
If you are using Microsoft Windows, make sure you open a command prompt window using the “Run as Administrator” option, and then run this command:
$ pip install virtualenv
The virtualenv
command takes the name of the virtual environment as its argument. Make sure your current directory is set to flasky, and then run the following command to create a virtual environment called venv:
$ virtualenv venv New python executable in venv/bin/python2.7 Also creating executable in venv/bin/python Installing setuptools, pip, wheel...done.
A subdirectory with the venv name will be created in the current directory, and all files associated with the virtual environment will be inside it.
Working with a Virtual Environment
When you want to start using a virtual environment, you have to “activate” it. If you are using a Linux or macOS computer, you can activate the virtual environment with this command:
$ source venv/bin/activate
If you are using Microsoft Windows, the activation command is:
$ venv\Scripts\activate
When a virtual environment is activated, the location of its Python interpreter is added to the PATH
environment variable in your current command session, which determines where to look for executable files. To remind you that you have activated a virtual environment, the activation command modifies your command prompt to include the name of the environment:
(
venv)
$
After a virtual environment is activated, typing python
at the command prompt will invoke the interpreter from the virtual environment instead of the system-wide interpreter. If you are using more than one command prompt window, you have to activate the virtual environment in each of them.
Note
While activating a virtual environment is usually the most convenient option, you can also use a virtual environment without activating it. For example, you can start a Python console for the venv virtual environment by running venv/bin/python
on Linux or macOS, or venv\Scripts\python
on Microsoft Windows.
When you are done working with the virtual environment, type deactivate
at the command prompt to restore the PATH
environment variable for your terminal session and the command prompt to their original states.
Installing Python Packages with pip
Python packages are installed with the pip package manager, which is included in all virtual environments. Like the python
command, typing pip
in a command prompt session will invoke the version of this tool that belongs to the activated virtual environment.
To install Flask into the virtual environment, make sure the venv virtual environment is activated, and then run the following command:
(venv) $ pip install flask
When you execute this command, pip will not only install Flask, but also all of its dependencies. You can check what packages are installed in the virtual environment at any time using the pip freeze
command:
(venv) $ pip freeze click==6.7 Flask==0.12.2 itsdangerous==0.24 Jinja2==2.9.6 MarkupSafe==1.0 Werkzeug==0.12.2
The output of pip freeze
includes detailed version numbers for each installed package. The version numbers that you get are likely going to be different from the ones shown here.
You can also verify that Flask was correctly installed by starting the Python interpreter and trying to import it:
(venv) $ python >>> import flask >>>
If no errors appear, you can congratulate yourself: you are ready for the next chapter, where you will write your first web application.
Get Flask Web Development, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.