I've been working on a small script that's supposed to run in the background of my computer. I already have the script working and everything except one thing that hours of google searching and hunting have not found the answer to.
The file has a .pyw extension, and when I run it from the command prompt:
pythonw File.pyw
the cmd window will continue on and give me another prompt without launching idle like it would with a regular .py file.
When I double-click the file, the cmd window opens and closes without a problem, just like it's supposed to. This is all perfect for me.
However, I tried to make a small batch file:
cd C:\Users\(my name)\Desktop
pythonw File.pyw
and I stuck that in the startup folder of windows. However, when I restarted my computer to see if it would work, it opened the cmd window, but wouldn't close it. I can't figure out why. I've tried everything I can think of, including sticking the File.pyw directly in the startup folder, and trying to put an exit command right in the batch file like so:
cd C:\Users\(my name)\Desktop
pythonw File.pyw
exit
But, as you can probably guess, that failed. I tried putting the command directly in my code, so right before the end, it had the line
os.system("exit")
but after realizing this wouldn't work, I just took it out. (Important detail: the last line of the code is set to loop it until the program closes. That's why I'm trying to use the pyw extension, so the console can close before the file ends)
Next, I shortened the batch file to only be the one line:
pythonw.exe C:\Users\(my name)\Desktop\File.pyw
but it still won't work. When the batch file is run, I get an open cmd window with the command entered, but it runs like a regular .py file, not closing the cmd window.
Can anyone help me figure out why the console won't close when the command is run from a .batch file, but will when run directly from command prompt?
UPDATE:
the script is meant to add a quick keyboard shortcut to close a task, specifically Google Chrome, when I press '+' twice quickly. Here's a my full code (minus some personal info)
import os
import sys
import pyHook, pythoncom
setting key to be '+' to avoid accidental usage.
def OnKeyBoardEvent(event):
global prevPlus, escPushed
if event.Ascii == 43:
if prevPlus == None:
prevPlus = event.Time
else:
if event.Time - prevPlus <= 1000:
os.system("taskkill /IM chrome.exe")
prevPlus = event.Time
else:
prevPlus = event.Time
elif event.Ascii == 27:
if escPushed == None:
escPushed = event.Time
else:
if event.Time - escPushed <= 1000:
sys.exit()
else:
escPushed = event.Time
getter = pyHook.HookManager()
getter.KeyDown = OnKeyBoardEvent
getter.HookKeyboard()
prevPlus = None
escPushed = None
pythoncom.PumpMessages()
This all works perfectly when I'm running it from pycharm, or from cmd, but when I put it in C:\Users(my name)\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup and then try it out, nothing happens. I'm guessing it's because windows runs the file and exits it before anything happens, but I'm really not sure. I just really need a solution that makes it run at startup and take my key input until I shut down.
So, my final approach was just to link a VBS file (like it was mentioned in the comments.) It turned out I was doing it wrong, and mixing file types, so I had weird errors. That VBS file called a BAT file silently, so the cmd window didn't show up. That BAT file then called my program, which for some reason ran better when called from cmd than when it was just executed on the spot. Now, it does run on startup, and the cmd window doesn't appear, so it is a good fix, though inefficient.