import pymysql
pymysql.install_as_MySQLdb()
執行python manage.py makemigrations時報錯:
mysqlclient 1.3.13 or newer is required; you have 0.9.3.
於是放棄使用pymysql這個包,直接用mysqlclient算了,那麼安裝mysqlclient 。
使用命令:pip install mysqlclient 安裝,但是又出錯:
MySQLdb/_mysql.c(29): fatal error C1083: 無法打開包括文件: “mysql.h”: No such file or directory
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\……\\x64\\cl.exe'
查看MySQL安裝目錄的文件include中是有mysql.h這個文件的,另外關Visual Studio什麼事呢?好菜哦,想不明白,再百一百
,有人說去https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient下載安裝包,然後我下載了1.3.13的64位那個文件,放到項目文件夾後,信心滿滿的執行安裝命令:
pip install mysqlclient-1.3.13-cp34-cp34m-win32.whl。
結果又敗了:
mysqlclient-1.3.13-cp34-cp34m-win_amd64.whl is not a supported wheel on this platform.
再百一百,說是換成32位的,結果還是敗了:
mysqlclient-1.3.13-cp34-cp34m-win32.whl is not a supported wheel on this platform.
誰叫是菜鳥呢,繼續百一百吧,說是先看一下pip支持的文件格式
>>> import pip
>>> print(pip.pep425tags.get_supported()) # 列出pip支持的文件名格式
[('cp27', 'none', 'win32'), ('py2', 'none', 'win32'), ('cp27', 'none', 'any'), ('cp2', 'none', 'any'), ('cp26', 'none', 'any'), ('cp25', 'none', 'any'), ('cp24', 'none', 'any'), ('cp23', 'none', 'any'), ('cp22', 'none', 'any'), ('cp21', 'none', 'any'), ('cp20', 'none', 'any'), ('py27', 'none', 'any'), ('py2', 'none', 'any'), ('py26', 'none', 'any'), ('py25', 'none', 'any'), ('py24', 'none', 'any'), ('py23', 'none', 'any'), ('py22', 'none', 'any'), ('py21', 'none', 'any'), ('py20', 'none', 'any')]
於是更改mysqlclient-1.3.13-cp36-cp36m-win_amd64.whl文件名再進行安裝
最終更改爲mysqlclient-1.3.13-py36-none-any.whl之後:
Processing d:\rd\py\myoa\mysqlclient-1.3.13-py36-none-any.whl
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.13
終於安裝完成。
好了,安裝完成,總是滿懷希望的,繼續debug吧,然後
ImportError: DLL load failed while importing _mysql: 找不到指定的模塊。
爲什麼?百了一下,這是PyCharm自帶的Python版本問題,創建應用時他會在當前應用中生成自己的一個,把他換掉就可以了