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

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

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

 報錯以下:mysql

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

根據本身的操做系統版本選擇下載的文件類型。我是選擇的62位的。下載完成後,咱們使用pip命令去安裝剛剛下載的mysql文件。windows

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

 

而後咱們在去執行:pip2 install MySQL-python -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.compost

發現已經安裝成功了。而後咱們進入到python2的交互模式,執行 import  MySQLdb 發現導入成功!!!!ui

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

 在安裝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爲主。

 

相關文章
相關標籤/搜索