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 Python version currently installed:
$ 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
.
The pyenv tool (https://github.com/pyenv/pyenv) 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:
- For macOS users, use the following:
$ brew install openssl readline sqlite3 xz zlib tcl-tk
- For Ubuntu users, use the following:
$ sudo apt update; sudo apt install make build-essential libssl-dev zlib1g-dev \libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \libncursesw5-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 from and how to install and uninstall them. Those 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 for macOS users
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:- If your shell is
bash
(the default for most Linux distributions and older versions of macOS), run the following commands:echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrcecho 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrcecho 'eval "$(pyenv init -)"' >> ~/.bashrc
- If your shell is
zsh
(the default in the latest version of macOS), run the following commands:echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrcecho 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrcecho 'eval "$(pyenv init -)"' >> ~/.zshrc
What is a shell and how do I know the one I’m using?
The shell is the underlying program running when you start a command line. It’s responsible for interpreting and running your commands. Several variants of those programs have been developed over time, such as bash
and zsh
. Even though they have their differences, in particular the names of their configuration files, they are mostly inter-compatible. To find out which shell you’re using, you can run the echo $
SHELL
command.
- Reload your shell configuration to apply those changes:
$ exec "$SHELL"
- If everything went well, you should now be able to invoke the
pyenv
tool:$ pyenv>>> pyenv 2.3.6>>> Usage: pyenv <command> [<args>]
- We can now install the Python distribution of our choice. Even though FastAPI is compatible with Python 3.7 and later, we’ll use Python 3.10 throughout this book, which has a more mature handling of the asynchronous paradigm and type hinting. All the examples in the book were tested with this version but should work flawlessly with newer versions. Let’s install Python 3.10:
$ pyenv install 3.10
This may take a few minutes since your system will have to compile Python from the source.
What about Python 3.11?
You might wonder why we use Python 3.10 here while Python 3.11 is already released and is available. At the time of writing, not every library we’ll use throughout this book officially supports this newest version. That’s why we prefer to stick with a more mature version. Don’t worry, though: what you’ll learn here will still be relevant to future versions of Python.
- Finally, you can set the default Python version with the following command:
$ pyenv global 3.10
This will tell your system to always use Python 3.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 --versionPython 3.10.8
Congratulations! You can now handle any version of Python on your system and switch it whenever you like!
Why does it show 3.10.8 instead of just 3.10?
The 3.10 version corresponds to a major version of Python. The Python core team regularly publishes major versions with new features, depreciations, and sometimes breaking changes. However, when a new major version is published, previous versions are not forgotten: they continue to receive bug and security fixes. It’s the purpose of the third part of the version.
It’s very possible by the time you’re reading this book that you’ve installed a more recent version of Python 3.10, such as 3.10.9. It just means that fixes have been published. You can find more information about how the Python life cycle works and how long the Python core team plans to support previous versions in this official document: https://devguide.python.org/versions/.