Skip to content

Instantly share code, notes, and snippets.

@1208nn
Created April 13, 2024 11:00
Show Gist options
  • Save 1208nn/63758451e4feb88833433b75a7cf2145 to your computer and use it in GitHub Desktop.
Save 1208nn/63758451e4feb88833433b75a7cf2145 to your computer and use it in GitHub Desktop.
import winreg
from os import getcwd
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Environment", 0, winreg.KEY_READ | winreg.KEY_WRITE)
path, _ = winreg.QueryValueEx(key, "Path")
if "%N0ENV%" not in path:
path += ";%N0ENV%"
path=path.replace(';;',';')
winreg.SetValueEx(key, "Path", 0, winreg.REG_EXPAND_SZ, path)
try:
path, _ = winreg.QueryValueEx(key, "N0ENV")
if getcwd() not in path:
path += f";{getcwd()}"
winreg.SetValueEx(key, "N0ENV", 0, winreg.REG_EXPAND_SZ, path)
except FileNotFoundError:
winreg.SetValueEx(key, "N0ENV", 0, winreg.REG_EXPAND_SZ, getcwd())
winreg.CloseKey(key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment