.pyw launched on startup via registry launches and quits but launching with double click works...?

718 Views Asked by At

I have a .pyw script that works when I double click it etc and it stays open till I close it but I've added it to the registry to run at startup. It does run on startup but it doesn't stay open like its set to. It flashes the gui and then just closes.

Any ideas why this is happening or how to fix it?

P.S I don't want to create a shortcut in the startup folder linking to the .pyw file.

I added the my python script to the registry with another python script :p

import _winreg
aReg    = _winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE)
aKey    = _winreg.OpenKey(aReg, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, _winreg.KEY_WRITE)
_winreg.SetValueEx(aKey,"MyScript",0, _winreg.REG_SZ, myScript_path) 

And when I browse that path in the registry: HKEY_LOCAL_MACHINE > SOFTWARE > Microsoft > Windows > CurrentVersion > Run it is there with the correct path in data and type REG_SZ

2

There are 2 best solutions below

0
On BEST ANSWER

There was a very strange error that only happened at startup since one of the processes the script seemed to use hadn't been started yet. Running as a .py with python.exe didn't produce the error, it only happened when the script was run as a .pyw so I wrote the sys.stdout to a file to log its progress and errors.

Before I had used the logging module but it didn't seem to give as much detail as just outputting straight to file.

After checking the file I was able to fix the code so essentially it was a code problem but quite a sneaky one.

0
On

My two cents: had the same issue (.pyw in startup folder just flashed its gui then closed). Added a 'sleep' before the import commands and it worked. I believe the OS takes a little time to get ready for one/some import commands in my code.

#coding:utf-8

from time import sleep

sleep(20)

from Tkinter import *
from Tix import *
from time import strftime
import urllib
from itertools import izip_longest
import winsound