Skip to content

Instantly share code, notes, and snippets.

@boseji
Last active August 29, 2015 14:13
Show Gist options
  • Save boseji/3aaa513c10d197186b43 to your computer and use it in GitHub Desktop.
Save boseji/3aaa513c10d197186b43 to your computer and use it in GitHub Desktop.
Windows Python Path Registration Script
#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Low for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# Websource:
# http://effbot.org/zone/python-register.htm
#
# FixInfo:
# http://stackoverflow.com/a/4579917
#
# -----------------------------------
# Modified by Boseji - 20150112
# -----------------------------------
#
# This script was updated to provide functionality for x64 architectures
#
# Note: This script Mandates PyWin32 package to be already installed
# http://sourceforge.net/projects/pywin32/
#
# IMPORTANT: This codes needs to run in an Administrator Command Prompt
# Open Start menu type 'cmd', then on 'cmd.exe' right click
# and select 'Run as Administrator'. This would start the
# Administrative command prompt.
#
#
# LICENSE: This code has been released under MIT License for the project
# MSP430-QRframe (https://github.com/boseji/MSP430-QRframe)
#
#
#
#
## Imports
import sys
import platform
from _winreg import *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
regpath2 = "SOFTWARE\\Wow6432Node\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (installpath, installpath, installpath)
def RegisterValues(reg):
try:
if QueryValue(reg, installkey) != installpath:
SetValue(reg, installkey, REG_SZ, installpath)
if QueryValue(reg, pythonkey) != pythonpath:
SetValue(reg, pythonkey, REG_SZ, pythonpath)
except:
return False
return True
def Win32Register():
try:
reg = OpenKey(HKEY_LOCAL_MACHINE, regpath)
except EnvironmentError:
try:
reg = CreateKey(HKEY_LOCAL_MACHINE, regpath)
except:
print "*** Unable to register! Win32"
return False
st = RegisterValues(reg)
CloseKey(reg)
if st:
print "--- Python", version, "is now registered! for Win32"
return True
print "*** Unable to register! Win32"
return False
def Win64Register():
try:
reg2 = OpenKey(HKEY_LOCAL_MACHINE, regpath2)
except EnvironmentError:
try:
reg2 = CreateKey(HKEY_LOCAL_MACHINE, regpath2)
except:
print "*** Unable to register! Win64"
return False
st = RegisterValues(reg2)
CloseKey(reg2)
if st:
print "--- Python", version, "is now registered! for Win64"
return True
print "*** Unable to register! Win64"
return False
def RegisterPy():
Win32Register()
if platform.machine() == 'AMD64':
Win64Register()
if __name__ == "__main__":
RegisterPy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment