Package management with pip
A FastAPI application constitutes packages, therefore you will be introduced to package management practices, such as installing packages, removing packages, and updating packages for your application.
Installing packages from the source can turn out to be a cumbersome task as, most of the time, it involves downloading and unzipping .tar.gz
files before manual installation. In a scenario where a hundred packages are to be installed, this method becomes inefficient. Then, how do you automate this process?
Pip is a Python package manager like JavaScript’s yarn
; it enables you to automate the process of installing Python packages – both globally and locally.
Installing pip
Pip is automatically installed during a Python installation. You can verify whether pip is installed by running the following command in your terminal:
$ python3 -m pip list
The preceding command should return a list of packages installed. The output should be similar to the following figure:
If the command returns an error, follow the instructions at https://pip.pypa.io/en/stable/installation/ to install pip.
Basic commands
With pip
installed, let’s learn its basic commands. To install the FastAPI
package with pip, we run the following command:
$ pip install fastapi
On a Unix operating system, such as Mac or Linux, in some cases, the sudo
keyword is prepended to install global packages.
To uninstall a package, the following command is used:
$ pip uninstall fastapi
To collate the current packages installed in a project into a file, we use the following freeze
command:
$ pip freeze > requirements.txt
The >
operator tells bash to save the output from the command into the requirements.txt
file. This means that running pip freeze
returns an output of all the currently installed packages.
To install packages from a file such as the requirements.txt
file, the following command is used:
$ pip install -r requirements.txt
The preceding command is mostly used in deployment.
Now that you have learned the basics of pip and have gone over some basic commands, let’s learn the basics of Docker.