【问题标题】:How to Install mysqlclient for Django in windows 10?如何在 Windows 10 中为 Django 安装 mysqlclient?
【发布时间】:2020-11-16 15:57:43
【问题描述】:

我是 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。 如果有人可以帮助我解决此错误,我将不胜感激。

【问题讨论】:

  • 在这里使用我的答案stackoverflow.com/a/61293962/5193536
  • MySQL-python只支持Python 2。使用pip install mysqlclient安装mysqlclient。确保您安装了 64 位 Python - 这样它就会安装轮子。
  • 我在使用 pip install mysqlclient 时遇到同样的错误。
  • mysqlclient 可能会出现类似的错误,但不会相同,因此请改为显示该错误。正如我之前所说,最简单的解决方案是切换到 64 位 Python,然后您就不必构建 mysqlclient

标签: python mysql mysql-python


【解决方案1】:

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

  1. 简单的解决方案。请注意,这是一种替代方法,而不是修复方法。此外,这不会为您提供最新版本。只要能满足您的需求,可能就足够了。
  1. 更好的解决方案。这是对pip install mysqlclient 的修复。请按照步骤进行,虽然它是虚拟的,但它有效:
  • 转至:https://downloads.mysql.com/archives/c-c/
  • 下载并安装 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\include复制到C:\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\lib复制到C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib\mariadb
  • 运行pip install mysqlclient
  • 完成!

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

  • python 3.10
  • mysql 8.0.27
  • django 3.2.9

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

【讨论】:

    【解决方案2】:

    您需要安装包含 C++ 编译器的 Microsoft Visual C++ 14.0 工具:https://wiki.python.org/moin/WindowsCompilers

    您可以在此处下载许多库的预编译二进制文件:https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient(适用于有关包采购的常见警告)

    【讨论】:

    • 目前,有official wheels 用于 Windows 上 64 位 Python 的最新 mysqlclient 版本。
    【解决方案3】:

    您可以从here 下载mysqlclient。只需检查您的 python 版本并下载 .whl 文件。

    然后只需将命令提示符中的文件名复制到下载文件夹并键入

    pip install {fullfile name}
    

    【讨论】:

      【解决方案4】:

      请仔细查看您提供的错误消息中的这一行:

       error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/
      

      您正在寻找什么的答案。

      【讨论】:

        猜你喜欢
        • 2018-09-01
        • 2016-04-03
        • 2017-03-31
        • 2018-12-11
        • 1970-01-01
        • 2022-12-07
        • 2018-01-08
        • 2017-08-31
        相关资源
        最近更新 更多