Skip to content

Instantly share code, notes, and snippets.

@stondo
Created April 12, 2014 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stondo/10526701 to your computer and use it in GitHub Desktop.
Save stondo/10526701 to your computer and use it in GitHub Desktop.
Python3 on Windows and virtual environments
# Credit goes to Mauro Riccardi (mauro.riccardi[at]gmail.com)
# You can read more about this on our blog: http://bitsentangled.com/2014/04/12/python3-on-windows-and-virtual-environments/
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# Adapted by Ned Batchelder from a script
# written by Joakim Low for Secret Labs AB / PythonWare
# Edited for Python3 compatibility by bitsentangled.com
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
import sys
try:
from winreg import *
except ImportError: # python 2
from _winreg import *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)
def RegisterPy():
try:
reg = OpenKey(HKEY_LOCAL_MACHINE, regpath)
except EnvironmentError:
try:
reg = CreateKey(HKEY_LOCAL_MACHINE, regpath)
except Exception as e:
print("*** Unable to register: %s" % (e,))
return
try:
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
print("--- Python %s at %s is now registered!" % (version, installpath))
except PermissionError:
print("Permission Error: please run the_python.py as administrator!")
if __name__ == "__main__":
RegisterPy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment