Repository navigation
import ctypes fails with a statically linked interpreter due to dlopen() failure #81241
Description
Activity
ctypes/__init__.pycalls_ctypes.dlopen(None)on Linux as part of code execution during module import. Unfortunately, dlopen() doesn't work if the current executable isn't a dynamic executable.Using a fully statically linked Python executable:
$ ldd python3.7 not a dynamic executable$ python3.7 >>> import _ctypes >>> _ctypes.dlopen(None) Traceback (most recent call last): File "<stdin>", line 1, in <module> OSError: Dynamic loading not supported >>> import ctypes Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/gps/src/python-build-standalone.git/build/python/install/lib/python3.7/ctypes/__init__.py", line 444, in <module> pythonapi = PyDLL(None) File "/home/gps/src/python-build-standalone.git/build/python/install/lib/python3.7/ctypes/__init__.py", line 356, in __init__ self._handle = _dlopen(self._name, mode) OSError: Dynamic loading not supported
I think it is a bug that
import ctypesraises OSError at import time in this environment. I can make a compelling argument that this error should either be suppressed or converted to an ImportError.Producing a fully statically linked Python executable is a bit of work and isn't easily accomplished with the existing build system. My "python-build-standalone" project automates the process. A fully statically linked Python executable is available in the zstd compressed archive at https://github.com/indygreg/python-build-standalone/releases/download/20190505/cpython-3.7.3-linux64-musl-20190526T0219.tar.zst under the python/install/bin/python3.7 path. Simply extract that archive and run that binary to reproduce.
@indygreg did you work out a way around this? I note your https://github.com/indygreg/python-build-standalone/blob/main/cpython-unix/patch-ctypes-static-binary.patch, but this does not seem to fix a static build of Python on its own.
I think this issue is still relevant on 3.15+
cpython/Lib/ctypes/__init__.py
Line 559 in 7f371ed
pythonapi = PyDLL(None) - addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or errorstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Jan 24, 2026 - added a commit that references this issue
on Sep 28, 2026 Backports are on auto-merge, and the 3.15 backport will be merged separately.
- added a commit that references this issue
on Oct 10, 2026
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
ctypesfrom statically linked interpreters #153890ctypesfrom statically linked interpreters (GH-153890) #158306ctypesfrom statically linked interpreters (GH-153890) #158307ctypesfrom statically linked interpreters (GH-153890) #158308