Running as administrator in Windows - python

I'm trying to write basic program with Python. I'm typing Windows commands with os library. Because of that, it doesn't work and wants to be admin. There was being superuser with a command in Linux(sudo). I couldn't find any way to run my program as administrator. I tried wmic and got an error named "Alias not found". Are there any way to run program as administrator?

If you are trying to make a program run in admin mode(the short handle we use for windows). If the object is an executable file or type of script and a few other file types I am not 100% certain of the extensive list. You would just right-click the file and select run as admin mode. If you are trying to run your program in admin mode I do believe that if you make python run as admin mode all the time your .py files should inherit the administrative privileges I would need someone to clarify that though. I am slightly confused as to your question though, as you say typing commands in os library are referring to the CMD(Command Prompt)?
Edit: In case you were referring to CMD open the start menu and type CMD and Right click and open as admin mode.

Related

How to associate my TKinter application to .txt files [duplicate]

This question already exists:
How to create file association in Windows 10 [duplicate]
Closed 1 year ago.
I have developed a .txt file opener application in Tkinter, python. I created the code to .exe and then I also made a setup.exe with InnoSetup. So now I want that all of the .txt files should be opened with my application.
As far as my coding skill goes on, I think this is not something to handle with Python. I think some PowerShell command or InnoSetup may be able to handle it.
So I have an open function in the application. Something like this:
def open(filepath):
bla bla bla
After some time, I got into my head how it will use my open function situated in my code. Can anyone help me to figure out how I can associate my application with all .txt files?
Please comment if there is a lack of information in this question. I will certainly clarify it.
This needs some workaround with the Windows Registry. This question's answers may be helpful: Associate file extension to python script, so that I can open the file by double click, in windows.
In your case, since it's an EXE, you might need to change the command a bit.
Firstly, run cmd (command prompt) as an administrator.
You can do that by searching for cmd and right clicking it to click the "Run as administrator" option.
The path can be just "C:\WINDOWS\system32>".
I tried changing the "%*", removing it and finally, I got a command which worked!
I tried the following command and it works fine for EXEs:
ftype txtfile=C:\Users\<Your name goes here>\main.exe %1
Or wherever your EXE is located (Copy-paste the path with the filename).
The syntax for EXEs is ftype txtfile=<path>\<filename>.exe %1.
Now, you should have the default app that opens .txt files changed!
The name "txtfile" can be changed using assoc .foo = foofile
You can find more about it in Honest Abe's answer.
You can do the same with the other registered file types also, I guess.
Since you want the setup to be distributed, I think changing something in the python file could help, since it needs the admin powers...
Maybe something like:
import win32com.shell.shell as shell
commands = 'ftype txtfile=C:\Users\<Your name goes here>\main.exe %1'
shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+commands)
This question provides more information on how to run command prompt as an administrator: How to run cmd command in Python with admin.

how to run explorer.exe as admin from python

background:
I want to run ESET Internet Security gui file from python as admin. It is named egui.exe. So in it's properties, under compatibility, I checked "run this program as an administrator" check box so it always run's as admin.
Another note is that for some reason that seems to be beyond me if I run this file in python like this:
os.startfile("c:\\program files\\ESET\\ESET Security\\egui.exe")
or
os.system("c:\\program files\\ESET\\ESET Security\\egui.exe")
It will freeze and do nothing. i tested the same approach in CMD like these:
c:\Program Files\ESET\ESET Security>egui.exe
c:\Program Files\ESET\ESET Security>start egui.exe
But both of these will cause CMD to freeze and do nothing and even ctrl + c will not interrupt it and CMD should be closed.
what I did:
The approach that somehow works is running the exe file via explorer.exe:
os.system('explorer.exe "c:\\program files\\ESET\\ESET Security\\egui.exe"')
This will not freeze but the problem is that because I want egui.exe to be run as admin, it will trigger UAC prompt, which is not acceptable because the program is supposed to be a test using GUI Automation.
Question:
I can run other programs as admin without triggering UAC prompt when my program itself is run as admin. But I cant when running it via explorer.exe and I think it is because explorer.exe is not as admin itself. So the question is how can I run explorer.exe as admin? Also entering password every time is not acceptable for obvious reasons.
PS: Unfortunately there is no compatibility setting for explorer.exe in its properties.

Why doesn't my python script run as a Mac app?

I've written a little GUI with tkinter for my own purposes on my Mac. I've written a two-line bash script to run it, and I can launch it from my desktop by double-clicking an icon. This is fine, but it pops up a terminal window, which I find annoying.
I would like to have it run as an app, without opening a terminal window. I followed the instructions in ccpizza's answer to create an app with Automator. I chose /usr/bin/python as the shell, and pasted in my python code. When I click on the desktop icon, I get an error box that says,
The action “Run Shell Script" encountered an error.
The console log has the error,
LaunchServices: Could not store lsd-identifiers file at /private/var/db/lsd/com.apple.lsdschemes.plist
I saw that /private/var/db/lsd is owned by root and I am the owner of the app. I tried
sudo chown root <appname>
and it doesn't give an error, but I am still shown as the owner of the app.
I've also tried choosing usr/local/bin/bash as the shell script, pasting in my bash script. When I run click the icon, I don't even get an error box, but the same error shows up in the console log.
Is there an easy fix for this? I'm just interested in getting this method to work. I'd rather not do anything more elaborate, like installing py2app or platypus.
I ended up getting platypus. It was trivial to do what I wanted. Just pick "None" for the interface.
If you just want to write a python script and run as a app, you can try pyinstaller.

