Python Forum
help to boot application on computer startup
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help to boot application on computer startup
#1
ok so i already googled it and found this article
i followed the steps but it still didn't work. i copied the application and csv file into the startup folder. i created a python script file with the code in the article and copied the path into the HKEY registry. Still no luck. Please help
chaebol2th likes this post
Reply
#2
Please show what you have tried.
Reply
#3
The example has some errors.
The registry path doesn't use raw string, which is necessary to prevent accidental escaping.

Instead of using os.path...., use pathlib.Path. I don't know why someone wrote 2021 a tutorial with code which was outdated for years since 2021.

You don't need to close the registry. OpenKey returns an object, which has support for context manager.

The naming was not Pythonic. Use PascalCase for classes. For functions lower case and word split by _.

autostart.py
from winreg import HKEY_CURRENT_USER, OpenKey, KEY_ALL_ACCESS, SetValueEx, REG_SZ from pathlib import Path def add_to_registry(name : str, script: Path | str): script = Path(script) quoted_script = f'"py.exe {script}"' if not script.exists(): raise ValueError(f"Script '{script}' does not exist") # use raw string to avoid unwanted escaping # e.g. \r, \n, \b, \t are Escape Sequences interpreted sub_key = r"Software\Microsoft\Windows\CurrentVersion\Run" # OpenKey returns winreg.PyHKEY object # which supports context manager # it closes the registry before leaving the # block with OpenKey( key=HKEY_CURRENT_USER, sub_key=sub_key, access=KEY_ALL_ACCESS, ) as registry: SetValueEx(registry, name,0, REG_SZ, quoted_script) if __name__=="__main__": add_to_registry("TEST", Path.home().joinpath("AppData", "Local", "script.py"))
The test script:
Path: C:\Users\USERNAME\AppData\Local\script.py
from datetime import datetime as DateTime from pathlib import Path with Path.home().joinpath("Desktop", "time.txt").open("w") as fd: fd.write(DateTime.now().isoformat()) fd.write("\n")
This creates a file named time.txt on your Desktop.
If you want to hide the window during running the code, further investigation is required.
I don't use Widows, so my knowledge is limited.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
(Jan-21-2025, 02:12 PM)jacksfrustration Wrote: ok so i already googled it and found this article
i followed the steps but it still didn't work. i copied the application and csv file into the startup folder. i created a python script file with the code in the article and copied the path into the HKEY registry. Still no luck. Please help

Please show what you have tried.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Autostart to run a GUI program at startup. Rpi Edward_ 1 1,766 Oct-28-2023, 03:19 PM
Last Post: SpongeB0B
  how to startup canbus interface on pi korenron 2 4,241 Oct-24-2021, 09:51 PM
Last Post: DeaD_EyE
  How to send data from a python application to an external application aditya_rajiv 1 3,513 Jul-26-2021, 06:00 AM
Last Post: ndc85430
  pexpect startup help korenron 2 5,169 Apr-27-2021, 07:23 AM
Last Post: korenron
  Error when running script on startup in Linux NoahTheNerd 0 2,951 Mar-07-2021, 04:54 PM
Last Post: NoahTheNerd
  Running from boot Murray6301 5 5,471 Nov-07-2020, 05:41 PM
Last Post: snippsat
  Run script on startup in Debian with systemd? MrGlasspoole 5 5,926 Jul-12-2020, 11:48 AM
Last Post: MrGlasspoole
  Script won't run at boot ebolisa 6 4,739 Mar-21-2020, 08:18 PM
Last Post: ebolisa
  run python script on startup? korenron 1 4,070 May-06-2019, 09:18 PM
Last Post: snippsat
  Python script runs on startup but does not register keystrokes. mericanpi 3 5,006 Sep-07-2018, 02:58 PM
Last Post: mericanpi

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.