Preparations

Installing and configuring software can be one of the most challenging tasks a developer performs regularly. Ask a developer what things give him the most trouble, and he will often mention installing and configuring software high on his list. The problem is that computers have different hardware, software (and software versions), and configurations. This adds up to hundreds or thousands of factors that can lead to trouble.

Don't be surprised if you run into trouble in this chapter. If something is going to go wrong, this is where it will do so. Think of it as a learning opportunity -- you can learn a lot by troubleshooting problems.

We recommend using a Macintosh or AWS Cloud9 for our books and courses. While students have enjoyed success with Windows and Linux, they can be trickier to set up. We'll discuss installing Python on all 4 types of systems a little later in this chapter.

Python sometimes gets installed as python3 and sometimes as python. This varies by operating system and version numbers, so predicting which command you should use is difficult. Throughout this book and the Core Curriculum, we assume you can use the python command that invokes Python 3. If you can't, or the python command runs the wrong version, try python3 instead. In many cases, either will work.

The Command Line

Let's start with a brief discussion of the shell command line.

We'll assume you know how to find the command line on your computer and type in commands. When we show example commands or commands that you should type, we will sometimes precede the command with a $ symbol:

$ ls -al

The $ represents the command line prompt, which may differ on your system. That's okay. You aren't meant to type the $ or any other prompt, so ignore the prompt.

Using the $ in code blocks interferes with the ability to copy and paste code, so we will try to avoid using it. However, we sometimes need to mix commands and output in the code block. When we do, the $ is needed to distinguish between code and output. For instance, here's some code mixed with output that requires the $:

$ which python
/usr/bin/python
$ python --version
Python 3.11.2

We usually disable the Copy Code button when displaying code mixed with output and prompts. This causes fewer problems for students that are too quick to copy and paste.

Pay attention to what shell you are using. You can determine the shell with the following command:

echo $SHELL

If you see output that ends with /bash, then you're using the Bash shell. In Bash, the default command line prompt is $, which we'll be using as the default prompt.

If, instead of /bash, the echo $SHELL command outputs something that ends with /zsh, then you are using the Zsh shell. In Zsh, the default command line prompt is %. We'll show the % prompt when showing Zsh-specific commands, but we'll usually use $ for commands where the shell doesn't matter.

There's a third possibility. If you see a shell name that doesn't end with /bash or /zsh, then you're using some other shell. Unfortunately, you're on your own if you're using something besides Bash or Zsh. We are unable to support any other shells.

We won't use many shell commands in this book. Most of the commands in this book only show the python command. However, you should know how to use the following commands so you can create and manage files and directories:

  • cd: navigate between directories
  • mkdir: create (make) a new directory
  • rmdir: delete an empty directory
  • touch: create an empty file
  • rm: delete a file
  • git: so you can push your code to GitHub

If you need a refresher, please review our Introduction to the Command Line and Introduction to Git and GitHub books.

Installing Python

You need Python installed on your computer or cloud environment to get the most from this book. We recommend using Python version 3.10.x or higher if you can. Older versions of Python may suffice, but you could experience issues. For instance, you can't use the match/case statement with Python versions older than 3.10.0. More recent versions should give you less trouble.

You may already have Python installed. Go to your command line or terminal application and enter the following commands:

python3 --version
python --version

If Python is installed, one or both of these commands should print a version number.

If you already have Python installed by your system, it may have restrictive permissions that can impede your ability to install packages, upgrade Python, and perform other development tasks. On a Mac, in particular, you do not want to use the system version of Python; use Homebrew for maximum flexibility.

When Things Go Wrong

Things don't always go the way you expect. Fortunately, programmers have a time-honored tradition: starting over. No matter how careful you are, things will go wrong one day. A software installation may fail, or a previously working installation suddenly stops working. When (not if) that happens, starting over may be your best bet.

We'll touch briefly on starting over for each system discussed below.

Installing Python on a Mac

We recommend using a Macintosh as your best bet for a trouble-free and painless experience. Sadly, that's not a guarantee, but most students have little trouble if they follow the directions in this section and elsewhere.

Homebrew is the easiest way to install Python and many Linux tools on a Mac. It lets you install Linux-based programs directly on your Mac without building them from scratch.

If you are using the Apple-supplied Terminal on a Mac that is running one of the Apple processors (e.g., M1, M2, M3, etc.), we recommend making sure the Terminal.app is not set to use Rosetta. To disable Rosetta in the Terminal application, go to Applications, Utilities in the Finder, then right click on the Terminal.app and select the Get Info option. In the General section of the pop up window that appears, make sure "Open using Rosetta" is not checked.

If you require Rosetta in the Apple Terminal for some reason, you can use an alternative terminal program like iTerm2 instead for your Python programming.

At some point, Apple is likely to remove Rosetta from macOS. If that happens, you won't see the "Open using Rosetta" option in the Get Info window.

Follow the directions at the top of the Homebrew home page to install Homebrew.

