How a Mac OS software can restart itself with admin permission in Python 3.7?


Python 3.7

Mac OS Catalina 10.15


I am developing a software in python. I would like to distribute it as commercial software.

I am compiling it with Nuitka for Windows and PyInstaller for Mac (because Nuitka can’t do the job on Mac).

My software need to install some libraries and other stuff like JDK, SDK tools, etc…
It needs also to change some environment variables. This can be done only with admin permissions.

So the first thing my software does is to check if it is running with admin permissions or not. If it is not the case it restart itself with admin permissions.

I succeeded to do it for Windows, but I have issues for Mac.

Here is the code:

def is_admin():
        is_admin = os.getuid() == 0

    except AttributeError:
        is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
    return is_admin

def StartMyAppasAdmin():
    if not is_admin():
        if platform.system() == 'Windows':
            ctypes.windll.shell32.ShellExecuteW(None, "runas", 'hello.exe', '', None, 1)
        elif platform.system() == 'Darwin':
  ["sudo","/usr/bin/open", os.path.join(os.path.dirname(sys.argv[0]),"hello")])

if __name__ == "__main__":

In Windows, it works fine.

In Mac OS, the app can’t be launch. And if I open the executable from terminal, it ask for admin password, then it restart and ask again for admin password, then it restart, etc… in a loop.

Someone knows what I am doing wrong?

Is this function valid for MacOS?

No. I am also searching the one for Mac. :grinning: