Is it possible to programaticly run compiled Python (comiled via py2exe) as administrator in Vista?
Some more clarification:
I have written a program that modifies the windows hosts file (c:Windowssystem32driversetchosts) in Vista the program will not run and will fail with an exception unless you right-click and run as administrator even when the user has administrator privileges, unlike in XP where it will run if the user has administration rights, so I need a way to elevate it to the correct privileges programaticly.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Following the examples from Python2xLibsite-packagespy2exesamplesuser_access_control just add uac_info="requireAdministrator" to console or windows dict:
windows = [{
'script': "admin.py",
'uac_info': "requireAdministrator",
},]
Method 2
Do you mean that you want Windows to prompt for elevation when your program is run? This is controlled by adding a UAC manifest to the EXE’s resources. This blog entry explains how to create the manifest and how to compile it into a .RES file.
I don’t know what facilities py2exe has for embedding custom .RES files, so you might need to use the MT.EXE tool from the Platform SDK to embed the manifest in your program. MT.EXE doesn’t need .RES files; it can merge the .manifest file directly.
Method 3
Following up Roger Lipscombe’s comment, I’ve used a manifest file in py2exe without any real knowledge of what I was doing. So this might work:
# in setup.py
# manifest copied from http://blogs.msdn.com/shawnfa/archive/2006/04/06/568563.aspx
manifest = '''
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<asmv3:trustInfo xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<asmv3:security>
<asmv3:requestedPrivileges>
<asmv3:requestedExecutionLevel
level="asInvoker"
uiAccess="false" />
</asmv3:requestedPrivileges>
</asmv3:security>
</asmv3:trustInfo>
</assembly>
'''
setup(name='MyApp',
#...
windows=[ { #...
'other_resources':[(24, 1, manifest)],
}]
)
You may need to do some fiddling though..
Method 4
The best solution for pyinstaller is found here: Request UAC elevation from within a Python script?
No amount of manipulation of exe manifest file will work. Handle uac from within python code using if else statement
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0