7

I need to create an executable for windows 8.1 and lower, so I tried Py2Exe (no success) and then PyInstaller, and damn it, it worked. Now I need to run it as admin (everytime since that uses admin tasks). My actual compiling script looks like this : python pyinstaller.py --onefile --noconsole --icon=C:\Python27\my_distrib\ico_name --name=app_name C:\Python27\my_distrib\script.py Is there an option to use UAC or things like this ? (its mess to me)

Also, everytime I start it on my laptop windows 8.1 (my desktop computer is windows 7) it says that is dangerous... Anything to make it a "trust" exe? Thanks in advance

Maxim
  • 149
  • 2
  • 12
  • I believe the trust level is more tied to the location rather than the application itself. And to get an application to request admin rights, you need to add a manifest that specifies it or rename your app to "setup.exe". – Mark Ransom Mar 08 '15 at 01:50

2 Answers2

21

PyInstaller 3.0 includes the --uac-admin option!

cdonts
  • 9,304
  • 4
  • 46
  • 72
0

If you want more direct control over when the program asks for admin privileges, or if you don't want to rely on PyInstaller, you can use the PyUAC module (for Windows). Install it using:

pip install pyuac
pip install pypiwin32

Direct usage of the package is:

import pyuac

def main():
    print("Do stuff here that requires being run as an admin.")
    # The window will disappear as soon as the program exits!
    input("Press enter to close the window. >")

if __name__ == "__main__":
    if not pyuac.isUserAdmin():
        print("Re-launching as admin!")
        pyuac.runAsAdmin()
    else:        
        main()  # Already an admin here.

Or, if you wish to use the decorator:

from pyuac import main_requires_admin

@main_requires_admin
def main():
    print("Do stuff here that requires being run as an admin.")
    # The window will disappear as soon as the program exits!
    input("Press enter to close the window. >")

if __name__ == "__main__":
    main()

You can then use your PyInstaller command without the UAC option.

(from this answer)

The Amateur Coder
  • 789
  • 3
  • 11
  • 33