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