Welcome folks today in this blog post we will be discussing how to
autorun a python script on windows startup using registry settings. All the source code of application will be given below.
The Registry Path is located on your computer at:
Registry Path is located at :
In order to get started we need to make a
app.py file and copy paste the following code
# Python code to add current script to the registry # module to edit the windows registry import winreg as reg import os def AddToRegistry(): # in python __file__ is the instant of # file path where it was executed # so if it was executed from desktop, # then __file__ will be # c:\users\current_user\desktop pth = os.path.dirname(os.path.realpath(__file__)) # name of the python file with extension s_name="mYscript.py" # joins the file name to end of path address address=os.join(pth,s_name) # key we want to change is HKEY_CURRENT_USER # key value is Software\Microsoft\Windows\CurrentVersion\Run key = HKEY_CURRENT_USER key_value = "Software\Microsoft\Windows\CurrentVersion\Run" # open the key to make changes to open = reg.OpenKey(key,key_value,0,reg.KEY_ALL_ACCESS) # modifiy the opened key reg.SetValueEx(open,"any_name",0,reg.REG_SZ,address) # now close the opened key reg.CloseKey(open) # Driver Code if __name__=="__main__": AddToRegistry()