Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Autorun a Python script on windows startup?
Making a Python script run automatically when Windows starts can be useful for background tasks, monitoring applications, or system utilities. There are two main approaches: using the Startup folder or modifying the Windows Registry.
Method 1: Using Windows Startup Folder
The simplest approach is to place your Python script (or a shortcut to it) in the Windows Startup folder. Windows automatically runs all programs in this folder during boot.
Startup Folder Location
C:\Users\current_user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
Steps to Add Script
1. Navigate to the Startup folder (you may need to enable "Show hidden files" in File Explorer).
2. Copy your Python script or create a shortcut to it in this folder.
3. Ensure .py files are associated with Python, not a text editor.
Method 2: Using Windows Registry
This method programmatically adds your script to the Windows Registry, making it run at startup. This approach requires administrative privileges and should be used carefully.
Registry Location
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
Python Implementation
import winreg as reg
import os
def add_to_registry():
# Get the current script's directory
script_path = os.path.dirname(os.path.realpath(__file__))
# Python file name with extension
script_name = "my_startup_script.py"
# Full path to the script
full_path = os.path.join(script_path, script_name)
# Registry key and path
key = reg.HKEY_CURRENT_USER
key_value = "Software\Microsoft\Windows\CurrentVersion\Run"
# Open the registry key
try:
registry_key = reg.OpenKey(key, key_value, 0, reg.KEY_ALL_ACCESS)
# Add the script to startup
reg.SetValueEx(registry_key, "MyPythonScript", 0, reg.REG_SZ, full_path)
# Close the key
reg.CloseKey(registry_key)
print("Script successfully added to startup!")
except Exception as e:
print(f"Error: {e}")
# Run the function
if __name__ == "__main__":
add_to_registry()
Removing Script from Startup
import winreg as reg
def remove_from_registry():
key = reg.HKEY_CURRENT_USER
key_value = "Software\Microsoft\Windows\CurrentVersion\Run"
try:
registry_key = reg.OpenKey(key, key_value, 0, reg.KEY_ALL_ACCESS)
reg.DeleteValue(registry_key, "MyPythonScript")
reg.CloseKey(registry_key)
print("Script removed from startup!")
except FileNotFoundError:
print("Entry not found in registry.")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
remove_from_registry()
Comparison
| Method | Difficulty | Risk Level | Best For |
|---|---|---|---|
| Startup Folder | Easy | Low | Simple scripts, manual setup |
| Registry | Moderate | Medium | Automated deployment, advanced users |
Important Considerations
? Ensure your Python script handles errors gracefully since it will run without user interaction.
? Test your script thoroughly before adding it to startup.
? Consider using absolute paths for any file operations in your script.
? Be cautious with registry modifications as incorrect changes can affect system stability.
Conclusion
Use the Startup folder method for simple scenarios and manual setup. Use the Registry method when you need programmatic control or are deploying scripts automatically. Always test your startup scripts thoroughly to avoid boot issues.
