3 examples of 'python run as administrator' in Python

Every line of 'python run as administrator' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
95def RunAsAdminWin32():
96 # FIXME ... what happens if "frozen"
97 script = os.path.abspath(sys.argv[0])
98 if sys.executable != script:
99 params = ' '.join([script] + sys.argv[1:])
100 else:
101 params = ' '.join(sys.argv[1:])
102
103 # fMask = 0
104 fMask = win32com.shell.shellcon.SEE_MASK_NO_CONSOLE
105 # fMask=win32com.shell.shellcon.SEE_MASK_NOCLOSEPROCESS
106 try:
107 win32com.shell.shell.ShellExecuteEx(
108 nShow=win32con.SW_SHOWNORMAL,
109 fMask=fMask,
110 lpVerb='runas',
111 lpFile=sys.executable,
112 lpParameters=params)
113 except pywintypes.error, e:
114 return False, e[2]
115
116 # If ShellExecuteEx was ok ... this will never be reached
117 # return True, None
118 # In any case exit to avoid a "single instance check" failure
119 sys.exit(0)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

99def runpython_install(args):
100 destination_dir = os.path.expanduser("~") + '/Library/Application Scripts/com.microsoft.Excel'
101 if not os.path.exists(destination_dir):
102 os.makedirs(destination_dir)
103 shutil.copy(os.path.join(this_dir, 'xlwings.applescript'), destination_dir)
104 print('Successfully installed RunPython for Mac Excel 2016!')
13def _managepy(args, djangoenv='develop', environment={}):
14 with shell_env(DJANGOENV=djangoenv, **environment):
15 local('python manage.py {args}'.format(args=args))

Related snippets