Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building Data Science Applications with FastAPI

You're reading from  Building Data Science Applications with FastAPI

Product type Book
Published in Oct 2021
Publisher Packt
ISBN-13 9781801079211
Pages 426 pages
Edition 1st Edition
Languages
Author (1):
François Voron François Voron
Profile icon François Voron

Table of Contents (19) Chapters

Preface Section 1: Introduction to Python and FastAPI
Chapter 1: Python Development Environment Setup Chapter 2: Python Programming Specificities Chapter 3: Developing a RESTful API with FastAPI Chapter 4: Managing Pydantic Data Models in FastAPI Chapter 5: Dependency Injections in FastAPI Section 2: Build and Deploy a Complete Web Backend with FastAPI
Chapter 6: Databases and Asynchronous ORMs Chapter 7: Managing Authentication and Security in FastAPI Chapter 8: Defining WebSockets for Two-Way Interactive Communication in FastAPI Chapter 9: Testing an API Asynchronously with pytest and HTTPX Chapter 10: Deploying a FastAPI Project Section 3: Build a Data Science API with Python and FastAPI
Chapter 11: Introduction to NumPy and pandas Chapter 12: Training Machine Learning Models with scikit-learn Chapter 13: Creating an Efficient Prediction API Endpoint with FastAPI Chapter 14: Implement a Real-Time Face Detection System Using WebSockets with FastAPI and OpenCV Other Books You May Enjoy

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:

$ python3 --version

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:

  1. Install the build dependencies:
    • macOS users, use this:
      $ brew install openssl readline sqlite3 xz zlib
    • Ubuntu users, use this:
      $ sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

      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.

  2. Install pyenv:
    $ curl https://pyenv.run | bash

    Tip

    If you are a macOS user, you can also install it with Homebrew: brew install pyenv.

  3. 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:

$ nano ~/.profile

b. Add the following lines before the block containing ~/.bashrc:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"

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:

$ nano ~/.bashrc

e. Add the following line at the end:

eval "$(pyenv init -)"

f. Save by using the keyboard shortcut Ctrl + O and confirm by pressing Enter. Then, quit by using the keyboard shortcut Ctrl + X.

  1. Reload your shell configuration to apply those changes:
    $ source ~/.profile && exec $SHELL
  2. If everything went well, you should now be able to invoke the pyenv tool:
    $ pyenv
    pyenv 1.2.21
    Usage: pyenv <command> [<args>]
  3. 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:
    $ pyenv install 3.7.10

This may take a few minutes since your system will have to compile Python from the source.

  1. Finally, you can set the default Python version with the following command:
    $ pyenv global 3.7.10

This will tell your system to always use Python 3.7.10 by default, unless specified otherwise in a specific project.

  1. To make sure everything is in order, run the following command to check the Python version that is invoked by default:
    $ python --version
    Python 3.7.10

Congratulations! You can now handle any version of Python on your system and switch it whenever you like!

You have been reading a chapter from
Building Data Science Applications with FastAPI
Published in: Oct 2021 Publisher: Packt ISBN-13: 9781801079211
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 $15.99/month. Cancel anytime}