1

I am trying to make my python script to start on windows boot. I have managed to start with registry, shortcut in startup folder and taskscheduler. However, windows stops the script immediately. This does not persist if I use the same start command on CMD or double clicking the shortcut. Only when trying to get it start automatically.

One more problem i have noticed, that when i run script with .pyw extension nothing happens. But it works if i use pythonw in CMD. Not sure if that has to do anything with the problem.

I am using Windows 10, python 3.7 and this is my code:

from serial import Serial
from time import sleep

ser = Serial('COM7', 9600)
while True:
    ser.write(b'1')
    sleep(1)

Launch script:

pythonw "%UserProfile%\PyCharm\hearthbeat\hearthbeat.pyw"
martineau
  • 119,623
  • 25
  • 170
  • 301
Nick
  • 29
  • 4
  • Looks like this question has already been answered, please see the following Question: https://stackoverflow.com/questions/4438020/how-to-start-a-python-file-while-windows-starts If the answers posted there do not apply to your problem, please provide more information on how exactly you are trying to start your script on startup. – Michael Krupp Feb 08 '19 at 20:45
  • The answer does not help with the problem. I have managed to start the script on boot with various ways. The problem is that the script is killed after it is started and does not run in background when it should. Please read into title – Nick Feb 08 '19 at 20:53
  • Is your script exiting with an error message? – yorodm Feb 08 '19 at 21:19
  • Change the interpreter to `python` and see if any error messages are being produced. You may need to add an `input("press return to continue")` statement at the end of the script to keep the window open long enough to give you a chance to read it (or them). – martineau Feb 08 '19 at 21:20
  • Everything works fine when i start it manually, even with pythonw. It runs as supossed to. The process is killed when it is started automatically on windows startup. – Nick Feb 08 '19 at 21:28
  • FYI, on Windows, the `.pyw` extension is associated with the `pythonw` version of the interpreter, which has no automatic `stdout` and `stderr` output files — so anything printed to either of them won't display and error message won't be visible. That's why I suggested using the regular "console" version if it named `python` in my last comment. The problem may also have something to do which one of the possible way you're starting the script when the computer boots (so that detail may matter). – martineau Feb 08 '19 at 21:29

0 Answers0