【提要】win平臺上,python2.7官網的安裝包在安裝後不會添加環境變量且不會把安裝信息寫入註冊表。python
把python和pip的安裝路徑添加到環境變量是作python開發必要的一步,而寫入註冊表的緣由是,有些python包以windows
windows installer的形式安裝,安裝的時候須要用到python的註冊表信息,好比,numpy, scipy。python2.7
安裝步驟:spa
(1)到python官網下載安裝包,www.python.org/downloads,運行安裝;code
(2)把python.exe所在路徑(python安裝路徑)以及pip.exe路徑(python安裝路徑下的Script文件加)添加到path環境變量。blog
好比個人python在這裏:「C:\Python27」,那麼添加路徑:「C:\Python27」和「C:\Python27\Scripts」到path環境變量;ip
(3)在註冊表中添加python註冊信息,用於python能夠操做windows的註冊表,能夠運行python文件來完成此步操做,ci
如下爲python源碼,把它拷貝出來,放在任意位置,用python運行便可。開發
1 import sys 2 3 4 from _winreg import * 5 6 # tweak as necessary 7 version = sys.version[:3] 8 installpath = sys.prefix 9 regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version) 10 installkey = "InstallPath" 11 pythonkey = "PythonPath" 12 pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % ( 13 installpath, installpath, installpath 14 ) 15 16 def RegisterPy(): 17 print "begin RegisterPy " 18 try: 19 print "open key : %s"%regpath 20 reg = OpenKey(HKEY_CURRENT_USER, regpath) 21 except EnvironmentError as e: 22 try: 23 reg = CreateKey(HKEY_CURRENT_USER, regpath) 24 SetValue(reg, installkey, REG_SZ, installpath) 25 SetValue(reg, pythonkey, REG_SZ, pythonpath) 26 CloseKey(reg) 27 except: 28 print "*** EXCEPT: Unable to register!" 29 return 30 31 print "--- Python", version, "is now registered!" 32 return 33 34 35 if (QueryValue(reg, installkey) == installpath and 36 QueryValue(reg, pythonkey) == pythonpath): 37 CloseKey(reg) 38 print "=== Python", version, "is already registered!" 39 return CloseKey(reg) 40 41 print "*** ERROR:Unable to register!" 42 print "*** REASON:You probably have another Python installation!" 43 44 def UnRegisterPy(): 45 #print "begin UnRegisterPy " 46 try: 47 print "open HKEY_CURRENT_USER key=%s"%(regpath) 48 reg = OpenKey(HKEY_CURRENT_USER, regpath) 49 #reg = OpenKey(HKEY_LOCAL_MACHINE, regpath) 50 except EnvironmentError: 51 print "*** Python not registered?!" 52 return 53 try: 54 DeleteKey(reg, installkey) 55 DeleteKey(reg, pythonkey) 56 DeleteKey(HKEY_LOCAL_MACHINE, regpath) 57 except: 58 print "*** Unable to un-register!" 59 else: 60 print "--- Python", version, "is no longer registered!" 61 62 if __name__ == "__main__": 63 RegisterPy()
以下圖所示,出現Pyhton 2.7 is now registered!字樣即爲註冊成功。get
在註冊表中也能看到相應的信息:
若是因爲諸如安裝後又卸載了多個版本python的緣由致使註冊表信息不對,能夠直接手動編輯註冊表,而後從新註冊。
手動在註冊表中添加註冊信息的方法跟上述python代碼中過程一致。