ImportError: DLL load failed while importing _ctypes

Hi all,

Current I work for embedded python to C.
My environment is windows.

When I call python function from C it show

ImportError: DLL load failed while importing _ctypes

But this operation success when I used python interprator directly.

I have checked python and C config as same and they are listed below and _ctype is put C:/msys64/mingw32/lib/python3.11/lib-dynload already.

{'data': 'C:/msys64/mingw32',
 'include': 'C:/msys64/mingw32/include/python3.11',
 'platinclude': 'C:/msys64/mingw32/include/python3.11',
 'platlib': 'C:/msys64/mingw32/lib/python3.11/site-packages',
 'platstdlib': 'C:/msys64/mingw32/lib/python3.11',
 'purelib': 'C:/msys64/mingw32/lib/python3.11/site-packages',
 'scripts': 'C:/msys64/mingw32/bin',
 'stdlib': 'C:/msys64/mingw32/lib/python3.11'}

['C:/msys64/home/hidden1/hidden2',
 'C:/msys64/mingw32/lib/python311.zip',
 'C:/msys64/mingw32/lib/python3.11',
 'C:/msys64/mingw32/lib/python3.11/lib-dynload',
 'C:/msys64/mingw32/lib/python3.11/site-packages',
 'C:/msys64/home/TJWu/ToStanley/auo_py/__init__.py/..']

----- update -----
When I use import sysconfig print(sysconfig.get_config_vars()) in __init__.py (under ctypes folder)

compare two configs only one difference

# diff python c
1159c1159
<  'projectbase': 'C:/msys64/mingw32/bin',
---
>  'projectbase': 'C:/msys64/home/hidden1/hidden2',
1168d1167

We don’t support the MinGW32 build here, so you may need to ask on their forums.

The only likely cause of this is a missing or incompatible libffi-7.dll.[1] It should be in the same directory as _ctypes.pyd, but because MinGW uses their own build process anything could’ve happened.


  1. I don’t think we updated 3.11 to libffi-8.dll. ↩︎

Thanks for your answer!

— update —
Here is related issue: [python] missing automatic add_dll_directory for c:\msys64\mingw64\bin in PyConfig · Issue #18984 · msys2/MINGW-packages (github.com)