py3 與 Django 鏈接數據庫: No module named 'MySQLdb'

py3 與 Django 鏈接數據庫: No module named 'MySQLdb'


enter description here

一言:讓這個無趣的世界變得有趣起來,這就是我一輩子的信念.

問題

在使用 Django 鏈接 MySQL 數據庫的時候,出現了錯誤html

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

解決

在 python2 中,使用 pip install mysql-python 進行安裝鏈接MySQL的庫,使用時 import MySQLdb 進行使用python

在 python3 中,改變了鏈接庫,改成了 pymysql 庫,使用pip install pymysql 進行安裝,直接導入import pymysql使用mysql

原本在上面的基礎上把 python3 的 pymysql 庫安裝上去就好了,可是問題依舊sql

通過查閱得知, Django 依舊是使用 py2 的 MySQLdb 庫的,獲得進行適當的轉換才行shell

__init__.py 文件中添加如下代碼數據庫

import pymysql
pymysql.install_as_MySQLdb()

額,找了一下卻沒有找到 install_as_MySQLdb() 這個方法的源碼,不過顧名思義應該是讓 Django 把 pymysql 當成 MySQLdb 來使用吧django

相關文章
相關標籤/搜索