PyInstaller, boto3 and configparser

Python
Work around a PyInstaller issue with boto3 by adjusting the build so a standalone executable still bundles what it needs.
Published

3 Apr 2024 12:00

The current version of PyInstaller (6.5.0) doesn’t play nicely with the boto3 package. Here’s how to fix it.

The Problem

My requirements.txt looks something like this:

boto3==1.20.54
botocore==1.23.54
pyinstaller==6.5.0

I wrap my script using PyInstaller.

pyinstaller -c -y --onefile crawler.py

When I build and run the executable locally it works 100% fine. However, when I build on GitHub Actions (using ubuntu-latest) I get a flurry of error messages (most mentioning either boto3 or botocore) that terminate with

ModuleNotFoundError: No module named 'configparser'

The Solution

The reason for the error is that PyInstaller is failing to detect the dependency on configparser. There are two ways to address this, either on the command line:

pyinstaller -c -y --hidden-import=configparser --onefile crawler.py

or via the .spec file:

a = Analysis(
    ['crawler.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=['configparser'],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
)
pyz = PYZ(a.pure)