Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
FastAPI Cookbook

You're reading from   FastAPI Cookbook Develop high-performance APIs and web applications with Python

Arrow left icon
Product type Paperback
Published in Aug 2024
Publisher Packt
ISBN-13 9781805127857
Length 358 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Giunio De Luca Giunio De Luca
Author Profile Icon Giunio De Luca
Giunio De Luca
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Chapter 1: First Steps with FastAPI FREE CHAPTER 2. Chapter 2: Working with Data 3. Chapter 3: Building RESTful APIs with FastAPI 4. Chapter 4: Authentication and Authorization 5. Chapter 5: Testing and Debugging FastAPI Applications 6. Chapter 6: Integrating FastAPI with SQL Databases 7. Chapter 7: Integrating FastAPI with NoSQL Databases 8. Chapter 8: Advanced Features and Best Practices 9. Chapter 9: Working with WebSocket 10. Chapter 10: Integrating FastAPI with other Python Libraries 11. Chapter 11: Middleware and Webhooks 12. Chapter 12: Deploying and Managing FastAPI Applications 13. Index 14. Other Books You May Enjoy

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:

  1. Visit the official Python website: python.org.
  2. Download the latest version of Python or any other version higher than 3.9.
  3. Run the installer. Ensure to check the box that says Add Python to PATH before clicking Install Now.
  4. 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:

  1. Installing FastAPI and Uvicorn
  2. Setting up your IDE (VS Code or PyCharm)
  3. 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.

You have been reading a chapter from
FastAPI Cookbook
Published in: Aug 2024
Publisher: Packt
ISBN-13: 9781805127857
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image