Installing Flask
The next step is to install Flask, but before I go into that I want to tell you about the best practices associated with installing Python packages.
In Python, packages such as Flask are available in a public repository, from where anybody can download them and install them. The official Python package repository is called PyPI, which stands for Python Package Index (some people also refer to this repository as the "cheese shop"). Installing a package from PyPI is very simple, because Python comes with a tool called pip
that does this work.
To install a package on your machine, you use pip
as follows:
$ pip install <package-name>
Interestingly, this method of installing packages will not work in most cases. If your Python interpreter was installed globally for all the users of your computer, chances are your regular user account is not going to have permission to make modifications to it, so the only way to make the command above work is to run it from an administrator account. But even without that complication, consider what happens when you install a package as above. The pip
tool is going to download the package from PyPI, and then add it to your Python installation. From that point on, every Python script that you have on your system will have access to this package. Imagine a situation where you have completed a web application using version 1.1 of Flask, which was the most current version of Flask when you started, but now has been superseeded by version 2.0. You now want to start a second application, for which you'd like to use the 2.0 version, but if you replace the 1.1 version that you have installed you risk breaking your older application. Do you see the problem? It would be ideal if it was possible to have Flask 1.1 installed and accessible to your old application, while also install Flask 2.0 for your new one.
To address the issue of maintaining different versions of packages for different applications, Python uses the concept of virtual environments. A virtual environment is a complete copy of the Python interpreter. When you install packages in a virtual environment, the system-wide Python interpreter is not affected, only the copy is. So the solution to have complete freedom to install any versions of your packages for each application is to use a different virtual environment for each application. Virtual environments have the added benefit that they are owned by the user who creates them, so they do not require an administrator account.
Let's start by creating a directory where the project will live. I'm going to call this directory microblog, since that is the name of the application:
$ mkdir microblog
$ cd microblog
Support for virtual environments is included in all recent versions of Python, so all you need to do to create one is this:
$ python3 -m venv venv
With this command, I'm asking Python to run the venv
package, which creates a virtual environment named venv
. The first venv
in the command is the name of the Python virtual environment package, and the second is the virtual environment name that I'm going to use for this particular environment. If you find this confusing, you can replace the second venv
with a different name that you want to assign to your virtual environment. In general I create my virtual environments with the name venv
in the project directory, so whenever I cd
into a project I find its corresponding virtual environment.
Note that in some operating systems you may need to use python
instead of python3
in the command above. Some installations use python
for Python 2.x releases and python3
for the 3.x releases, while others map python
to the 3.x releases.
After the command completes, you are going to have a directory named venv where the virtual environment files are stored.
Now you have to tell the system that you want to use this virtual environment, and you do that by activating it. To activate your brand new virtual environment you use the following command:
$ source venv/bin/activate
(venv) $ _
If you are using a Microsoft Windows command prompt window, the activation command is slightly different:
$ venv\Scripts\activate
(venv) $ _
When you activate a virtual environment, the configuration of your terminal session is modified so that the Python interpreter stored inside it is the one that is invoked when you type python
. Also, the terminal prompt is modified to include the name of the activated virtual environment. The changes made to your terminal session are all temporary and private to that session, so they will not persist when you close the terminal window. If you work with multiple terminal windows open at the same time, it is perfectly fine to have different virtual environments activated on each one.
Now that you have a virtual environment created and activated, you can finally install Flask in it:
(venv) $ pip install flask
If you want to confirm that your virtual environment now has Flask installed, you can start the Python interpreter and import Flask into it:
>>> import flask
>>> _
If this statement does not give you any errors you can congratulate yourself, as Flask is installed and ready to be used.
Note that the above installation commands does not specify which version of Flask you want to install. The default when no version is specified is to install the latest version available in the package repository. This tutorial can be followed with Flask versions 1 and 2. The above command will install the latest 2.x version. If for any reason you prefer to follow this tutorial on a 1.x release of Flask, you can use the following command to install the latest 1.x version:
(venv) % pip install "flask<2"