【mysql-python】No module named MySQLdb

0x01 问题描述

想要使用mysql,但是运行时提示“No module named MySQLdb”.

PS:自己从网上找了一些,安装了VC python的库,然后通过pipenv安装mysql-python和pycharm安装mysql-python时均失败

0x02 问题截图

0x03 安装mysql-python扩展时的报错信息

Collecting MySQL-python
  Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
Building wheels for collected packages: MySQL-python
  Running setup.py bdist_wheel for MySQL-python: started
  Running setup.py bdist_wheel for MySQL-python: finished with status 'error'
  Complete output from command C:\Users\Amazing\.virtualenvs\p1-aHLDr-UJ\Scripts\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\Amazing\\AppData\\Local\\Temp\\pycharm-packaging\\MySQL-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d c:\users\amazing\appdata\local\temp\pip-wheel-llowdq --python-tag cp27:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win-amd64-2.7
  copying _mysql_exceptions.py -> build\lib.win-amd64-2.7
  creating build\lib.win-amd64-2.7\MySQLdb
  copying MySQLdb\__init__.py -> build\lib.win-amd64-2.7\MySQLdb
  copying MySQLdb\converters.py -> build\lib.win-amd64-2.7\MySQLdb
  copying MySQLdb\connections.py -> build\lib.win-amd64-2.7\MySQLdb
  copying MySQLdb\cursors.py -> build\lib.win-amd64-2.7\MySQLdb
  copying MySQLdb\release.py -> build\lib.win-amd64-2.7\MySQLdb
  copying MySQLdb\times.py -> build\lib.win-amd64-2.7\MySQLdb
  creating build\lib.win-amd64-2.7\MySQLdb\constants
  copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-2.7\MySQLdb\constants
  copying MySQLdb\constants\CR.py -> build\lib.win-amd64-2.7\MySQLdb\constants
  copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-2.7\MySQLdb\constants
  copying MySQLdb\constants\ER.py -> build\lib.win-amd64-2.7\MySQLdb\constants
  copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-2.7\MySQLdb\constants
  copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-2.7\MySQLdb\constants
  copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-2.7\MySQLdb\constants
  warning: build_py: byte-compiling is disabled, skipping.
  
  running build_ext
  building '_mysql' extension
  creating build\temp.win-amd64-2.7
  creating build\temp.win-amd64-2.7\Release
  C:\Users\Amazing\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include" -IC:\Python27\include -IC:\Users\Amazing\.virtualenvs\p1-aHLDr-UJ\PC /Tc_mysql.c /Fobuild\temp.win-amd64-2.7\Release\_mysql.obj /Zl
  _mysql.c
  _mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
  error: command 'C:\\Users\\Amazing\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\cl.exe' failed with exit status 2
  
  ----------------------------------------
  Running setup.py clean for MySQL-python
Failed to build MySQL-python
Installing collected packages: MySQL-python
  Running setup.py install for MySQL-python: started
    Running setup.py install for MySQL-python: finished with status 'error'
    Complete output from command C:\Users\Amazing\.virtualenvs\p1-aHLDr-UJ\Scripts\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\Amazing\\AppData\\Local\\Temp\\pycharm-packaging\\MySQL-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record c:\users\amazing\appdata\local\temp\pip-record-idzhvg\install-record.txt --single-version-externally-managed --compile --install-headers C:\Users\Amazing\.virtualenvs\p1-aHLDr-UJ\include\site\python2.7\MySQL-python:
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-2.7
    copying _mysql_exceptions.py -> build\lib.win-amd64-2.7
    creating build\lib.win-amd64-2.7\MySQLdb
    copying MySQLdb\__init__.py -> build\lib.win-amd64-2.7\MySQLdb
    copying MySQLdb\converters.py -> build\lib.win-amd64-2.7\MySQLdb
    copying MySQLdb\connections.py -> build\lib.win-amd64-2.7\MySQLdb
    copying MySQLdb\cursors.py -> build\lib.win-amd64-2.7\MySQLdb
    copying MySQLdb\release.py -> build\lib.win-amd64-2.7\MySQLdb
    copying MySQLdb\times.py -> build\lib.win-amd64-2.7\MySQLdb
    creating build\lib.win-amd64-2.7\MySQLdb\constants
    copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-2.7\MySQLdb\constants
    copying MySQLdb\constants\CR.py -> build\lib.win-amd64-2.7\MySQLdb\constants
    copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-2.7\MySQLdb\constants
    copying MySQLdb\constants\ER.py -> build\lib.win-amd64-2.7\MySQLdb\constants
    copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-2.7\MySQLdb\constants
    copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-2.7\MySQLdb\constants
    copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-2.7\MySQLdb\constants
    warning: build_py: byte-compiling is disabled, skipping.
    
    running build_ext
    building '_mysql' extension
    creating build\temp.win-amd64-2.7
    creating build\temp.win-amd64-2.7\Release
    C:\Users\Amazing\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include" -IC:\Python27\include -IC:\Users\Amazing\.virtualenvs\p1-aHLDr-UJ\PC /Tc_mysql.c /Fobuild\temp.win-amd64-2.7\Release\_mysql.obj /Zl
    _mysql.c
    _mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
    error: command 'C:\\Users\\Amazing\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\cl.exe' failed with exit status 2
    
    ----------------------------------------

  Failed building wheel for MySQL-python
Command "C:\Users\Amazing\.virtualenvs\p1-aHLDr-UJ\Scripts\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\Amazing\\AppData\\Local\\Temp\\pycharm-packaging\\MySQL-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record c:\users\amazing\appdata\local\temp\pip-record-idzhvg\install-record.txt --single-version-externally-managed --compile --install-headers C:\Users\Amazing\.virtualenvs\p1-aHLDr-UJ\include\site\python2.7\MySQL-python" failed with error code 1 in C:\Users\Amazing\AppData\Local\Temp\pycharm-packaging\MySQL-python\

可以考虑改用 PyMySQL。

同时数据库 URI 改成: mysql+pymysql://...

OK,谢谢推荐。:grin:

1 个赞