package it into a service, that should then be installed
add it to the windows registry (HKCU\Software\Microsoft\Windows\CurrentVersion\Run)
add a shortcut to it to the startup folder of start menu - its location may change with OS version, but installers always have some instruction to put a shortcut into that folder
use windows' task scheduler, and then you can set the task on several kind of events, including logon and on startup.
The actual solution depends on your needs, and what the script is actually doing.
Some notes on the differences:
Solution #1 starts the script with the computer, while solution #2 and #3 start it when the user who installed it logs in.
It is also worth to note that #1 always start the script, while #2 and #3 will start the script only on a specific user (I think that if you use the default user then it will start on everyone, but I am not sure of the details).
Solution #2 is a bit more "hidden" to the user, while solution #3 leaves much more control to the user in terms of disabling the automatic start.
Finally, solution #1 requires administrative rights, while the other two may be done by any user.
Solution #4 is something I discovered lately, and is very straightforward. The only problem I have noticed is that the python script will cause a small command window to appear.
As you can see, it all boils down to what you want to do; for instance, if it is something for your purposes only, I would simply drag it into startup folder.
In any case, lately I am leaning on solution #4, as the quickest and most straightforward approach.
Above mentioned all the methods did not worked I tried them all , I will tell you more simpler solution and alternative of windows task scheduler
Create a .bat file with content
"ADDRESS OF YOUR PROJECT INTERPRETER" "ADDRESS OF YOUR PYTHON SCRIPT WITH SCRIPT NAME"
Store this bat file into the window startup folder(by default hidden)
FYI: to find window startup folder
press windos+r then
type shell:startup -- it will directly take you to the startup folder
copy the bat file there with following 2 address in the same format ,
then simply restart the system or shut down and boot up.
The code will automatically run within 20 seconds of opening.
import winreg
def set_autostart_registry(app_name, key_data=None, autostart: bool = True) -> bool:
"""
Create/update/delete Windows autostart registry key
! Windows ONLY
! If the function fails, OSError is raised.
:param app_name: A string containing the name of the application name
:param key_data: A string that specifies the application path.
:param autostart: True - create/update autostart key / False - delete autostart key
:return: True - Success / False - Error, app name dont exist
"""
with winreg.OpenKey(
key=winreg.HKEY_CURRENT_USER,
sub_key=r'Software\Microsoft\Windows\CurrentVersion\Run',
reserved=0,
access=winreg.KEY_ALL_ACCESS,
) as key:
try:
if autostart:
winreg.SetValueEx(key, app_name, 0, winreg.REG_SZ, key_data)
else:
winreg.DeleteValue(key, app_name)
except OSError:
return False
return True
def check_autostart_registry(value_name):
"""
Check Windows autostart registry status
! Windows ONLY
! If the function fails, OSError is raised.
:param value_name: A string containing the name of the application name
:return: True - Exist / False - Not exist
"""
with winreg.OpenKey(
key=winreg.HKEY_CURRENT_USER,
sub_key=r'Software\Microsoft\Windows\CurrentVersion\Run',
reserved=0,
access=winreg.KEY_ALL_ACCESS,
) as key:
idx = 0
while idx < 1_000: # Max 1.000 values
try:
key_name, _, _ = winreg.EnumValue(key, idx)
if key_name == value_name:
return True
idx += 1
except OSError:
break
return False