python操作mysql數據庫系列-安裝MySQLdb


一波三折,先是pip命令出現問題,然后各種方法嘗試解決。然后是直接使用pip2命令安裝報錯,mysql-python庫安裝再次出現問題。於是使用國內鏡像的方式去安裝:pip2 install MySQL-python -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com 這個命令去安裝,總算鏈接上了,下載下來了,但是依然有報錯。

pymongo 換成你想要安裝的包,,別照搬照抄哈~~~~~~

 報錯如下:

針對這個問題,我們再次尋找解決方法,報錯原因是因為我們缺少了一個文件     MySQL_python-1.2.5-cp27-none-win_amd64.whl     這個文件的下載地址https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python 

根據自己的操作系統版本選擇下載的文件類型。我是選擇的62位的。下載完成后,我們使用pip命令去安裝剛剛下載的mysql文件。

再當前目錄我們執行: pip2 install MySQL_python-1.2.5-cp27-none-win_amd64.whl  命令   如下圖表示安裝成功。

 

然后我們在去執行:pip2 install MySQL-python -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

發現已經安裝成功了。然后我們進入到python2的交互模式,執行 import  MySQLdb 發現導入成功!!!!

 方案二:直接下載  MySQLdb for python(32/64位)下載地址:http://www.codegood.com/archives/129

 在安裝MySQL-python-1.2.3.win-amd64-py2.7.exe時,提示:Python version 2.7 required,which was not found in the registry

這是在注冊表不能識別python2.7,原因windows是64位,安裝的python是32位。怎么查看自己安裝的python是多少位的呢?因為32位、64位都可以在64位操作系統中安裝,所以年代久遠的時候,就不記得當時自己安裝的python是32呢,還是64了。查看非常之簡單。輸入平時我們檢查是否有python環境的命令即可:

C:\Users\Administrator>python

MySQLdb for python(32/64位)下載地址:http://www.codegood.com/archives/129

解決方法:

1.新建一個register.py文件,將如下代碼copy進去:

復制代碼
#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html
 
import sys
 
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_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()
復制代碼

2.定位到該文件所在目錄運行python register.py

說明python2.7已經注冊成功。

3.再執行MySQLdb安裝程序,則會自動識別,並安裝成功了。

注意:目前Mysqldb這個模塊 不支持python3環境,所以我們的安裝教程是以python2為主。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM