Making sure we have Python
Before we start digging into its features, we should make sure that we have Python installed and working!
You might see some documentation or posts online that mention Python version 2. There was a long transition from Python 2 to Python 3, and had this book been written a few years earlier, we would be discussing the merits of each. However, Python 3 is fully capable of everything the majority of people need to do, and Python 2 stopped being supported by the core Python team in 2020. This book uses the latest Python 3.9 stable release for all its code examples, but they are likely to work on Python 3.7 or later, as that's the minimum version that Quart requires in order to work.
If your computer does not have at least Python 3.7, you can download a new version from Python's own website, where installation instructions are provided: https://www.python.org/downloads/.
You will find it easier if all the code examples in this book are run in a virtual environment, or virtualenv (https://docs.python.org/3/library/venv.html). A virtual environment is Python's way of keeping each project separate, as it means you can install Quart and any other libraries you need; it will only affect the application you are currently working on. Other applications and projects can have different libraries, or different versions of the same library, without them getting in the way of each other. Using a virtualenv also means that you can easily recreate your project's dependencies somewhere else, which will be very useful when we deploy a microservice in a later chapter.
Some code editors, such as PyCharm or Visual Studio, may manage a virtual environment for you. Every code example in the book runs in a terminal, and so we will use a terminal to create our virtualenv. This also shows how things work in more detail than viewing a program's output on the web, or in log files, and will be helpful when fixing any problems in the future.
In a terminal, such as a macOS Terminal application, or a Windows Subsystem for Linux, change to the directory you would like to work in and run the following command:
python -m venv my-venv
Depending on how you installed Python, you may need to use python3
to create the virtual environment.
This creates a new virtual environment called my-venv
in the current directory. You could give it another path if you like, but it's important to remember where it is. To use the virtual environment, you must activate it:
source my-venv/bin/activate
For most of the command-line examples in this book, we assume you are running on Linux, as that is what most services online use, so it is good to be familiar with it. This means that most of the commands will also work on macOS or on Windows using the Windows Subsystem for Linux. It's also possible to run Docker containers on all these systems, and we will describe containers later on when we discuss deploying your microservice.
Now, let's install Quart so that we can run our example code:
pip install quart
To stop using the virtual environment without closing the terminal, you can type deactivate
. For now, though, let's keep the virtualenv active and look at how Quart will work.