diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index da1aaa89fef7c7..018d673fa2bc02 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1704,6 +1704,10 @@ These prefabricated library loaders are available: :c:expr:`int`, which is of course not always the truth, so you have to assign the correct :attr:`!restype` attribute to use these functions. + .. note:: + + If the Python interpreter is statically linked, this may be ``None``. + .. audit-event:: ctypes.dlopen name ctypes.LibraryLoader Loading a library through any of these objects raises an diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index 0b84a27f8c6d61..0aebd5877d4a9d 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -553,7 +553,10 @@ def LoadLibrary(self, name): # These are Unix-like platforms which use a dynamically-linked libpython. pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY")) else: - pythonapi = PyDLL(None) + try: + pythonapi = PyDLL(None) + except OSError: + pythonapi = None if _os.name == "nt": diff --git a/Misc/NEWS.d/next/Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst b/Misc/NEWS.d/next/Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst new file mode 100644 index 00000000000000..6110b7404a811e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst @@ -0,0 +1,2 @@ +Fix :exc:`OSError` when importing :mod:`ctypes` from a statically linked +interpreter.