If running the installation command displays a Next steps: part near the end that tells you to run one or more commands, don't run those commands. They may interfere with our setup instructions.

Once you've installed Homebrew, restart your Terminal, then run the following command to ensure that Homebrew is correctly installed:

brew --version

Next, determine what versions of Python you can install with Homebrew:

$ brew search python | grep python@
python@3.10
python@3.11
python@3.12
python@3.7
python@3.8
python@3.9

The output identifies the different Python versions you can install. Use this list to identify the most recent version. It has the highest number after the first (or only) period (.). In this case, version 3.12 is the most recent. You can install this version of Python like so:

brew install python@3.12

Follow the direction below based on your current shell:

  • Bash: Update your PATH environment variable as shown here:

    WHERE="$HOMEBREW_PREFIX/bin"
    echo "export PATH='$WHERE:$PATH'" >> ~/.bash_profile
    echo "export PATH='$WHERE:$PATH'" >> ~/.bashrc
    echo "alias python=python3" >> ~/.bash_profile
    
  • Zsh: Update your PATH environment variable as shown here:

    WHERE="$HOMEBREW_PREFIX/bin"
    echo "export PATH='$WHERE:$PATH'" >> ~/.zshrc
    echo "alias python=python3" >> ~/.zshrc
    

Once you've run the above commands, restart your Terminal application, then ensure you're using the expected version of Python:

python --version

You should also be able to use the python3 command.

Check Your PATH with the echo $PATH command. You should see something like this:

/opt/homebrew/bin:/usr/bin:/usr/sbin:/sbin:/bin

On older Macs (those that use an Intel processor), you may see /usr/local/bin instead of /opt/homebrew/bin at the beginning of the PATH:

/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin

