【轉載】windows安裝python2.7後的註冊表問題

來源:互聯網
上載者:User

標籤:err   gis   jpg   .com   erp   exe   python   another   exce   

原文出自:https://www.cnblogs.com/tlz888/p/6879227.html

【提要】win平台上,python2.7官網的安裝包在安裝後不會添加環境變數且不會把安裝資訊寫入註冊表。

把python和pip的安裝路徑添加到環境變數是做python開發必要的一步,而寫入註冊表的原因是,有些python包以

windows installer的形式安裝,安裝的時候需要用到python的註冊表資訊,比如,numpy, scipy。

安裝步驟:

  (1)到python官網下載安裝包,www.python.org/downloads,運行安裝;

  (2)把python.exe所在路徑(python安裝路徑)以及pip.exe路徑(python安裝路徑下的Script檔案加)添加到path環境變數。

比如我的python在這裡:“C:\Python27”,那麼添加路徑:“C:\Python27”和“C:\Python27\Scripts”到path環境變數;

  (3)在註冊表中添加python註冊資訊,用於python可以操作windows的註冊表,可以運行python檔案來完成此步操作,

以下為python源碼,把它拷貝出來,放在任意位置,用python運行即可。

import sysfrom _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():    print "begin RegisterPy "    try:        print "open key : %s"%regpath        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 "*** EXCEPT: 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 "*** ERROR:Unable to register!"     print "*** REASON:You probably have another Python installation!"def UnRegisterPy():    #print "begin UnRegisterPy "    try:        print "open HKEY_CURRENT_USER key=%s"%(regpath)        reg = OpenKey(HKEY_CURRENT_USER, regpath)        #reg = OpenKey(HKEY_LOCAL_MACHINE, regpath)    except EnvironmentError:          print "*** Python not registered?!"        return    try:       DeleteKey(reg, installkey)       DeleteKey(reg, pythonkey)       DeleteKey(HKEY_LOCAL_MACHINE, regpath)    except:       print "*** Unable to un-register!"    else:       print "--- Python", version, "is no longer registered!"            if __name__ == "__main__":      RegisterPy()

如所示,出現Pyhton 2.7 is now registered!字樣即為註冊成功。

  

  在註冊表中也能看到相應的資訊:

  

  如果由於諸如安裝後又卸載了多個版本python的原因導致註冊表資訊不對,可以直接手動編輯註冊表,然後重新註冊。

  手動在註冊表中添加註冊資訊的方法跟上述python代碼中過程一致。

【轉載】windows安裝python2.7後的註冊表問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.