背景
重裝系統,發現之前裝在E盤的python可以直接使用,就只是將python的安裝目錄加入到環境變量中,也一直沒有管它,今天跟天軟交互的時候發現一直不成功,猜測可能是沒有注冊表信息。
從網上找的一段代碼,直接復制運行即可,留存備查。
代碼
py3.5.2
import sysfrom winreg import *# tweak as necessaryversion = sys.version[:3]installpath = sys.prefixregpath = "SOFTWARE//Python//Pythoncore//%s//" % (version)installkey = "InstallPath"pythonkey = "PythonPath"pythonpath = "%s;%s//Lib//;%s//DLLs//" % ( installpath, installpath, installpath)def RegisterPy(): try: reg = OpenKey(HKEY_CURRENT_USER, regpath) except EnvironmentError as e: try: reg = CreateKey(HKEY_CURRENT_USER, regpath) SetValue(reg, installkey, REG_SZ, installpath) SetValue(reg, pythonkey, REG_SZ, pythonpath) CloseKey(reg) except: print("*** Unable to register!") return print("--- Python", version, "is now registered!") return if (QueryValue(reg, installkey) == installpath and QueryValue(reg, pythonkey) == pythonpath): CloseKey(reg) print("=== Python", version, "is already registered!") return CloseKey(reg) print("*** Unable to register!") print("*** You probably have another Python installation!")if __name__ == "__main__": RegisterPy()
py2.*版本
'''解決注冊問題,pywin32安裝存在問題'''import sysfrom _winreg import *# tweak as necessaryversion = sys.version[:3]installpath = sys.prefixregpath = "SOFTWARE//Python//Pythoncore//%s//" % (version)installkey = "InstallPath"pythonkey = "PythonPath"pythonpath = "%s;%s//Lib//;%s//DLLs//" % ( installpath, installpath, installpath)def RegisterPy(): try: reg = OpenKey(HKEY_CURRENT_USER, regpath) except EnvironmentError as e: try: reg = CreateKey(HKEY_CURRENT_USER, regpath) SetValue(reg, installkey, REG_SZ, installpath) SetValue(reg, pythonkey, REG_SZ, pythonpath) CloseKey(reg) except: print "*** Unable to register!" return print "--- Python", version, "is now registered!" return if (QueryValue(reg, installkey) == installpath and QueryValue(reg, pythonkey) == pythonpath): CloseKey(reg) print "=== Python", version, "is already registered!" return CloseKey(reg) print "*** Unable to register!" print "*** You probably have another Python installation!"if __name__ == "__main__": RegisterPy()
以上這篇將python安裝信息加入注冊表的示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網之家。
|
新聞熱點
疑難解答