Installing Python on your machine
The first step to setting up a Python environment on your local machine is to install the interpreter for the language.
Most Unix-based Operating Systems (like every Linux distribution and MacOS) nowadays have a version of Python already installed. This is usually referred to as system installation. The system installation of Python is very rarely the most up-to-date available version of the language (as of July 2020, this is Python 3.8.5, with 3.9 still in Beta).
Before diving deep into the correct installation steps for your OS, you might be wondering: why are we trying to install a new version of Python then, while we could simply upgrade the existing one?
If you have a Unix-based OS, the Python version that already lives on your machine is one that many core parts of the OS rely upon to function properly. Changing it usually results in some rather important parts of your system refusing to do their job. To give a practical example of this, updating (or downgrading) the Python 3.5 system installation on a Linux Mint distribution makes its terminal GUI freeze in place -and God knows what else; this is just one of the most evident side effects of the change! For this reason, it is important to manage a user installation of Python alongside the system one.
Note that, for Unix-based OSs, some installation steps require
echo
ing some commands into your shell language configuration file. On most Linux distributions, the default shell language is bash. A very popular alternative to that iszsh
. You will find instructions for both of these, where needed.
Python 3.8 on Ubuntu (18.04 or above)
Ubuntu is only one of many Linux distributions, but is by far the most popular one.
The following assumes the presence of the apt
package manager on your machine (should you happen to work on a
distribution with a different package manager, you should find that there’s a suitable alternative with it to install what you need).
Run the following:
apt-get update
apt-get install -y python3.8 python3.8-venv python3-venv
NOTE: if you are on Ubuntu 16.04 or lower, run the below commands before retrying the above:
apt-get update apt-get install software-properties-common add-apt-repository ppa:deadsnakes/ppa
Python 3.8 on MacOS
The easiest way to install Python 3.8 on MacOS is via homebrew, the de facto standard package manager on MacOS. Install Homebrew with the following command in your Terminal app:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Once the above completes, run the following to get Python 3.8 on your machine:
brew install python3
##### IF YOU ARE USING BASH AS YOUR SHELL LANGUAGE
echo "export PATH=\"/usr/local/opt/python@3.8/bin:\$PATH\"" >> ~/.bash_profile
##### IF YOU ARE USING ZSH AS YOUR SHELL LANGUAGE
echo "export PATH=\"/usr/local/opt/python@3.8/bin:\$PATH\"" >> ~/.zshrc
exec "$SHELL" # restart your shell for changes to take effect
TIP: check pyenv out to manage and seamelessly switch between different Python versions on your machine!
Python 3.8 on Windows
The Windows Operating System does not come with the Python programming language by default.
Assuming you have an up-to-date version of Windows 10, the easiest way to install Python is to visit the Microsoft Store (from the Start Menu), search for “Python 3.8”, and install it from there.
Alternatively, you can type python
or python3
into the Command Prompt (Windows button, then type cmd
) or Powershell, and Windows will automatically redirect you to the Microsoft Store if Python is not yet installed. Make sure that the Python version is 3.8 or above.
Finally
By the time you read this, you should have a working installation of the latest Python 3.8 on your machine. To verify the precise Python version, run:
python3.8 -V
# should print like...
# Python 3.8.5
and should also be able to run the Python 3.8 interpreter by issuing python3.8
in your terminal.
To quit the Python interpreter, hit
Ctrl-D
or type eitherquit()
orexit()
.
Your machine might also make things a little bit easier for you depending on whether a Python 3 installation existed before
you installed Python 3.8 or not. On MacOS, for example, Homebrew aliases python3.8
with python3
, while the python
command is left pointing
to Python 2.7 (required by System Libraries).
Curiosity: why are we still talking about Python 2.7 after 12 years since the launch of Python 3? Python 3 was the second major version of the language released since its origins (1991 -yes, Python is that old), meaning programs written in Python 3 cannot be understood by a Python 2 interpreter. The reason for Python 2.7 to be with us in 2020 is one you’ll hear frequently when talking about “old” software still hanging around: legacy. Python 2.7 is used by several system libraries across a variety of Operating Systems, hence it can’t be lightheartedly forgotten. Things might however change in the coming few years, given that the Python Software Foundation has officially stopped maintaining this version of the language since January 1st, 2020.