[转载]Python注册表信息丢失的解决方案

今天安装Python的模块时,安装失败,提示信息:Python version 2.7 required, which was not found in the registry.

原因在于Python在注册表中没有注册信息,需要手动添加:

 1 #
 2 # script to register Python 2.0 or later for use with win32all
 3 # and other extensions that require Python registry settings
 4 #
 5 # written by Joakim Loew for Secret Labs AB / PythonWare
 6 #
 7 # source:
 8 # http://www.pythonware.com/products/works/articles/regpy20.htm
 9 #
10 # modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html
11  
12 import sys
13  
14 from _winreg import *
15  
16 # tweak as necessary
17 version = sys.version[:3]
18 installpath = sys.prefix
19  
20 regpath = "SOFTWARE\Python\Pythoncore\%s\" % (version)
21 installkey = "InstallPath"
22 pythonkey = "PythonPath"
23 pythonpath = "%s;%s\Lib\;%s\DLLs\" % (
24     installpath, installpath, installpath
25 )
26  
27 def RegisterPy():
28     try:
29         reg = OpenKey(HKEY_CURRENT_USER, regpath)
30     except EnvironmentError as e:
31         try:
32             reg = CreateKey(HKEY_CURRENT_USER, regpath)
33             SetValue(reg, installkey, REG_SZ, installpath)
34             SetValue(reg, pythonkey, REG_SZ, pythonpath)
35             CloseKey(reg)
36         except:
37             print "*** Unable to register!"
38             return
39         print "--- Python", version, "is now registered!"
40         return
41     if (QueryValue(reg, installkey) == installpath and
42         QueryValue(reg, pythonkey) == pythonpath):
43         CloseKey(reg)
44         print "=== Python", version, "is already registered!"
45         return
46     CloseKey(reg)
47     print "*** Unable to register!"
48     print "*** You probably have another Python installation!"
49  
50 if __name__ == "__main__":
51     RegisterPy()

将上面的代码保存为register.py 然后双击运行,问题即可解决。

方法转载自:http://effbot.org/zone/python-register.htm