Short answer: To update Python, install the newer version with the tools of your operating system. On Windows, run choco upgrade python -y in PowerShell or use the installer from python.org; on macOS, install pyenv with brew install pyenv and run pyenv install <version>; on Linux, add the deadsnakes PPA and run sudo apt install python3.x (the version you need). Check the result with python -V.
Here is a way to update Python on Windows, Mac or Linux (commands vary depending on the operating system):
# On windows Powershell
$ choco upgrade python -y
# MacOs homebrew with pyenv
# you would need to install pyenv first
$ pyenv install 3.9.2
# linux
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt update
$ sudo apt install python3.9During the rest of the article you can check details on the rest of the installation steps depending on your operating system.
If you only need the commands, our guide on how to update Python in terminal walks through macOS, Linux and Windows step by step.
Updating Python on Windows OS:
To update Python on a Windows Operating System to the latest, we can choose one of 2 ways, it's up to you which way you want to follow:
- Downloading the Python installer of the version we want.
- Making use of Windows Chocolatey, a powerful package manager.
Update by downloading the Python version
- The installer we'll find it on python.org
- Select
Latest Python 3 Release - Python 3.11.1(if there's a new version when you read this article, it won't be Python 3.11.1) - Scroll until you find a table with all the version available and select the one that suits you the best (in our case
Windows installer (64-bit)) - Once downloaded run the installer and follow instructions. If there's not a installed Python version already, it'll install this version. If there's a Python version already installed, it'll upgrade it.
- You can verify your installation/upgrade by opening
CMDorPowerShellterminal and typingpython -V
$ python -V
# Output: Python 3.11.1🖐 For a more in-depth guide regarding this procedure, please head to our step-by-step guide on how to update python on windows where we cover with pictures of every step.
Update Python version using Chocolatey Package Manager
Chocolatey Package Manager has become a favorite among Windows users when handling Python versions or adding other software to our System using PowerShell.
Installing Chocolatey
-
Open the PowerShell as administrator. On your start menu type
powershelland selectRun as Administrator -
Install Chocolatey using the following command:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))- Verify Chocolatey installation. On the
Powershellterminal, typechoco.
$ choco
# Expected output:
Chocolatey v1.2.0
Please run 'choco -?' or 'choco <command> -?' for help menu.Updating Python with Chocolatey
Having Chocolatey installed in our System, upgrading Python is now as simple as typing: choco upgrade python -y
$ choco upgrade python -y
# expected output:
Chocolatey v1.2.0
Upgrading the following packages:
python
By upgrading, you accept licenses for the packages.
python is not installed. Installing
Progress: Downloading python3 3.11.0... 90%Let's verify that the install and upgrade was successful with the command we used earlier
$ python -V
# Output: Python 3.11.0Updating Python on MacOS
Even if almost the entire Python community has moved to Python 3.X, older MacOS versions came with Python 2.7 and current ones don't include the latest Python 3. To use the latest Python version you'll need to install it manually.
Checking python version on MacOS:
Let's first check if your System is indeed using the version 2.7 or a later one.
- Open the terminal. You can find it on MacOs spotlight
(command + space)and typingterminal - type
python
If you receive a message like this:
WARNING: Python 2.7 is not recommended. This version is included in macOS for compatibility with legacy software. Future versions of macOS will not include Python 2.7. Instead, it is recommended that you transition to using 'python3' from within Terminal.It means that you need to update your python version ASAP, so let´s get into it.
There are 2 options to update Python on MacOs:
-
Download and install the version from python
-
Use Homebrew to install pyenv package manager and then install the Python version(s) you want
Update by downloading
- Head to python.org
- Select the latest version (
Latest Python 3 Release - Python 3.11.1latest version when writing this article ) or scroll until you find the version you want. - Once download finishes, run the installer
- Verify the installation opening the terminal and typing the following command
python -V
Getting ready to upgrade with Homebrew:
To handle our Python development environments and Python version we use Homebrew, a powerful package manager for all Mac Operating Systems.
- Install homebrew with the following command:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Most probably, you'll be prompted for sudo (Super User-level access) and to grant it, you´ll need to write your password. Keep in mind, what you write, will not be displayed, not even with *.
- Confirm installation with
Return(Enter) or cancel with any other key.
Now that we installed Homebrew on our System, we´ll install pyenv to manage our Python versions more efficiently.
Installing with pyenv (recommended)
What pyenv will allow us to do, is to switch between different Python versions.
Open a terminal as we did earlier $ brew install pyenv
It´s done! We now have pyenv installed and now we'll make a new Python installation or upgrade it.
Installing/Updating Python on pyenv
Thanks to pyenv we now can install Python with a simple one liner:
$ pyenv install 3.9.9It´s not mandatory to use version 3.9.9, you can instead pass any version you wish to install.
Installation errors
If you receive an error regarding C compiler cannot create executables most probably is XCode related and if you upgrade your MacOS to Big Sur, then probabilities goes higher.
Fixing this problem ain´t a big problem, but will take as much time as your internet speed will allow it.
- Download the latest version of Apple's Xcode here
- Re-run the previous commands
pyenv install 3.9.9and it should work just fine
Set up your MacOS path for pyenv (Bash)
To use our pyenv we need to update our .bash_profile with the following commands:
With /bin directory
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profileWithout /bin directory
If your System doesn't have a /bin directory in the pyenv_root folder most probably, you only have /shims directory. If it´s the case, you'll need this commands instead
$ echo 'export PATH="$PYENV_ROOT/shims:$PATH"' >> ~/.bash_profile$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profileReloading the profile to get the changes
We need to reload .bash_profile (or open a new terminal) in order for all these changes to be accessible.
To reload it in the current terminal, just type:
$ source ~/.bash_profileUpdate Python on Linux
We´ll be using deadsnakes ppa (Personal Package Archives) that'll allows us to have multiple python versions installed on our System at the same time.
- First we need to include deadsnakes to our repositories trusted list
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update- After installation finishes, we'll be able to install any Python version we want with the following command
$ sudo apt install python3.11- To run python, use the following
$ python3.11If you want to grow as a developer you can check this 4Geeks Lesson titled Learn to Code with Python. Hope you enjoy the reading and keep on the Geek side!
Keeping Python up to date is a small habit that pays off in every project. To go further, a coding bootcamp lets you practice Python in real projects with mentors reviewing your code.
Frequently asked questions
Does updating Python remove the packages I already installed?
Updating to a new minor version does not delete your packages, but the new interpreter will not see them. Each minor version, such as 3.9 or 3.11, has its own site-packages folder. Export the list from the old version with python -m pip freeze > requirements.txt and reinstall it with the new one using python -m pip install -r requirements.txt.
Should I uninstall the old Python version before installing a new one?
No, it is not required. Different Python versions can live side by side: on Windows the py launcher selects one with py -3.11, and on macOS and Linux each version has its own command, such as python3.11. Remove old versions only after migrating your projects, and never remove the Python that ships with Linux, because system tools depend on it.
Why does python -V still show the old version after updating?
Your terminal is still finding the old executable first in the PATH. Close and reopen the terminal, then check which file runs with where python on Windows or which -a python3 on macOS and Linux. With pyenv, set the active version with pyenv global 3.9.9, and on Windows confirm that the installer option adding Python to PATH was enabled.
How do I update pip after upgrading Python?
Run python -m pip install --upgrade pip with the interpreter you just installed, for example py -3.11 -m pip install --upgrade pip on Windows or python3.11 -m pip install --upgrade pip on Linux. Calling pip through python -m guarantees the upgrade applies to that exact version instead of another Python found earlier in the PATH.
