Skip to content

    Chapter 2, Environment

    Installing Python on macOS, Windows, and Linux

    The correct way to install varies by operating system, and the wrong way often costs hours later. I show the path I use for each one.

    Code for this chapter: ambiente/cap02_instalacao.py

    Which version to install

    Install the latest stable series, which when I wrote was 3.14, and never download anything below 3.12 for a new project. Python 3.10 goes out of support in October 2026.

    A rule that saves me headaches: the Python that came with the system is for the system. It serves macOS or your Linux distribution, which depend on a specific version. For your projects, install a separate Python.

    Install by operating system

    The most straightforward way is Homebrew. If you don't have it yet, install it from the official site (brew.sh) and then:

    Terminal
    brew install python
    python3 --version
    

    You can also download the installer from the official site (python.org, macOS section), which works on Intel and Apple Silicon Macs. I prefer Homebrew because updating becomes a command.

    On macOS, the command is python3. The command python alone only exists within a virtual environment or when you use pyenv or uv.

    Error externally-managed-environment

    On recent Linux distributions and in Homebrew, running pip install outside of a virtual environment fails with the message externally-managed-environment. This is intentional (the rule is called PEP 668) and protects the system Python. The correct solution is to create a virtual environment, which is the subject of chapter 4. Do not use --break-system-packages.

    Check the installation

    Before anything else, confirm the version in the terminal. Use python3 --version on macOS and Linux, and py --version on Windows. Then, run this script, which shows where your Python comes from:

    ambiente/cap02_instalacao.pylinhas 10 a 16
    import platform
    import sys
    
    print("Versão:", platform.python_version())
    print("Implementação:", platform.python_implementation())
    print("Executável:", sys.executable)
    print("Sistema:", platform.system())
    
    Output
    Versão: 3.14.6
    Implementação: CPython
    Executável: /opt/homebrew/bin/python3
    Sistema: Darwin
    

    The block above is an example from a Mac with Homebrew. Your output will vary, and that's exactly what you want to observe: the path of the executable tells you which Python you are actually using.

    The commands python, python3, and py

    SituationCommand
    macOS and Linux, outside of a venvpython3
    Windows, outside of a venvpy
    Inside a virtual environment (any system)python
    With pyenv or uv configuredpython

    The interactive mode (REPL)

    Type python3 (or py) without arguments to open interactive mode, great for testing a quick expression:

    Console Python
    >>> 2 + 2
    4
    >>> "python".upper()
    'PYTHON'
    >>> exit()