Setting up your development environment
This recipe, dedicated to setting up your development environment, is a critical foundation for any successful project in web development. Here, you’ll learn how to install and configure all the essential tools needed to start building with FastAPI.
We begin by guiding you through the installation of Python, the core language behind FastAPI. Next, we’ll move on to installing FastAPI itself, along with Uvicorn, a lightning-fast ASGI server, which serves as the bedrock for running your FastAPI applications.
Setting up an IDE is our next stop. Whether you prefer VS Code, PyCharm, or any other Python-friendly IDE, we’ll provide tips to make your development process smoother and more efficient.
Lastly, we’ll introduce you to Git and GitHub – indispensable tools for version control and collaboration in modern software development. Understanding how to use these tools will not only help you manage your code effectively but also open doors to the vast world of community-driven development and resources.
Getting ready
FastAPI works with Python, so you need to check your Python version before using it. This is an important step for setting up FastAPI. We will guide you through how to install it.
Windows installation
If you work on Windows, follow these steps to install Python:
- Visit the official Python website: python.org.
- Download the latest version of Python or any other version higher than 3.9.
- Run the installer. Ensure to check the box that says Add Python to PATH before clicking Install Now.
- After the installation, open Command Prompt and type
python --version
to confirm the installation.
macOS/Linux installation
macOS usually comes with Python pre-installed; however, it might not be the latest version.
You can use Homebrew
(a package manager for macOS). To install it, open the terminal and run it:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/\Homebrew/install/HEAD/install.sh)"
Then, you can install Python – still from the terminal – with the following command:
$ brew install python
On Linux, you can install Python using the package manager by running the following command:
$ sudo apt-get install python3
That’s all you need to install Python on macOS and Linux systems.
Checking the installation
You can then check that Python is correctly installed by running the following command in the terminal:
$ python --version
If you installed it on Linux, the binary command is python3
, so you can check that Python is correctly installed by running the following command:
$ python3 --version
Once Python is installed, we want to make sure that the Python’s package manager is correctly installed. It comes with Python’s installation, and it’s called pip
.
From a terminal window, run the following command:
$ pip --version
On Linux, run the following command:
$ pip3 --version
Once Python is installed on your computer, you can now consider installing FastAPI.
How to do it...
When you have Python and pip
ready, we can continue with installing FastAPI, the IDE. Then, we will configure Git.
We will do it by following these steps:
- Installing FastAPI and Uvicorn
- Setting up your IDE (VS Code or PyCharm)
- Setting up Git and GitHub to track your project
Installing FastAPI and Uvicorn
With Python set up, the next step is installing FastAPI and Uvicorn. FastAPI is the framework we’ll use to build our applications, and Uvicorn is an ASGI server that runs and serves our FastAPI applications.
Open your command-line interface and install FastAPI and Uvicorn together by running the following command:
$ pip install fastapi[all]
This command installs FastAPI along with its recommended dependencies, including Uvicorn.
To verify the installation, you can simply run uvicorn --version
from the terminal.
Setting up your IDE
Choosing the right IDE is a crucial step in your FastAPI journey. An IDE is more than just a text editor; it’s a space where you write, debug, and test your code.
A good IDE can significantly enhance your coding experience and productivity. For FastAPI development and Python in general, two popular choices are VS Code and PyCharm.
VS Code
VS Code is a free, open source, lightweight IDE with powerful features. It offers excellent Python support and is highly customizable.
You can download and install VS Code from the official website (code.visualstudio.com
). The installation is quite straightforward. Once installed, open VS Code, go to Extensions (a square icon on the left bar), and search for python
. Install the Microsoft version, and that is it.
PyCharm
PyCharm, created by JetBrains, is specifically tailored for Python development. It offers a broad range of tools for professional developers, including excellent support for web development frameworks such as FastAPI.
You can choose between a Community free edition and a Professional paid version. For the scope of the book, the Community Edition is largely sufficient, and it can be downloaded on the JetBrains website: https://www.jetbrains.com/pycharm/download/.
For PyCharm as well, the installation is straightforward.
Enhancing your development experience
For both IDEs – and if you use another of your choice – make sure to leverage basic perks to improve your experience as a developer and be more efficient. Here is a short checklist that I use when I approach a new IDE environment:
- Code completion and analysis: Good IDEs provide intelligent code completion, error highlighting, and fixes, which are invaluable for efficient development
- Debugging tools: Utilize debugging features provided by the IDE to diagnose and resolve issues in your code
- Version control integration: A good IDE offers support for Git, simplifying code change tracking and repository management
- Customization: Customize your IDE by adjusting themes, key binding, and settings to match your workflow, making your development experience as comfortable and productive as possible
Setting up Git and GitHub
Version control is an essential aspect of software development. Git, coupled with GitHub, forms a powerful toolset for tracking changes, collaborating, and maintaining the history of your projects. You can download the Git installer from the official website git-scm.com and install it.
After installation, configure Git with your username and email using the following commands in the command line:
$ git config --global user.name "Your Name" $ git config --global user.email "your.email@example.com"
GitHub is the platform chosen to store code examples used in the book. Sign up for a GitHub account at github.com if you don’t already have one.