Easy way to launch Python scripts with the mouse in OS-X

I'd like to write cross platform Python scripts that are GUI frontends for command line programs. The problem is I know a few Mac users who think that using the terminal will have the same effect as throwing their computer off the top of a skyscraper. In Linux and Windows it's easy enough to setup a Python script so the user can double click an icon and the script will start without opening any extra windows. Is there an easy way to do this with OS-X? Would the user have to install a different Python than the one that comes with OS-X? I haven't been able to find a definitive answer.
You might want to look at Platypus. It's a freeware app for generating apps which wrap scripts.
Another way to do something like that is using Automator or even AppleScript Editor. Either can produce an application which just runs a script.
Update:
For Automator: Launch Automator, select the Application template, type "script" in the search field, double-click Run Shell Script, switch the shell pop-up menu to /usr/bin/python, type/paste your Python script into the text field. Or, leave the pop-menu on /bin/bash and just write an invocation of an external script in the text field. Save as an application.
You can also view help from its Help menu.
For AppleScript, launch AppleScript Editor, type the following as the script:
do shell script "/usr/bin/true"
Replace /usr/bin/true with the path to whatever script you like. Save as an application.
Again, there's help in the Help menu.
py2app does this with aplomb. You make your Python script, use whatever dependencies you need (wx, Tkinter, etc.) and py2app makes you a standalone app bundle that will run in any modern OS X environment. It bundles Python too, so you can use any Python you want (not just the system default).
The downside is that the generated apps might be large, up to 50MB if you have a lot of dependencies (though that is somewhat of an extreme).
There are two ways to do this:
Click on a script.
Press command-i to open the "get info" window.
Expand the "Open With" section (if it isn't already).
Choose "Python Launcher" from the drop-down menu.
Click "Change All" if you would like ALL Python scripts to launch when double clicked.
Possibly open Python Launcher and uncheck "Run in a Terminal window"
This will work for this machine only, so it is less portable than the following. Why? Because the default for opening a document type varies depending on what is installed (XCode and/or IDLE will both take over opening a .py file).
Method Two:
Validate the Interpreter Directive, that's the first line of the file. I suggest using /usr/bin/env python3. This will run the first python3 interpreter that is on the users path.
Make the script executable chmod a+x <script_name> from the Terminal.
Change the extension from .py to .command (this will be opened by the Terminal).
Use zip or tar for distribution so that the permissions do not get mangled.
This method will open a Terminal window, but when the Python window is closed the terminal window will also close.
If your script has dependencies outside of the standard library, then you should provide a second .command file to install those. This may make things more complicated, but using pip3 install --user <list of dependencies> should minimize complications.

How do I add a python script to the startup registry?

I'm trying to make my python script run upon startup but I get the error message windowserror access denied, but I should be able to make programs start upon boot because teamviewer ( a third-party program I downloaded ) runs every time I restart my computer so I know that I should be able to make my program run at startup (I might be doing something different though, so if you could shed some light on what teamviewer is doing differently to get its script to run at startup that would be helpful).
Here is my script
import _winreg, webbrowser
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Software\Microsoft\Windows\CurrentVersion\Run')
_winreg.SetValueEx(key,'pytest',0,_winreg.REG_BINARY,'C:\Users\"USERNAME"\Desktop\test.py')
key.Close()
webbrowser.open('www.youtube.com')
Any input is appreciated.
import webbrowser
webbrowser.open('www.youtube.com')
Get rid of all of that _winreg stuff. Instead, you (assuming double-clicking on a py file opens the console) should simply place it in your startup folder (C:\Users\yourusername\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup on Windows 7, and C:\Documents and Settings\yourusername\Start Menu\Programs\Startup in XP). This works because Windows tries to open all files in the startup folder, and if Python opens PYs by default, Windows will open the Python console. Try restarting, that should work.
Baboon:
I am a little late posting, but you seem to have left off the sam at the end of your code here.
When you open a key you need to add the user rights, if you do not _winreg defaults to "READ":
Here is a snippet from the python site
http://docs.python.org/2/library/_winreg.html#access-rights
sam is an integer that specifies an access mask that describes the desired security access for the key. Default is KEY_READ. See Access Rights for other allowed values.
Here is the code corrected:
import _winreg, webbrowser
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Software\Microsoft\Windows\CurrentVersion\Run',_winreg.KEY_SET_VALUE)
_winreg.SetValueEx(key,'pytest',0,_winreg.REG_BINARY,'C:\Users\"USERNAME"\Desktop\test.py')
key.Close()
webbrowser.open('www.youtube.com')

Categories

Resources