2025, Dec 04 21:00

Airflow Docker crashes with ModuleNotFoundError: flask_session.sessions — how to fix by pinning Flask-Session < 0.6

Airflow 2.7.3 in Docker failing with ModuleNotFoundError: flask_session.sessions? Pin Flask-Session below 0.6. Includes minimal Dockerfile, root cause, and fix

Building Apache Airflow into a single Docker image is convenient until a quiet dependency bump pulls the rug from under you. A typical symptom is the container starting and exiting right away with an import error around Flask-Session, even though you installed it explicitly.

Symptom

On container startup, Airflow crashes during initialization with:

ModuleNotFoundError: No module named 'flask_session.sessions'

The package appears installed, but the process still fails while Airflow imports its web layer.

Minimal Dockerfile that reproduces the failure

The following Dockerfile installs Airflow 2.7.3 and pulls the latest Flask-Session. Despite including Flask-Session, the image exits with the error above.

FROM python:3.10.13-slim-bullseye

ENV DEBIAN_FRONTEND=noninteractive
ENV TERM=linux

ARG AF_VER=2.7.3
ARG AF_EXTRAS=""
ARG EXTRA_PY_PKGS=""
ENV AIRFLOW_HOME=/usr/local/airflow

ENV LANGUAGE=en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LC_ALL=en_US.UTF-8
ENV LC_CTYPE=en_US.UTF-8
ENV LC_MESSAGES=en_US.UTF-8
ENV AIRFLOW__CORE__EXECUTOR=LocalExecutor

