天天看點

windows10 下使用Pycharm2016 基于Anaconda3 Python3.6 安裝Mysql驅動總結

本文記錄:在PyCharm2016.3.3 中基于Anaconda3 Python3.6版本安裝Python for Mysql驅動。嘗試了安裝Mysql-Connector成功,但是連接配接資料庫時驅動有bug而失敗;嘗試安裝MySQLdb,因Python版本相容問題 失敗。最終安裝PyMysql成功。現将詳細安裝過程記錄如下。

若不想折騰,且系統環境與我的一緻,建議直接就安裝PyMysql吧。

一,系統環境

windows10 64位,安裝了Anaconda3 和PyCharm2016.3.3。此外,windows10下還自己安裝了Python3.4版本(已配置好環境變量),如下圖:

PyCharm中配置的Python解釋器為Anaconda3自帶的Python環境,如下圖:

安裝的Mysql版本如下:

mysql  Ver 14.14 Distrib 5.5.53, for debian-linux-gnu (x86_64) using readline 6.2      

二,Python for Mysql driver 介紹

對于Python而言,有很多通路Mysql的驅動,主要是Mysqldb、mysqlconnector 和 pymysql(參考這篇文章)

MySQL-python: This package contains the MySQLdb module, which is written in C.
It is one of the most commonly used Python packages for MySQL.
mysql-connector-python: This package contains the mysql.connector module, which is written entirely in Python.
PyMySQL: This package contains the pymysql module, which is written entirely in Python. 
It is designed to be a drop-in replacement for the MySQL-python package.      

三,本地環境安裝MySQL Connector/Python

在MySQL Connector/Python官網下載下傳:mysql-connector-python-2.1.7-py3.4-windows-x86-64bit.msi 輕按兩下安裝成功。

cmd指令行下執行 import mysql.connector 成功,如下圖:

但是,我想使用Anaconda3下自帶的Python環境,而不想使用本地的Python3.4環境。因為Anaconda3中已經幫我們自帶了很多第三方的依賴包,比如Python中的ORM架構:SQLAlchemy 。而下面也将 通過 “from sqlalchemy import create_engine”來測試Mysql資料庫連接配接

于是,打開Anaconda Prompt,執行指令:

conda install -c anaconda mysql-connector-python       

安裝 mysql-connector-python 成功。參考:how do i get mysql to work with python 3.6 in anaconda ide (spyder)? 

于是,開始歡喜滿滿地去測試是否能成功地連接配接資料庫。

打開PyCharm,在菜單欄“Tools”--->“Python console”打開Python Console:

執行如下指令:

from sqlalchemy import create_engine
engine = create_engine('mysql+mysqlconnector://username:password.@HOST_IP:PORT/db_name?charset=utf8')
conn = engine.connect()      

報錯如下:

sqlalchemy.exc.InterfaceError: (mysql.connector.errors.InterfaceError) Failed parsing handshake; end byte not present in buffer      

網上搜了一下,說是Bug,錯誤無法解決,參考這裡。如能解決,請留言告知,感激不盡。

于是,嘗試安裝MySQLdb。參考了這篇文章,執行:conda install mysql-python,結果報錯:The following specifications were found to be in conflict

說是MySQLdb不支援Python3.6版本。具體可參考這篇文章,于是又隻得放棄。

The mysql-python conda package hasn't been built for python 3 . 
You can try creating a python 2.7 environment and install mysql-python into that.      

再嘗試:PyMysql,PyMysql項目的目标是替換MySQLdb

This package contains a pure-Python MySQL client library. 
The goal of PyMySQL is to be a drop-in replacement for MySQLdb and work on CPython, PyPy and IronPython.      

在Anaconda Prompt中執行:

conda install -c anaconda pymysql ,參考:這裡

安裝成功後,在PyCharm中,Ctrl+Alt+S 搜尋“python”,打開project 解釋器,可看到已成功安裝 pymysql

于是可以使用PyMySQL 來測試連接配接Mysql資料庫了。執行如下指令成功。(注意替換成你自己的資料庫名)。至此,在PyCharm中基于 Anaconda3的Python3.6 終于可以通路Mysql資料庫了。

from sqlalchemy import create_engine
engine=create_engine('mysql+pymysql://username:password.@HOST_IP:PORT/db_name?charset=utf8')
conn = engine.connect()
r = conn.execute('SELECT field1,field2 FROM DB_NMAE limit 1 ')      

另外,順便說一下,對于不同的驅動,連接配接Mysql的語句是不同的:具體可參考 sqlalchemy 官方文檔。

#MySQL-Python
mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>

#pymysql
mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]

#mysqlconnector
mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>      

另外,還可以在Python Console中使用 %run 直接運作編寫好的 .py 檔案:如下圖所示,在 audit包下,建立了一個my_file.py測試檔案

#audit/my_file.py
s = "Hello World"
print(s)      

原文:http://www.cnblogs.com/hapjin/p/7800959.html