If you don't see /opt/homebrew/bin (or /usr/local/bin on an older Mac), at the beginning of that output, you're likely not set up properly (don't worry about the text after the first :). Check the following before proceeding:

  • If you're using Bash, you may have a pre-existing ~/.bash_profile or ~/.profile file that is interfering with your ~/.bashrc startup.
  • If you're using Zsh, you may have a ~/.zshenv, ~/.zprofile, or ~/.zlogin file that is interfering with the startup process of your ~/.zshrc file.

If you have one or more of the above files, you may need to edit or remove those files. You should probably seek assistance if you do not know what is happening in those files. You might be better off using AWS Cloud9 instead (see below).

Starting Over on a Mac

Starting over on a Mac with Homebrew usually means uninstalling and reinstalling Python. You may also have to reinstall any Python packages you had installed previously.

Sometimes, you may have to take that one step further and uninstall Homebrew entirely, then reinstall it. You can find the uninstall directions on the Homebrew site. As of this writing, those directions are here.

In the worst case, starting over may mean restoring from a reliable backup, such as Time Machine. This lets you return your system to a time when the system was working. This is a drastic solution, but you'll be thankful you had those backups. You do have backups, don't you?

Installing Python on GitHub Codespaces

If you don't have a Macintosh or don't want to use your Mac as a development environment, you can try using GitHub Codespaces instead. GitHub Codespaces is a low-cost, cloud-based alternative to using your own system for development. They also have free environments which can be sufficient for Launch School, provided you take care not to exceed the free limits:

  • Use the smallest (2-core with 8GB RAM and 32GB disk space) codespaces
  • Don't leave codespaces running.
  • Don't create multiple codespaces.
  • Don't add additional disk space.

Even if you exceed one or more of these limitations, your monthly charge is typically less than a few US dollars.

To use GitHub Codespaces, you only need a modern web browser and an internet connection. Codespaces is also relatively trouble-free. However, GitHub may change its UI and software more often than Apple changes macOS, which can lead to outdated instructions. We do our best to update our instructions when needed.

By default, codespaces have an older version of Python installed, with no obvious way to upgrade to a more recent version. For instance, as of this writing, GitHub Codespaces have Python 3.10 pre-installed. For now, we're not going to worry about this. Use whatever version is already installed. You can determine that version by running the following command:

python --version

We discuss how you can install more recent versions of Python in the Core Curriculum.

Starting Over on GitHub Codespaces

Starting over On GitHub Codespaces usually means creating a new "Blank" template. You can then copy over any files you want to save from the old workspace to the new one. Finally, remove the old workspace to avoid GitHub charges. You may need to reinstall any programs you installed in the old workspace.

Installing Python on AWS Cloud9

If you don't have a Macintosh or don't want to use your Mac as a development environment, you can try the AWS Cloud9 service. Cloud9 is a low-cost, cloud-based alternative to using your own system for development. You can get up to a year of free service, provided you take care not to exceed the free tier limits:

  • Don't leave workspaces running.
  • Don't create multiple workspaces.
  • Don't add additional disk space.

Even if you exceed one or more of these limitations, your monthly charge is typically less than a cup of plain black coffee from a vending machine.

To use Cloud9, you only need a modern web browser and an internet connection. Cloud9 is also relatively trouble-free. However, AWS changes its UI and software more often than Apple changes macOS, which can lead to outdated instructions. We do our best to update our instructions when needed.

By default, Cloud9 workspaces have an older version of Python installed, with no obvious way to upgrade to a more recent version. For instance, as of this writing, Cloud9 workspaces with Amazon Linux 2023 have Python 3.9 pre-installed. For now, we're not going to worry about this. Use whatever version is already installed. You can determine that version by running the following command:

python --version

We discuss how you can install more recent versions of Python in the Core Curriculum.

Starting Over on Cloud9

Starting over On AWS Cloud9 usually means creating a new workspace with your desired platform choice. You can then copy over any files you want to save from the old workspace to the new one. Finally, remove the old workspace to avoid AWS charges. You may need to reinstall any programs you installed in the old workspace.

Installing Python on Other Linux Systems

Most Linux systems come with Python pre-installed. However, it is often an older version. For now, don't worry about this. You should use the version that is already installed in your workspace. You can determine that version by running the following command:

python --version

Refer to the official Python installation instructions if you need to install Python on your system. However, we are unable to provide any support for this process.

Starting Over on Linux

Starting over on Linux systems often begins with uninstalling and reinstalling Python. If that works, you're golden and can get back to work.

If that doesn't work, you may need to restore from a reliable backup. This lets you return your system to a time when the system was working. This is a drastic solution, but you'll be thankful you had those backups. You do have backups, don't you?

Installing Python on Windows

We do not recommend using Windows and cannot provide support for it. However, many Launch School students have successfully used the Windows Subsystem for Linux Version 2 in our courses. WSL2 essentially lets Windows users work with Linux on a Windows system. See the previous section for further information.

Starting Over on Windows

Starting over on Windows often begins with uninstalling and reinstalling Python. If that works, you're golden and can get back to work.

If you're using WSL2, you may be able to delete your development environment and then recreate it. You'll need to reinstall any programs you need, but that's better than restoring your entire Windows system.

If that doesn't work, you may need to restore from a reliable backup. This lets you return your system to a time when the system was working. This is a drastic solution, but you'll be thankful you had those backups. You do have backups, don't you?

Using a Code Editor

Developers often use a code editor to write code. A code editor creates plain text documents with no styling or formatting. If you already have a favorite code editor (e.g., Visual Studio Code, Vim, Emacs, etc.), use it to work through this book.

If you do not have a preferred code editor, we strongly recommend using Visual Studio Code (also known as VSCode). It's free and built as an open-source project. It's highly polished and pleasant to look at and use. Most surprisingly, perhaps, is that it comes from Microsoft. Its popularity is skyrocketing in the developer community.

VSCode comes in Mac, Windows, and Linux versions.

Do not use a word processor when writing code. Word processors are for writing, not coding. They often inject invisible characters or whitespace into the document to help format and style it. While that can make your prose look great, it doesn't work well with code. Code written with a word processor may not run even if you copy and paste it into a code editor before saving it.

Python Documentation

As you start your journey as a developer, you must learn how to read documentation. Like all popular programming languages, Python comes with a rich set of standard libraries you can use. However, you must study the Python documentation to familiarize yourself with the language and its different classes, modules, functions, and methods.

Some developers also refer to the documentation for a class, module, function, or method as its API. API is a somewhat overloaded acronym for Application Programming Interface that can refer to the documentation or how applications talk to each other. If someone asks whether you looked at the list API, they want to know if you read the Python documentation for the list class. However, if they want to know how to use Google's Search API, they want you to tell them how those search services can be used programmatically.

In this section, we'll look briefly at the Python documentation. Since the documentation sometimes changes structure and appearance, this quick look doesn't use images or many details of the documentation's organization.

The Python docs website, as of August 2023, has a button near the top labeled "Python Docs." This button takes you to the most recent version of the documentation, which may not be the same as your Python version. If the version number matters, view the Documentation Releases by Version.

The main page for each documentation version should show a high-level table of contents. However, finding the information you need may not be painless even with that table of contents. For the purposes of this book, you can focus on these parts:

  • The Language Reference explores the language itself. Oddly, the most useful sections for beginners are near the bottom of the page (as of December 2023):
    • Expressions
    • Simple Statements
    • Compound Statements
  • The Library Reference covers all built-in classes and modules. The most valuable sections (as of December 2023) are:
    • Built-in Types
    • Built-in Functions
    • Built-in Constants
  • Turn to the Glossary if you need unfamiliar terms explained.
  • You can use the Complete Table of Contents if you know the right words. Your browser's in-window search will help you quickly find the appropriate entry.

We'll refer you to the documentation to highlight special items of interest when the information will benefit you most. For now, bookmark the documentation and try to familiarize yourself with its structure.

It's worth noting that the documentation is not always helpful, even when you find the right page. You can search the web for a better explanation if necessary. Be careful, though: searches often turn up outdated or incorrect content, so focus on recent items and try to look at several pages for conflicting information. AI tools like ChatGPT may also be helpful, but be extra careful: AI tools may lie to you. Convincingly.