On this page
winreg
Prev: webbrowser | Up: stdlib_module | Next: winsound
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — winreg
Tags: #public #python3 #stdlib
Import
import winreg
Overview
Windows-only interface to the Windows Registry (OpenKey(), QueryValueEx(), SetValueEx(), EnumKey(), EnumValue()). Use winreg to read installed software settings, OS theme/proxy preferences, or file associations on Windows without shelling out to reg.exe.
Quick Start
import sys
if sys.platform == "win32":
import winreg
with winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
r"SOFTWARE\Microsoft\Windows NT\CurrentVersion",
) as key:
product_name, reg_type = winreg.QueryValueEx(key, "ProductName")
print(product_name)
Implementation Walkthrough
1. Writing Per-User Settings Safely
Write to HKEY_CURRENT_USER (which doesn't require administrator privileges) using CreateKeyEx() as a context manager so handles always close:
import sys
def save_user_setting(subkey: str, name: str, value: str) -> None:
if sys.platform != "win32":
return
import winreg
with winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, subkey, 0, winreg.KEY_SET_VALUE) as key:
winreg.SetValueEx(key, name, 0, winreg.REG_SZ, value)
Gotchas & Boundaries
- Windows-only: Raises
ModuleNotFoundErroron macOS and Linux; always guard behindsys.platform == "win32". - Permissions & UAC: Writing under
HKEY_LOCAL_MACHINErequires elevated administrator rights and raisesPermissionErrorotherwise. - 32-bit vs. 64-bit registry redirection: Use
KEY_WOW64_64KEY/KEY_WOW64_32KEYwhen a 32-bit Python process needs to inspect the 64-bit registry view (or vice versa).
Common Functions
- winreg.ConnectRegistry() —
ConnectRegistry(computer_name, key) -> handle— Establishes a connection to a predefined registry handle on another computer. - winreg.OpenKey() —
OpenKey(key, sub_key, reserved=0, access=KEY_READ) -> handle— Opens the specified key. - winreg.QueryValueEx() —
QueryValueEx(key, value_name) -> (value, type_id)— Retrieves the type and data for a specified value name associated with an open registry key. - winreg.SetValueEx() —
SetValueEx(key, value_name, reserved, type, value)— Stores data in the value field of an open registry key.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/webbrowser
- tuts/python3/stdlib_module/winreg/ConnectRegistry
- tuts/python3/stdlib_module/winreg/OpenKey
- tuts/python3/stdlib_module/winreg/QueryValueEx
- tuts/python3/stdlib_module/winreg/SetValueEx
- tuts/python3/stdlib_module/winsound
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/webbrowser Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/winreg/ConnectRegistry Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/winreg/OpenKey Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/winreg/QueryValueEx Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/winreg/SetValueEx Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/winsound Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/argparse Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/getopt Linked here; shared tags: #stdlib