Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a python script to the startup registry?

I'm trying to make my python script run upon startup but I get the error message windowserror access denied, but I should be able to make programs start upon boot because teamviewer ( a third-party program I downloaded ) runs every time I restart my computer so I know that I should be able to make my program run at startup (I might be doing something different though, so if you could shed some light on what teamviewer is doing differently to get its script to run at startup that would be helpful).

Here is my script

import _winreg, webbrowser
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Software\Microsoft\Windows\CurrentVersion\Run')
_winreg.SetValueEx(key,'pytest',0,_winreg.REG_BINARY,'C:\Users\"USERNAME"\Desktop\test.py') 
key.Close()
webbrowser.open('www.youtube.com')

Any input is appreciated.

like image 594
Baboon Avatar asked Jan 16 '12 06:01

Baboon


People also ask

How do I create a Python startup?

The first task in getting a Python program to boot on start-up is to create a script file that call the Python program to execute. Copy and paste the following code into a file and then save that file as “startup.sh”. To keep things simple, make sure that the script file is saved to your Documents folder.


1 Answers

import webbrowser
webbrowser.open('www.youtube.com')

Get rid of all of that _winreg stuff. Instead, you (assuming double-clicking on a py file opens the console) should simply place it in your startup folder (C:\Users\yourusername\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup on Windows 7, and C:\Documents and Settings\yourusername\Start Menu\Programs\Startup in XP). This works because Windows tries to open all files in the startup folder, and if Python opens PYs by default, Windows will open the Python console. Try restarting, that should work.

like image 141
elijaheac Avatar answered Sep 20 '22 12:09

elijaheac