首页 > 解决方案 > 如何在 Windows 10 中为 Django 安装 mysqlclient?

问题描述

我是 Django 的新手。我想为我的项目使用 Mysql 数据库。我正在为此安装 mysqlclient,但它显示以下错误。我正在使用 --pip install mysql-python 命令安装 mysqlclient

Collecting mysql-python   Using cached MySQL-python-1.2.5.zip (108 kB) Using legacy setup.py install for mysql-python, since package 'wheel' is not installed. Installing collected packages: mysql-python
    Running setup.py install for mysql-python ... error
    ERROR: Command errored out with exit status 1:
     command: 'f:\xampp\htdocs\django\scripts\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\DELL\\AppData\\Local\\Temp\\pip-install-ti26jpbi\\mysql-python\\setup.py'"'"';
__file__='"'"'C:\\Users\\DELL\\AppData\\Local\\Temp\\pip-install-ti26jpbi\\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\DELL\AppData\Local\Temp\pip-record-h90u4076\install-record.txt'
--single-version-externally-managed --compile --install-headers 'f:\xampp\htdocs\django\include\site\python3.8\mysql-python'
         cwd: C:\Users\DELL\AppData\Local\Temp\pip-install-ti26jpbi\mysql-python\
    Complete output (24 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win32-3.8
    copying _mysql_exceptions.py -> build\lib.win32-3.8
    creating build\lib.win32-3.8\MySQLdb
    copying MySQLdb\__init__.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\converters.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\connections.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\cursors.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\release.py -> build\lib.win32-3.8\MySQLdb
    copying MySQLdb\times.py -> build\lib.win32-3.8\MySQLdb
    creating build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\__init__.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\CR.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\ER.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\FLAG.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\REFRESH.py -> build\lib.win32-3.8\MySQLdb\constants
    copying MySQLdb\constants\CLIENT.py -> build\lib.win32-3.8\MySQLdb\constants
    running build_ext
    building '_mysql' extension
    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/
    ---------------------------------------- ERROR: Command errored out with exit status 1: 'f:\xampp\htdocs\django\scripts\python.exe' -u
-c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\DELL\\AppData\\Local\\Temp\\pip-install-ti26jpbi\\mysql-python\\setup.py'"'"';
__file__='"'"'C:\\Users\\DELL\\AppData\\Local\\Temp\\pip-install-ti26jpbi\\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\DELL\AppData\Local\Temp\pip-record-h90u4076\install-record.txt'
--single-version-externally-managed --compile --install-headers 'f:\xampp\htdocs\django\include\site\python3.8\mysql-python' Check the logs for full command output.

我正在使用 Windows 10。如果有人可以帮助我解决此错误,我将不胜感激。

标签: pythonmysqlmysql-python

解决方案


我将指出2个解决方案。根据您的需要进行选择。

  1. 轻松解决。请注意,这是一种替代方法,而不是修复方法。此外,这不会为您提供最新版本。只要它适合您的需求可能就足够了。
  1. 更好的解决方案。这是对pip install mysqlclient. 请一步一步地进行,虽然它是虚拟的,但它有效:
  • 转到:https ://downloads.mysql.com/archives/cc/
  • 下载并安装x64版本;
  • 移至; C:\Program Files\MySQL\MySQL Connector C 6.1_C:\Program Files (x86)\MySQL\MySQL Connector C 6.1
  • 转到:https ://mariadb.com/downloads/connectors/connectors-data-access/c-connector
  • 下载并安装适用于 MS Windows(x64 位)的最新 C 连接器
  • C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include
  • 创建一个名为:mariadb
  • 将内容复制C:\Program Files\MariaDB\MariaDB Connector C 64-bit\includeC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include\mariadb
  • C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib
  • 创建一个名为:mariadb
  • 将内容复制C:\Program Files\MariaDB\MariaDB Connector C 64-bit\libC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib\mariadb
  • pip install mysqlclient
  • 完毕!

测试:使用最新的可用配置测试了上述两种解决方案:

  • 蟒蛇 3.10
  • mysql 8.0.27
  • django 3.2.9

仅供参考:这些不是唯一的两个解决方案。例如,如果您使用 dockerize、使用 VM 或仅使用 Windows 10 的新功能将 ubuntu 作为应用程序,您也可以修复此问题。


推荐阅读