The Real Python Podcast

Reducing the Size of Python Docker Containers


Listen Later

How can you easily reduce the size of a Python Docker container? What are the exceptions you should catch in your code? Christopher Trudeau is back on the show this week with another batch of PyCoder’s Weekly articles and projects.

We cover a tutorial by Khuyen Tran at CodeCut about shrinking the size of a Python Docker container. The piece explores SlimToolKit, which analyzes a container at runtime, identifies what files are used, and then builds a minimal image with only those dependencies.

We dig into a recent piece by Trey Hunner about Python exceptions. When trying to determine a strategy to handle potential errors, which exceptions should you catch and which should you leave unhandled?

We also share other articles and projects from the Python community, including recent releases, two PEPs that have been deferred to 3.16, a critical vulnerability in an open-source ASGI framework, resolving a lazy import manually, a project to anonymize sensitive PII data, and a tool for loading Django settings from a TOML file.

This episode is sponsored by AURI by Endor Labs.

Course Spotlight: Raising and Handling Python Exceptions

In this course, you’ll learn what an exception is and how it differs from a syntax error. You’ll learn about raising exceptions, making assertions, and catching exceptions to change the control flow of your program using the try, except, else, and finally keywords.

Topics:

  • 00:00:00 – Introduction
  • 00:02:32 – Django 6.1 Alpha 1 Released
  • 00:03:07 – Nuitka Python Compiler Release 4.1
  • 00:04:00 – PEP 813: The Pretty Print Protocol (Deferred to 3.16)
  • 00:04:28 – PEP 830: Add Timestamps to Exceptions and Tracebacks
  • 00:04:50 – Millions of AI agents imperiled by critical vulnerability in open source package
  • 00:07:27 – What Types of Exceptions Should You Catch?
  • 00:13:28 – Sponsor: AURI from Endor Labs
  • 00:14:18 – PyCon US 2026 Packaging Summit Recap
  • 00:18:39 – Slim Down Python Docker Containers
  • 00:24:17 – Video Course Spotlight
  • 00:25:45 – Resolve a Lazy Import Manually
  • 00:28:04 – presidio: Detect, Redact, & Anonymize Sensitive Data (PII)
  • 00:32:37 – dj-toml-settings: Load Django settings from a TOML file
  • 00:37:14 – Thanks and goodbye
  • News:

    • Django 6.1 Alpha 1 Released
    • Nuitka Python Compiler Release 4.1
    • PEP 813: The Pretty Print Protocol (Deferred to 3.16)
    • PEP 830: Add Timestamps to Exceptions and Tracebacks (Deferred to 3.16)
    • Millions of AI agents imperiled by critical vulnerability in open source package - Ars Technica
    • Missing Host header validation poisons request.url.path, bypassing path-based security checks · Advisory · Kludex/starlette
    • Show Links:

      • What Types of Exceptions Should You Catch? – The trickiest programming bugs are often caused by catching exceptions that you didn’t mean to catch or handling exceptions in ways that obfuscate the actual error that’s occurring. Which exceptions should you catch and which should you leave unhandled?
      • PyCon US 2026 Packaging Summit Recap – Per-talk notes from the PyCon US 2026 Packaging Summit, including: Emma Smith on Wheel 2.0 and Zstandard compression, Mike Fiedler on PyPI abuse vectors, Mahe Iram Khan on ecosystems, lightning talks on PEP 772, mobile wheels, AI accelerator variants, and the roundtable discussions.
      • Slim Down Python Docker Containers – Learn how SlimToolKit can reduce a Python Docker image by analyzing what your app actually uses at runtime. This tutorial walks through slimming a Chainlit LLM chatbot image, shows where container bloat comes from, and explains how to avoid breaking lazily loaded Python frameworks.
      • Resolve a Lazy Import Manually – Learn how to work around the Python 3.15 machinery to resolve an explicit lazy import manually.
      • TIL #141 – Inspect a lazy import - mathspp
      • Projects:

        • presidio: Detect, Redact, & Anonymize Sensitive Data (PII)
        • dj-toml-settings: Load Django settings from a TOML file
        • Additional Links:

          • Using raise for Effective Exceptions - Real Python Video Course
          • Working With Python’s Built-in Exceptions – Real Python Video Course
          • Episode #177: Welcoming PyPI’s Safety & Security Engineer Mike Fiedler
          • Chainlit - Build AI applications
          • AURI for Developers - AI-Native AppSec Platform - Endor Labs
          • Level up your Python skills with our expert-led courses:

            • Raising and Handling Python Exceptions
            • Advanced Python import Techniques
            • Working With Python's Built-in Exceptions
            • Support the podcast & join our community of Pythonistas

              ...more
              View all episodesView all episodes
              Download on the App Store

              The Real Python PodcastBy Real Python

              • 4.7
              • 4.7
              • 4.7
              • 4.7
              • 4.7

              4.7

              139 ratings


              More shows like The Real Python Podcast

              View all
              The Changelog: Software Development, Open Source by Changelog Media

              The Changelog: Software Development, Open Source

              288 Listeners

              Software Engineering Daily by Software Engineering Daily

              Software Engineering Daily

              623 Listeners

              Talk Python To Me by Michael Kennedy

              Talk Python To Me

              583 Listeners

              Soft Skills Engineering by Jamison Dance and Dave Smith

              Soft Skills Engineering

              289 Listeners

              Super Data Science: ML & AI Podcast with Jon Krohn by Jon Krohn

              Super Data Science: ML & AI Podcast with Jon Krohn

              300 Listeners

              Python Bytes by Michael Kennedy and Calvin Hendryx-Parker

              Python Bytes

              213 Listeners

              Syntax - Tasty Web Development Treats by Wes Bos & Scott Tolinski - Full Stack JavaScript Web Developers

              Syntax - Tasty Web Development Treats

              984 Listeners

              Darknet Diaries by Jack Rhysider

              Darknet Diaries

              8,052 Listeners

              Tech Brew Ride Home by Morning Brew

              Tech Brew Ride Home

              963 Listeners

              Practical AI by Practical AI LLC

              Practical AI

              213 Listeners

              AWS Podcast by Amazon Web Services

              AWS Podcast

              203 Listeners

              Django Chat by William Vincent and Carlton Gibson

              Django Chat

              75 Listeners

              Last Week in AI by Skynet Today

              Last Week in AI

              317 Listeners

              Machine Learning Street Talk (MLST) by Machine Learning Street Talk (MLST)

              Machine Learning Street Talk (MLST)

              97 Listeners

              The Pragmatic Engineer by Gergely Orosz

              The Pragmatic Engineer

              75 Listeners