Python3 on Windows and virtual environments

This post is a follow-up of a quite old but exceedingly useful post from Ned Batchelder.

Python is a great language, with a great environment and a great community. It has loads of production-ready modules to solve almost every problem you might encounter. When you live on a Unix-like system, most of the times you have a single command line to issue to install each one of them.

But when you are on a PC running Windows, things can be not so smooth: you can find the details of the issues you would run into in the aforementioned post.

For those in the (surely temporary) TL;DR mood let's say that if you want to install a Python package which depends on some Python extension, in one of those useful Python features known as a virtual environment on the Windows operating system, grabbing the binary .exe or .msi won't usually help. Unless.

Unless you use that old script from that old post by Ned, the same script that saved more than a day to many, including ourselves (big thanks, mate!).

As years has passed by, though, and the language evolved, this script has unfortunately aged a bit, so that it seemed a useful thing for the Python community (at least those who, for whatever reason, operate on Windows) to payback and publish an updated version of it, that would work on Python3 as well.

So, here we go, meet the revised version of the_python.py

# 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 WindowsError as e:
        if e.errno==13:
            print("Permission Error: please run the_python.py as administrator!")
        else:
            pass

if __name__ == "__main__":  
    RegisterPy()

To use this script, run as administrator a command prompt, then give the command

\path\to\python\intepreter\python.py the_python.py

Here you can find the gist for this file.