7

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.

oroy
  • 82
  • 16
Baboon
  • 307
  • 3
  • 6
  • 14
  • 3
    Why don't you simply add it in your startup folder? If `.py` files are associated with python interpreter, that should work fine – wim Jan 16 '12 at 06:17
  • Do you mean "run this when I log into my computer" or "run in the background"? py2exe can be used to create Services that run when the computer boots. Putting your script in the startup folder works if you want it to run for your user account. – g.d.d.c Jan 16 '12 at 06:37

2 Answers2

5
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.

elijaheac
  • 912
  • 2
  • 8
  • 23
3

Baboon: I am a little late posting, but you seem to have left off the sam at the end of your code here.

When you open a key you need to add the user rights, if you do not _winreg defaults to "READ":

Here is a snippet from the python site http://docs.python.org/2/library/_winreg.html#access-rights

sam is an integer that specifies an access mask that describes the desired security access for the key. Default is KEY_READ. See Access Rights for other allowed values.

Here is the code corrected:

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