Chapter 1: Python Development Environment Setup
Before we can get started on our FastAPI journey, we need to configure a clean and efficient Python environment. This chapter will show you the best practices and conventions that Python developers use daily to run their projects.
By the end of this chapter, you'll be able to run Python projects and install third-party dependencies in a contained environment that won't raise conflicts if you happen to work on another project that uses different versions of the Python language or different dependencies.
In this chapter, we're going to cover the following main topics:
- Installing a Python distribution using
pyenv
- Creating a Python virtual environment
- Installing Python packages with
pip
- Installing the HTTPie command-line utility
Technical requirements
Throughout this book, we'll assume you have access to a Unix-based environment, such as a Linux distribution or macOS.
If they haven't done so already, macOS users should install the Homebrew package (https://brew.sh), which helps a lot in installing command-line tools.
If you are a Windows user, you should enable Windows Subsystem for Linux (https://docs.microsoft.com/windows/wsl/install-win10), WSL, and install a Linux distribution (such as Ubuntu) that will run alongside the Windows environment, which should give you access to all the required tools. There are currently two versions of WSL, WSL and WSL2. Depending on your Windows version, you might not be able to install the newest version. However, we do recommend using WSL2 if your Windows installation supports it.
Installing a Python distribution using pyenv
Python is already bundled with most Unix environments. To ensure this is the case, you can run this command in a command line to show the version of the currently installed Python:
The output version displayed will vary depending on your system. You may think that this is enough to get started, but it poses an important issue: you can't choose the Python version for your project. Each Python version introduces new features and breaking changes. Thus, it's important to be able to switch to a recent version for new projects to take advantage of the new features but still be able to run older projects that may not be compatible. This is why we need pyenv
.
pyenv
(https://github.com/pyenv/pyenv) is a tool that helps you manage and switch between multiple Python versions on your system. It allows you to set a default Python version for your whole system but also per project.
Beforehand, you need to install several build dependencies on your system to allow pyenv
to compile Python on your system. The official documentation provides clear guidance on this (https://github.com/pyenv/pyenv/wiki#suggested-build-environment), but here are the commands you should run:
- Install the build dependencies:
- macOS users, use this:
- Ubuntu users, use this:
Package managers
Brew and APT are what are commonly known as package managers. Their role is to automate the installation and management of software on your system. Thus, you don't have to worry about where to download them and how to install and uninstall them. The commands just tell the package manager to update its internal package index and then install the list of required packages.
- Install
pyenv
:Tip
If you are a macOS user, you can also install it with Homebrew:
brew install pyenv
. - This will download and execute an installation script that will handle everything for you. At the end, it'll prompt you with some instructions to add some lines to your shell scripts so that
pyenv
is discovered properly by your shell:
a. Open your ~/.profile
script in nano, a simple command-line text editor:
b. Add the following lines before the block containing ~/.bashrc
:
c. Save by using the keyboard shortcut Ctrl + O and confirm by pressing Enter. Then, quit by using the keyboard shortcut Ctrl + X.
d. Open your ~/.bashrc
script in nano. If you are using zsh instead of Bash (the default on the latest macOS), the file is named ~/.zshrc
:
e. Add the following line at the end:
f. Save by using the keyboard shortcut Ctrl + O and confirm by pressing Enter. Then, quit by using the keyboard shortcut Ctrl + X.
- Reload your shell configuration to apply those changes:
- If everything went well, you should now be able to invoke the
pyenv
tool: - We can now install the Python distribution of our choice. Even though FastAPI is compatible with Python 3.6 and later, we'll use Python 3.7 throughout this book, which has more mature handling of the asynchronous paradigm. All the examples in the book were tested with this version but should work flawlessly with newer versions. Let's install Python 3.7:
This may take a few minutes since your system will have to compile Python from the source.
- Finally, you can set the default Python version with the following command:
This will tell your system to always use Python 3.7.10
by default, unless specified otherwise in a specific project.
- To make sure everything is in order, run the following command to check the Python version that is invoked by default:
Congratulations! You can now handle any version of Python on your system and switch it whenever you like!