A
Installation and Troubleshooting

There are many versions of Python available and numerous ways to set it up on each operating system. If the approach in Chapter 1 didn’t work, or if you want to install a different version of Python than the one currently installed, the instructions in this appendix can help.

Python on Windows

The instructions in Chapter 1 show you how to install Python using the official installer at https://python.org. If you couldn’t get Python to run after using the installer, the troubleshooting instructions in this section should help you get Python up and running.

Using py Instead of python

If you run a recent Python installer and then issue the command python in a terminal, you should see the Python prompt for a terminal session (>>>). When Windows doesn’t recognize the python command, it will either open the Microsoft Store because it thinks Python isn’t installed, or you’ll get a message such as “Python was not found.” If the Microsoft Store opens, close it; it’s better to use the official Python installer from https://python.org than the one that Microsoft maintains.

The simplest solution, without making any changes to your system, is to try the py command. This is a Windows utility that finds the latest version of Python installed on your system and runs that interpreter. If this command works and you want to use it, simply use py anywhere you see the python or python3 command in this book.

Rerunning the Installer

The most common reason python doesn’t work is that people forget to select the Add Python to PATH option when running the installer; this is an easy mistake to make. The PATH variable is a system setting that tells Python where to look for commonly used programs. In this case, Windows doesn’t know how to find the Python interpreter.

The simplest fix in this situation is to run the installer again. If there’s a newer installer available from https://python.org, download the new installer and run it, making sure to check the Add Python to PATH box.

If you already have the latest installer, run it again and select the Modify option. You’ll see a list of optional features; keep the default options selected on this screen. Then click Next and check the Add Python to Environment Variables box. Finally, click Install. The installer will recognize that Python is already installed, and it will add the location of the Python interpreter to the PATH variable. Make sure you close any open terminals, because they’ll still be using the old PATH variable. Open a new terminal window and issue the command python again; you should see a Python prompt (>>>).

Python on macOS

The installation instructions in Chapter 1 use the official Python installer at https://python.org. The official installer has been working well for years now, but there are a few things that can get you off track. This section will help if anything isn’t working in a straightforward manner.

Accidentally Installing Apple’s Version of Python

If you run the python3 command and Python is not yet installed on your system, you’ll most likely see a message that the command line developer tools need to be installed. The best approach at this point is to close the pop-up showing this message, download the Python installer from https://python.org, and run the installer.

If you choose to install the command line developer tools at this point, macOS will install Apple’s version of Python along with the developer tools. The only issue with this is that Apple’s version of Python is usually somewhat behind the latest official version of Python. However, you can still download and run the official installer from https://python.org, and python3 will then point to the newer version. Don’t worry about having the developer tools installed; there are some useful tools in there, including the Git version control system discussed in Appendix D.

Python 2 on Older Versions of macOS

On older versions of macOS, before Monterey (macOS 12), an outdated version of Python 2 was installed by default. On these systems, the command python points to the outdated system interpreter. If you’re using a version of macOS with Python 2 installed, make sure you use the python3 command, and you’ll always be using the version of Python you installed.

Python on Linux

Python is included by default on almost every Linux system. However, if the default version on your system is earlier than Python 3.9, you should install the latest version. You can also install the latest version if you want the most recent features, like Python’s improved error messages. The following instructions should work for most apt-based systems.

Using the Default Python Installation

If you want to use the version of Python that python3 points to, make sure you have these three additional packages installed:

$ sudo apt install python3-dev python3-pip python3-venv

These packages include tools that are useful for developers and tools that let you install third-party packages, like the ones used in the projects section of this book.

Installing the Latest Version of Python

We’ll use a package called deadsnakes, which makes it easy to install multiple versions of Python. Enter the following commands:

$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt update
$ sudo apt install python3.11

These commands will install Python 3.11 onto your system.

Enter the following command to start a terminal session that runs Python 3.11:

$ python3.11
>>>

Anywhere you see the command python in this book, use python3.11 instead. You’ll also want to use this command when you run programs from the terminal.

You’ll need to install two more packages to make the most of your Python installation:

$ sudo apt install python3.11-dev python3.11-venv

These packages include modules you’ll need when installing and running third-party packages, like the ones used in the projects in the second half of the book.

Checking Which Version of Python You’re Using

If you’re having any issues running Python or installing additional packages, it can be helpful to know exactly which version of Python you’re using. You may have multiple versions of Python installed and not be clear about which version is currently being used.

Issue the following command in a terminal:

$ python --version
Python 3.11.0

This tells you exactly which version the command python is currently pointing to. The shorter command python -V will give the same output.

Python Keywords and Built-in Functions

Python comes with its own set of keywords and built-in functions. It’s important to be aware of these when you’re naming things in Python: your names cannot be the same as these keywords and shouldn’t be the same as the function names, or you’ll overwrite the functions.

In this section, we’ll list Python’s keywords and built-in function names, so you’ll know which names to avoid.

Python Keywords

Each of the following keywords has a specific meaning, and you’ll see an error if you try to use any of them as a variable name.

False      await      else       import     pass
None       break      except     in         raise
True       class      finally    is         return
and        continue   for        lambda     try
as         def        from       nonlocal   while
assert     del        global     not        with
async      elif       if         or         yield

Python Built-in Functions

You won’t get an error if you use one of the following readily available built-in functions as a variable name, but you’ll override the behavior of that function:

abs()           hash()            slice()
aiter()         help()            sorted()
all()           hex()             staticmethod()
any()           id()              str()
anext()         input()           sum()
ascii()         int()             super()
bin()           isinstance()      tuple()
bool()          issubclass()      type()
breakpoint()    iter()            vars()
bytearray()     len()             zip()
bytes()         list()            __import__()
callable()      locals()
chr()           map()
classmethod()   max()
compile()       memoryview()
complex()       min()
delattr()       next()
dict()          object()
dir()           oct()
divmod()        open()
enumerate()     ord()
eval()          pow()
exec()          print()
filter()        property()
float()         range()
format()        repr()
frozenset()     reversed()
getattr()       round()
globals()       set()
hasattr()       setattr()