Flask is a small framework by most standards, small enough to be called a “micro-framework.” It is 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 two main dependencies. The routing, debugging, and Web Server Gateway Interface (WSGI) subsystems come from Werkzeug, while template support is provided by Jinja2. Werkzeug and Jinja2 are authored by the core developer of Flask.
There is no native support in Flask 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 you need is a computer with Python installed.
The code examples in this book have been verified to work with Python 2.7 and Python 3.3, so using one of these two versions is strongly recommended.
The most convenient way to install Flask is to use a virtual environment. A virtual environment is a private copy of the Python interpreter onto 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 application ensures that applications have access to only 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, virtual environments don’t require administrator rights.
If you get an error, you will have to install the utility.
Python 3.3 adds native support of virtual environments through the venv module and the pyvenv command.
pyvenv can be used instead of virtualenv, but note that virtual environments created with pyvenv on Python 3.3 do not include pip, which needs to be installed manually. This limitation has been removed in Python 3.4, where
pyvenv can be used as a complete virtualenv replacement.
Most Linux distributions provide a package for virtualenv. For example, Ubuntu users can install it with this command:
$sudo apt-get install python-virtualenv
If you are using Mac OS X, then you can install virtualenv using easy_install:
$sudo easy_install virtualenv
If you are using Microsoft Windows or any operating system that does not provide an official virtualenv package, then you have a slightly more complicated install procedure.
Using your web browser, navigate to https://bitbucket.org/pypa/setuptools, the home of the
setuptools installer. In that page, look for a link to download the installer script. This is a script called ez_setup.py. Save this file to a temporary folder on your computer, then run the following commands in that folder:
The previous commands must be issued from an account with administrator rights. On Microsoft Windows, start the command prompt window using the “Run as Administrator” option. On Unix-based systems, the two installation commands must be preceded with
sudo or executed as the root user. Once installed, the virtualenv utility can be invoked from regular accounts.
Now you need to create the folder that will host the example code, which is available from 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 folder to version “1a,” the initial version of the application:
$git clone https://github.com/miguelgrinberg/flasky.git
$git checkout 1a
The next step is to create the Python virtual environment inside the flasky folder using the virtualenv command. This command has a single required argument: the name of the virtual environment. A folder with the chosen name will be created in the current directory and all files associated with the virtual environment will be inside. A commonly used naming convention for virtual environments is to call them venv:
$virtualenv venv New python executable in venv/bin/python2.7 Also creating executable in venv/bin/python Installing setuptools............done. Installing pip...............done.
Now you have a venv folder inside the flasky folder with a brand-new virtual environment that contains a private Python interpreter. To start using the virtual environment, you have to “activate” it. If you are using a bash command line (Linux and Mac OS X users), you can activate the virtual environment with this command:
If you are using Microsoft Windows, the activation command is:
When a virtual environment is activated, the location of its Python interpreter is added to the
PATH, but this change is not permanent; it affects only your current command session. To remind you that you have activated a virtual environment, the activation command modifies the command prompt to include the name of the environment:
Most Python packages are installed with the pip utility, which virtualenv automatically adds to all virtual environments upon creation. When a virtual environment is activated, the location of the pip utility is added to the
If you created the virtual environment with pyvenv under Python 3.3, then pip must be installed manually. Installation instructions are available on the pip website. Under Python 3.4, pyvenv installs pip automatically.
To install Flask into the virtual environment, use the following command:
$pip install flask
With this command, Flask and its dependencies are installed in the virtual environment. You can verify that Flask was installed correctly by starting the Python interpreter and trying to import it:
If no errors appear, you can congratulate yourself: you are ready for the next chapter, where you will write your first web application.