18cb86eadSVedant Kumar# On macOS, system python binaries like /usr/bin/python and $(xcrun -f python3) 28cb86eadSVedant Kumar# are shims. They do some light validation work and then spawn the "real" python 38cb86eadSVedant Kumar# binary. Find the "real" python by asking dyld -- sys.executable reports the 48cb86eadSVedant Kumar# wrong thing more often than not. This is also useful when we're running under 58cb86eadSVedant Kumar# a Homebrew python3 binary, which also appears to be some kind of shim. 68cb86eadSVedant Kumardef getDarwinRealPythonExecutable(): 78cb86eadSVedant Kumar import ctypes 8*2238dcc3SJonas Devlieghere 9*2238dcc3SJonas Devlieghere dyld = ctypes.cdll.LoadLibrary("/usr/lib/system/libdyld.dylib") 108cb86eadSVedant Kumar namelen = ctypes.c_ulong(1024) 11*2238dcc3SJonas Devlieghere name = ctypes.create_string_buffer(b"\000", namelen.value) 128cb86eadSVedant Kumar dyld._NSGetExecutablePath(ctypes.byref(name), ctypes.byref(namelen)) 13*2238dcc3SJonas Devlieghere return name.value.decode("utf-8").strip() 14*2238dcc3SJonas Devlieghere 158cb86eadSVedant Kumar 168cb86eadSVedant Kumarprint(getDarwinRealPythonExecutable()) 17