解决pip安装mysqlclient报错error C1083: Cannot open include file: ‘mysql.h‘: No such file or directory

问题描述:

系统:win10(64位)

python版本:3.7.8

MySQL Server 8.0

MySQL Connector C 6.1

使用pip install -r requirements.txt 命令时,安装到mysqlclient时报错(mysqlclient已通过pip命令安装完成):

  ERROR: Command errored out with exit status 1:
     command: 'c:\python3.7.8\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\pip-install-k4s1hfze\\mysqlclient\\setup.py'"'"'; __file__='"'"'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\pip-install-k4s1hfze\\mysqlclient\\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\ADMINI~1\AppData\Local\Temp\pip-record-8r_vb76d\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\python3.7.8\Include\mysqlclient'
         cwd: C:\Users\ADMINI~1\AppData\Local\Temp\pip-install-k4s1hfze\mysqlclient\
    Complete output (30 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.7
    copying _mysql_exceptions.py -> build\lib.win-amd64-3.7
    creating build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\__init__.py -> build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\compat.py -> build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\connections.py -> build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\converters.py -> build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\cursors.py -> build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\release.py -> build\lib.win-amd64-3.7\MySQLdb
    copying MySQLdb\times.py -> build\lib.win-amd64-3.7\MySQLdb
    creating build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\CR.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\ER.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-3.7\MySQLdb\constants
    running build_ext
    building '_mysql' extension
    creating build\temp.win-amd64-3.7
    creating build\temp.win-amd64-3.7\Release
    C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include" -Ic:\python3.7.8\include -Ic:\python3.7.8\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\8.1\include\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\winrt" /Tc_mysql.c /Fobuild\temp.win-amd64-3.7\Release\_mysql.obj /Zl
    _mysql.c
    _mysql.c(29): fatal error C1083: Cannot open include file: 'mysql.h': No such file or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2
    ----------------------------------------
  Rolling back uninstall of mysqlclient
  Moving to c:\python3.7.8\lib\site-packages\mysqlclient-1.4.6.dist-info\
   from c:\python3.7.8\lib\site-packages\~ysqlclient-1.4.6.dist-info
  Moving to c:\python3.7.8\lib\site-packages\mysqldb\
   from c:\python3.7.8\lib\site-packages\~ysqldb
ERROR: Command errored out with exit status 1: 'c:\python3.7.8\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\pip-install-k4s1hfze\\mysqlclient\\setup.py'"'"'; __file__='"'"'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\pip-install-k4s1hfze\\mysqlclient\\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\ADMINI~1\AppData\Local\Temp\pip-record-8r_vb76d\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\python3.7.8\Include\mysqlclient' Check the logs for full command output.

报错截图如下:

解决pip安装mysqlclient报错error C1083: Cannot open include file: ‘mysql.h‘: No such file or directory_第1张图片

解决pip安装mysqlclient报错error C1083: Cannot open include file: ‘mysql.h‘: No such file or directory_第2张图片

原因解析:

对于64位Windows,pip安装mysqlclient时,它只在Program files (x86)文件夹中搜索了某些MySQL文件(例如" mysql.h" ),而不会搜索Program files,本机将MySQL连接器安装到了C:\Program Files\MySQL中,而在C:\Program Files (x86)中没有MySQL安装目录,所以在Program files (x86)中未找到任何内容,导致错误。

解决方法:

将C:\Program Files\MySQL\MySQL Connector C 6.1文件夹拷贝到C:\Program Files (x86)\MySQL\MySQL Connector C 6.1目录中(本人采用了非常简单粗暴的办法,拷贝了整个MySQL文件夹),重新执行命令pip install -r requirements.txt。命令执行成功

解决pip安装mysqlclient报错error C1083: Cannot open include file: ‘mysql.h‘: No such file or directory_第3张图片

E:\HttpRunnerManager>pip install -r requirements.txt
Requirement already satisfied: Django==2.0.3 in c:\python3.7.8\lib\site-packages (from -r requirements.txt (line 1)) (2.0.3)
Requirement already satisfied: PyYAML==3.12 in c:\python3.7.8\lib\site-packages (from -r requirements.txt (line 2)) (3.12)
Requirement already satisfied: requests==2.18.4 in c:\python3.7.8\lib\site-packages (from -r requirements.txt (line 3)) (2.18.4)
Requirement already satisfied: eventlet==0.22.1 in c:\python3.7.8\lib\site-packages (from -r requirements.txt (line 4)) (0.22.1)
Collecting mysqlclient==1.3.12
  Using cached mysqlclient-1.3.12.tar.gz (89 kB)
Collecting django-celery==3.2.2
  Using cached django-celery-3.2.2.tar.gz (90 kB)
Collecting flower==0.9.2
  Using cached flower-0.9.2.tar.gz (1.3 MB)
Collecting dwebsocket==0.4.2
  Using cached dwebsocket-0.4.2.tar.gz (14 kB)
Collecting paramiko==2.4.1
  Using cached paramiko-2.4.1-py2.py3-none-any.whl (194 kB)
Collecting HttpRunner==1.5.8
  Using cached HttpRunner-1.5.8-py2.py3-none-any.whl (48 kB)
Requirement already satisfied: pytz in c:\python3.7.8\lib\site-packages (from Django==2.0.3->-r requirements.txt (line 1)) (2020.1)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\python3.7.8\lib\site-packages (from requests==2.18.4->-r requirements.txt (line 3)) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\python3.7.8\lib\site-packages (from requests==2.18.4->-r requirements.txt (line 3)) (2020.6.20)
Requirement already satisfied: idna<2.7,>=2.5 in c:\python3.7.8\lib\site-packages (from requests==2.18.4->-r requirements.txt (line 3)) (2.6)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in c:\python3.7.8\lib\site-packages (from requests==2.18.4->-r requirements.txt (line 3)) (1.22)
Requirement already satisfied: greenlet>=0.3 in c:\python3.7.8\lib\site-packages (from eventlet==0.22.1->-r requirements.txt (line 4)) (0.4.16)
Requirement already satisfied: celery<4.0,>=3.1.15 in c:\users\administrator\appdata\roaming\python\python37\site-packages (from django-celery==3.2.2->-r requirements.txt (line 6)) (3.1.26.post2)
Collecting tornado>=4.2.0
  Using cached tornado-6.0.4-cp37-cp37m-win_amd64.whl (417 kB)
Collecting babel>=1.0
  Using cached Babel-2.8.0-py2.py3-none-any.whl (8.6 MB)
Requirement already satisfied: setuptools in c:\python3.7.8\lib\site-packages (from dwebsocket==0.4.2->-r requirements.txt (line 8)) (47.1.0)
Requirement already satisfied: six in c:\python3.7.8\lib\site-packages (from dwebsocket==0.4.2->-r requirements.txt (line 8)) (1.15.0)
Collecting cryptography>=1.5
  Using cached cryptography-3.1-cp37-cp37m-win_amd64.whl (1.5 MB)
Collecting pynacl>=1.0.1
  Using cached PyNaCl-1.4.0-cp37-cp37m-win_amd64.whl (206 kB)
Collecting pyasn1>=0.1.7
  Using cached pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
Collecting bcrypt>=3.1.3
  Using cached bcrypt-3.2.0-cp36-abi3-win_amd64.whl (28 kB)
Requirement already satisfied: har2case in c:\python3.7.8\lib\site-packages (from HttpRunner==1.5.8->-r requirements.txt (line 10)) (0.3.1)
Collecting requests-toolbelt
  Using cached requests_toolbelt-0.9.1-py2.py3-none-any.whl (54 kB)
Requirement already satisfied: Jinja2 in c:\python3.7.8\lib\site-packages (from HttpRunner==1.5.8->-r requirements.txt (line 10)) (2.11.2)
Requirement already satisfied: colorama in c:\python3.7.8\lib\site-packages (from HttpRunner==1.5.8->-r requirements.txt (line 10)) (0.4.3)
Collecting colorlog
  Using cached colorlog-4.2.1-py2.py3-none-any.whl (14 kB)
Requirement already satisfied: billiard<3.4,>=3.3.0.23 in c:\users\administrator\appdata\roaming\python\python37\site-packages (from celery<4.0,>=3.1.15->django-celery==3.2.2->-r requirements.txt (line 6)) (3.3.0.23)
Requirement already satisfied: kombu<3.1,>=3.0.37 in c:\users\administrator\appdata\roaming\python\python37\site-packages (from celery<4.0,>=3.1.15->django-celery==3.2.2->-r requirements.txt (line 6)) (3.0.37)
Requirement already satisfied: cffi!=1.11.3,>=1.8 in c:\python3.7.8\lib\site-packages (from cryptography>=1.5->paramiko==2.4.1->-r requirements.txt (line 9)) (1.14.2)
Requirement already satisfied: MarkupSafe>=0.23 in c:\python3.7.8\lib\site-packages (from Jinja2->HttpRunner==1.5.8->-r requirements.txt (line 10)) (1.1.1)
Requirement already satisfied: anyjson>=0.3.3 in c:\users\administrator\appdata\roaming\python\python37\site-packages (from kombu<3.1,>=3.0.37->celery<4.0,>=3.1.15->django-celery==3.2.2->-r requirements.txt (line 6)) (0.3.3)
Requirement already satisfied: amqp<2.0,>=1.4.9 in c:\users\administrator\appdata\roaming\python\python37\site-packages (from kombu<3.1,>=3.0.37->celery<4.0,>=3.1.15->django-celery==3.2.2->-r requirements.txt (line 6)) (1.4.9)
Requirement already satisfied: pycparser in c:\python3.7.8\lib\site-packages (from cffi!=1.11.3,>=1.8->cryptography>=1.5->paramiko==2.4.1->-r requirements.txt (line 9)) (2.20)
Using legacy 'setup.py install' for mysqlclient, since package 'wheel' is not installed.
Using legacy 'setup.py install' for django-celery, since package 'wheel' is not installed.
Using legacy 'setup.py install' for flower, since package 'wheel' is not installed.
Using legacy 'setup.py install' for dwebsocket, since package 'wheel' is not installed.
Installing collected packages: mysqlclient, django-celery, tornado, babel, flower, dwebsocket, cryptography, pynacl, pyasn1, bcrypt, paramiko, requests-toolbelt, colorlog, HttpRunner
  Attempting uninstall: mysqlclient
    Found existing installation: mysqlclient 1.4.6
    Uninstalling mysqlclient-1.4.6:
      Successfully uninstalled mysqlclient-1.4.6
    Running setup.py install for mysqlclient ... done
  Attempting uninstall: django-celery
    Found existing installation: django-celery 3.3.1
    Uninstalling django-celery-3.3.1:
      Successfully uninstalled django-celery-3.3.1
    Running setup.py install for django-celery ... done
    Running setup.py install for flower ... done
    Running setup.py install for dwebsocket ... done
  Attempting uninstall: HttpRunner
    Found existing installation: httprunner 3.1.4
    Uninstalling httprunner-3.1.4:
      Successfully uninstalled httprunner-3.1.4
Successfully installed HttpRunner-1.5.8 babel-2.8.0 bcrypt-3.2.0 colorlog-4.2.1 cryptography-3.1 django-celery-3.2.2 dwebsocket-0.4.2 flower-0.9.2 mysqlclient-1.3.12 paramiko-2.4.1 pyasn1-0.4.8 pynacl-1.4.0 requests-toolbelt-0.9.1 tornado-6.0.4

E:\HttpRunnerManager>

其他原因:

原因一:

1、Mysqlclient版本不匹配,Mysqlclient下载地址https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient

2、Mysqlclient安装命令

pip install mysqlclient-1.4.6-cp37-cp37m-win_amd64.whl

 

3、验证是否安装成功,使用命令:

python

>>> import MySQLdb

无报错即为成功

C:\Users\Administrator>python
Python 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>

原因二:

没有安装MySQL Connector-c,下载地址https://downloads.mysql.com/archives/c-c/,下载安装即可。

原因三:

使用pip install mysql-connector命令安装mysql-connector

C:\Users\Administrator>pip install mysql-connector
Collecting mysql-connector
  Downloading mysql-connector-2.2.9.tar.gz (11.9 MB)
     |████████████████████████████████| 11.9 MB 467 kB/s
Using legacy 'setup.py install' for mysql-connector, since package 'wheel' is not installed.
Installing collected packages: mysql-connector
    Running setup.py install for mysql-connector ... done
Successfully installed mysql-connector-2.2.9

参考:

https://blog.csdn.net/qq_15907907/article/details/88236280

https://www.codenong.com/51294268/

 

你可能感兴趣的:(Python报错,python,mysql)