RUN set -ex \
    && compile_apt=' \
        freetds-dev \
        libkrb5-dev \
        libsasl2-dev \
        libssl-dev \
        libffi-dev \
        libpq-dev \
    ' \
    && apt-get update -yqq \
    && apt-get upgrade -yqq \
    && apt-get install -yqq --no-install-recommends \
        $compile_apt \
        freetds-bin \
        build-essential \
        default-libmysqlclient-dev \
        unixodbc-dev \
        apt-utils \
        curl \
        rsync \
        netcat \
        locales \
        sudo \
        git \
        krb5-user \
        openssh-client \
    && apt-get install -y gnupg2 apt-transport-https curl \
    && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
    && curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list \
    && apt-get update \
    && ACCEPT_EULA=Y apt-get install -y msodbcsql17 \
    && ACCEPT_EULA=Y apt-get install -y mssql-tools \
    && echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc \
    && sed -i 's/^# en_US.UTF-8 UTF-8$/en_US.UTF-8 UTF-8/g' /etc/locale.gen \
    && locale-gen \
    && update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 \
    && useradd -ms /bin/bash -d /usr/local/airflow airflow \
    && pip install -U pip setuptools wheel \
    && pip install 'SQLAlchemy==1.3.23' \
    && pip install 'Flask-SQLAlchemy==2.4.4' \
    && pip install pytz \
    && pip install pyOpenSSL \
    && pip install ndg-httpsclient \
    && pip install pyasn1 \
    && pip install openpyxl \
    && pip install pyodbc \
    && pip install pysmb \
    && pip install twilio \
    && pip install oauth2client \
    && pip install googleads \
    && pip install twitter_ads \
    && pip install google-api-python-client \
    && pip install virtualenv \
    && pip install 'werkzeug<1.0.0' \
    && pip install 'email-validator' \
    && pip install "apache-airflow[crypto,celery,hive,jdbc,ssh]==2.7.3" \
    && pip install 'redis==3.2' \
    && pip install 'Flask-Session' \
    && pip install facebook_business \
    && if [ -n "${EXTRA_PY_PKGS}" ]; then pip install ${EXTRA_PY_PKGS}; fi \
    && apt-get purge --auto-remove -yqq $compile_apt \
    && apt-get autoremove -yqq --purge \
    && apt-get clean \
    && rm -rf \
        /var/lib/apt/lists/* \
        /tmp/* \
        /var/tmp/* \
        /usr/share/man \
        /usr/share/doc \
        /usr/share/doc-base

What’s actually breaking

Airflow tries to import SqlAlchemySessionInterface from flask_session.sessions while bringing up its web layer. The package is present, but a newer Flask-Session release is not compatible with the Airflow version in use. As a result, Airflow’s import path does not resolve and the process exits with the ModuleNotFoundError. Limiting Flask-Session to an older line eliminates the mismatch.

The fix

Pin Flask-Session to a version lower than 0.6. With that constraint, the container starts and Airflow initializes as expected.

FROM python:3.10.13-slim-bullseye

ENV DEBIAN_FRONTEND=noninteractive
ENV TERM=linux

ARG AF_VER=2.7.3
ARG AF_EXTRAS=""
ARG EXTRA_PY_PKGS=""
ENV AIRFLOW_HOME=/usr/local/airflow

ENV LANGUAGE=en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LC_ALL=en_US.UTF-8
ENV LC_CTYPE=en_US.UTF-8
ENV LC_MESSAGES=en_US.UTF-8
ENV AIRFLOW__CORE__EXECUTOR=LocalExecutor

RUN set -ex \
    && compile_apt=' \
        freetds-dev \
        libkrb5-dev \
        libsasl2-dev \
        libssl-dev \
        libffi-dev \
        libpq-dev \
    ' \
    && apt-get update -yqq \
    && apt-get upgrade -yqq \
    && apt-get install -yqq --no-install-recommends \
        $compile_apt \
        freetds-bin \
        build-essential \
        default-libmysqlclient-dev \
        unixodbc-dev \
        apt-utils \
        curl \
        rsync \
        netcat \
        locales \
        sudo \
        git \
        krb5-user \
        openssh-client \
    && apt-get install -y gnupg2 apt-transport-https curl \
    && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
    && curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list \
    && apt-get update \
    && ACCEPT_EULA=Y apt-get install -y msodbcsql17 \
    && ACCEPT_EULA=Y apt-get install -y mssql-tools \
    && echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc \
    && sed -i 's/^# en_US.UTF-8 UTF-8$/en_US.UTF-8 UTF-8/g' /etc/locale.gen \
    && locale-gen \
    && update-locale LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 \
    && useradd -ms /bin/bash -d /usr/local/airflow airflow \
    && pip install -U pip setuptools wheel \
    && pip install 'SQLAlchemy==1.3.23' \
    && pip install 'Flask-SQLAlchemy==2.4.4' \
    && pip install pytz \
    && pip install pyOpenSSL \
    && pip install ndg-httpsclient \
    && pip install pyasn1 \
    && pip install openpyxl \
    && pip install pyodbc \
    && pip install pysmb \
    && pip install twilio \
    && pip install oauth2client \
    && pip install googleads \
    && pip install twitter_ads \
    && pip install google-api-python-client \
    && pip install virtualenv \
    && pip install 'werkzeug<1.0.0' \
    && pip install 'email-validator' \
    && pip install "apache-airflow[crypto,celery,hive,jdbc,ssh]==2.7.3" \
    && pip install 'redis==3.2' \
    && pip install 'Flask-Session<0.6' \
    && pip install facebook_business \
    && if [ -n "${EXTRA_PY_PKGS}" ]; then pip install ${EXTRA_PY_PKGS}; fi \
    && apt-get purge --auto-remove -yqq $compile_apt \
    && apt-get autoremove -yqq --purge \
    && apt-get clean \
    && rm -rf \
        /var/lib/apt/lists/* \
        /tmp/* \
        /var/tmp/* \
        /usr/share/man \
        /usr/share/doc \
        /usr/share/doc-base

If you encounter the same error elsewhere, first ensure the flask_session package is actually installed. If it is already present and you are on this Airflow version line, limiting Flask-Session below 0.6 resolves the import path issue.

Why this matters

Unpinned dependencies can turn a previously stable image into a failing one from one build to the next. Reproducibility is critical for CI/CD pipelines and for predictable container behavior on servers. Small version constraints often make the difference between a smooth rollout and a debugging session in production.

Conclusion

If your Airflow container exits with “No module named 'flask_session.sessions'” while you already include Flask-Session, constrain Flask-Session to a version lower than 0.6. This aligns it with the Airflow version in use and restores a clean startup. Going forward, prefer pinning versions of key Python dependencies to keep images deterministic.