Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

import ibm_db fails on Windows because the Db2 driver DLLs are not found

Your error

ImportError: DLL load failed while importing ibm_db: The specified module could not be found.1 more line it can show

Fix it fast

Most common cause, 1 of 3

An ibm_db release from 3.2.0 to 3.2.8 that never registers the driver folder

Upgrade to a release that carries the fix.

Fix
pip install --upgrade ibm_db
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ImportError: DLL load failed while importing ibm_db means Python could not find the Db2 clidriver DLLs. Since Python 3.8, Windows ignores PATH for extension DLLs. ibm_db 3.2.0 to 3.2.8 never registered the folder; 3.2.9 and 3.3.0 add an ibm_db_dll.pth hook. Upgrade, check the .pth file exists, and fix or unset IBM_DB_HOME.

  • Applies to ibm_db 3.3.0, Python 3.11 to 3.14 on Windows
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print three facts in one go: the installed ibm_db version, the IBM_DB_HOME value, and whether the startup hook file sits in site-packages. An old version points to cause 1, False to cause 2, a set IBM_DB_HOME to cause 3.

Run this first
python -c "import os, sysconfig, importlib.metadata as m; print(m.version('ibm_db'), os.environ.get('IBM_DB_HOME'), os.path.isfile(os.path.join(sysconfig.get_path('purelib'), 'ibm_db_dll.pth')))"

Then find the line you see in this list and open its fix:

  1. 1An ibm_db release from 3.2.0 to 3.2.8 that never registers the driver folderThe version printed is 3.2.x below 3.2.9, and the same script worked with 3.1.4 or on Linux
  2. 2The ibm_db_dll.pth hook is missing or Python never runs itibm_db is 3.2.9 or newer but the hook check prints False, or Python is started with the -S option
  3. 3IBM_DB_HOME sends the hook to the wrong driver folderThe first command prints a path for IBM_DB_HOME, often one ending in \bin or one left over from an old Db2 client

What does the error look like?

  • ImportError: DLL load failed while importing ibm_db: The specified module could not be found.
  • OSError: [WinError 87] The parameter is incorrect: 'path to clidriver installation until bin'

Cause 1 of 3

An ibm_db release from 3.2.0 to 3.2.8 that never registers the driver folder

You seeThe version printed is 3.2.x below 3.2.9, and the same script worked with 3.1.4 or on Linux

Check

ImportError: DLL load failed while importing ibm_db appears on these releases because the bundled clidriver\bin folder is never added to the DLL search path. Python 3.8 stopped using PATH to resolve DLLs that an extension module depends on, so having the folder on PATH changes nothing.

Check
pip show ibm_db

Fix

Upgrade to a release that carries the fix. Version 3.2.9 added a startup hook that calls os.add_dll_directory() for the clidriver bin folder, and 3.3.0 keeps it.

Fix
pip install --upgrade ibm_db

Verify

A new Python process imports both modules without the ImportError.

Verify
python -c "import ibm_db, ibm_db_dbi; print('ibm_db loaded')"

Confirmed by github.com

Did this fix it?

Cause 2 of 3

The ibm_db_dll.pth hook is missing or Python never runs it

You seeibm_db is 3.2.9 or newer but the hook check prints False, or Python is started with the -S option

Check

On current releases, the DLL error means the hook did not register clidriver\bin. Import lines in a .pth file are run by Python's site module at start-up, and the -S option switches that module off. A partial install, or a launcher that passes -S, therefore reaches the import with no DLL folder added.

Check
python -c "import os, site, sysconfig; paths=[os.path.join(site.getusersitepackages(),'clidriver','bin'), os.path.join(sysconfig.get_path('purelib'),'clidriver','bin')]; print(next((p for p in paths if os.path.isdir(p)), 'clidriver not found - reinstall ibm_db'))"

Fix

Reinstall the package so pip writes the hook again. Where the hook cannot run, register the folder yourself before the import, using the full path that ends in clidriver\bin. Windows accepts only an absolute folder here, so pasting the README's placeholder text literally ends in WinError 87.

Fix
pip uninstall -y ibm_db
pip install ibm_db

# fallback inside your script, before the import
import os
os.add_dll_directory(r"C:\venvs\app\Lib\site-packages\clidriver\bin")
import ibm_db

Verify

The hook check prints True after the reinstall, or the script with the explicit path imports ibm_db cleanly.

Verify
python -c "import os, sysconfig; print(os.path.isfile(os.path.join(sysconfig.get_path('purelib'), 'ibm_db_dll.pth')))"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

IBM_DB_HOME sends the hook to the wrong driver folder

You seeThe first command prints a path for IBM_DB_HOME, often one ending in \bin or one left over from an old Db2 client

Check

When IBM_DB_HOME is set, the hook registers %IBM_DB_HOME%\bin and ignores the clidriver that came inside the wheel. A value that already ends in bin, a deleted folder, or a different client install leaves the import with no matching DLLs. The README also warns that the prebuilt wheel is not meant to run against an external clidriver.

Check
echo %IBM_DB_HOME%
dir "%IBM_DB_HOME%\bin"

Fix

If you do not need a separate Db2 client, clear the variable so the bundled driver is used. If you do, point it at the install root (the folder above bin) and build ibm_db from source so it compiles against that client.

Fix
set IBM_DB_HOME=
python -c "import ibm_db"

rem only when an external client is required
set IBM_DB_HOME=C:\IBM\clidriver
pip install ibm_db --no-binary :all: --no-cache-dir

Verify

The import works in a fresh terminal where IBM_DB_HOME is either empty or names a folder whose bin directory exists.

Verify
python -c "import os, ibm_db; print(os.environ.get('IBM_DB_HOME'))"

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Pin ibm_db at 3.2.9 or later in requirements.txt so a fresh Windows machine gets the start-up hook.
  • Keep IBM_DB_HOME out of machine-wide environment settings unless a project really builds against an external Db2 client.
  • If a launcher or service wrapper starts Python with -S, call os.add_dll_directory() with the clidriver\bin path before any import of ibm_db.
  • Before moving a project to Python 3.18, confirm the ibm_db release you pin no longer relies on an import line in a .pth file, since 3.18 ignores those lines.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.