【mysqlclient】MySQLdb/_mysql.c:1340:5: 错误:只允许在 C99 模式下使用‘for’循环初始化声明

报错信息如下:

[root@localhost home]# pip3 install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.0.2.tar.gz (88 kB)
Using legacy 'setup.py install' for mysqlclient, since package 'wheel' is not installed.
Installing collected packages: mysqlclient
    Running setup.py install for mysqlclient ... error
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python3.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-e456v8b0/mysqlclient_5c3bd443aca04f30a21398641a3f4551/setup.py'"'"'; __file__='"'"'/tmp/pip-install-e456v8b0/mysqlclient_5c3bd443aca04f30a21398641a3f4551/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-3ifs3sfe/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.7m/mysqlclient
         cwd: /tmp/pip-install-e456v8b0/mysqlclient_5c3bd443aca04f30a21398641a3f4551/
    Complete output (31 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.7
    creating build/lib.linux-x86_64-3.7/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.7/MySQLdb
    …………
    …………
    creating build/temp.linux-x86_64-3.7
    creating build/temp.linux-x86_64-3.7/MySQLdb
    gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -Dversion_info=(2,0,2,'final',0) -D__version__=2.0.2 -I/usr/include/mysql -I/usr/local/include/python3.7m -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.7/MySQLdb/_mysql.o -m64
    MySQLdb/_mysql.c: 在函数‘_mysql_row_to_dict_cached’中:
    MySQLdb/_mysql.c:1340:5: 错误:只允许在 C99 模式下使用‘for’循环初始化声明
         for (unsigned int i=0; i

解决方案:
手动下载,更改出错的代码,手动安装
解决步骤:

  1. 查找python下安装模块的文件夹site-packages,查看到多个文件夹,通过查看之前安装的模块确定安装位置
[root@localhost Python-3.7.4]# find / -name site-packages
/home/python/Python-3.7.4/Lib/site-packages
/usr/local/python3/lib/python3.7/site-packages
[root@localhost Python-3.7.4]# ls /usr/local/lib/python3.7/site-packages
colorlog  easy_install.py  pip-20.3.1.dist-info  __pycache__
  1. 使用wget下载mysqlclient的安装包,地址:https://pypi.org/project/mysqlclient/2.0.2/#files
[root@localhost site-packages]# wget https://files.pythonhosted.org/packages/0e/68/f79de0e0fcdb041783124b825771e0ba6a3d0893b7b4c4735f130e42af47/mysqlclient-2.0.2.tar.gz
--2020-12-15 17:56:38--  https://files.pythonhosted.org/packages/0e/68/f79de0e0fcdb041783124b825771e0ba6a3d0893b7b4c4735f130e42af47/mysqlclient-2.0.2.tar.gz
正在解析主机 files.pythonhosted.org (files.pythonhosted.org)... 151.101.109.63, 2a04:4e42:36::319
正在连接 files.pythonhosted.org (files.pythonhosted.org)|151.101.109.63|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:88293 (86K) [application/x-tar]
正在保存至: “mysqlclient-2.0.2.tar.gz”

100%[=================================================================================================================================================>] 88,293       372KB/s 用时 0.2s   

2020-12-15 17:56:39 (372 KB/s) - 已保存 “mysqlclient-2.0.2.tar.gz” [88293/88293])

  1. 解压
[root@localhost site-packages]# tar -xvf mysqlclient-2.0.2.tar.gz 
  1. [root@localhost Python-3.7.4]# cd mysqlclient-2.0.2/MySQLdb/
  2. vi _mysql.c
  3. 在vi界面中,输入1340n,直达第1340行,原for (unsigned int i=0; i ​​【mysqlclient】MySQLdb/_mysql.c:1340:5: 错误:只允许在 C99 模式下使用‘for’循环初始化声明_第1张图片
  4. 进入mysqlclient-2.0.2目录,安装
[root@localhost mysqlclient-2.0.2]# python setup.py install
  1. 完成

你可能感兴趣的:(python)