Skip to main content

Appendix: Using Python

This book uses Python. Python is free, available on almost every operating system, and there are thousands of libraries to do almost anything you want. Python is great.

Fun Fact: Python’s name stems from comedy, not reptiles.

Installing Python and the required libraries

We recommend uv for installing Python and the libraries used in this book. It is a fast, free tool that installs the right version of Python for you, and keeps the libraries for each project in a separate environment.

import antigravity
  1. Install uv, following the instructions for your operating system.

  2. Create a new folder for your work with the book, and download two files into it:

    • requirements.txt, which lists the libraries used in this book, pinned to the exact versions the book was built and tested with;
    • fpppy_data.zip, which contains all the data used in this book.

    Unzip fpppy_data.zip in that folder. This creates a data subfolder, which is where the code in this book looks for the data.

  3. Open your command line or terminal in that folder, and create an environment with Python 3.10 and the required libraries:

    uv venv --python 3.10
    uv pip install -r requirements.txt
  4. Activate the environment, so that python and jupyter use it. On macOS and Linux:

    source .venv/bin/activate

    On Windows:

    .venv\Scripts\activate

    You will need to activate the environment each time you open a new terminal. Then jupyter lab will start JupyterLab for running the code in this book.

If you prefer conda or plain pip, requirements.txt also works with pip install -r requirements.txt in a Python 3.10 environment.

The pinned libraries install on Windows, Linux, and Macs with Apple silicon, but not on Intel Macs, because some of them (torch and jaxlib) are not available for Intel Macs at these versions.

That’s it! You should now be ready.

Python examples in this book

We provide Python code for most examples in shaded boxes like this:

# Import required libraries
import pandas as pd
from statsforecast import StatsForecast
from statsforecast.models import AutoETS
from utilsforecast.plotting import plot_series

# Plot one time series
aus_retail = pd.read_csv('data/aus_retail.csv', parse_dates=['Month'])
df = aus_retail.query('`Series ID` == "A3349640L"')[['Series ID', 'Month', 'Turnover']]
plot_series(df, id_col='Series ID', time_col='Month', target_col='Turnover')

# Produce some forecasts
sf = StatsForecast(
    models = [AutoETS(season_length=12)],
    freq = 'MS'
)

sf.forecast(df=df, h=24, id_col='Series ID', time_col='Month', target_col='Turnover')

These examples assume that you have installed the necessary Python libraries, as described above. To add another library to your environment, use uv pip install, for example

uv pip install pandas

Sometimes we assume that the Python code that appears earlier in the same chapter of the book has also been run; so it is best to work through the Python code in the order provided within each chapter.

Getting started with Python

There are many great resources on how to get started with Python. Here are some that we like:

Courses:

Read the docs: