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