Python pip 安装mysqlclient

问题描述 投票:0回答:5

所以我下载了Python 32位第1版,然后我就这么做了

python -m pip install mysqlclient

我需要 Visual C++ 2015,但我拥有它们,因为我的文本编辑器是 VSCode。 我卸载了 VSCode,安装了 Visual Studio Build Tools,每当我这样做时,我都会收到此错误

python -m pip install mysqlclient
。其中,cl.exe 没有添加到我的路径中,所以我添加了,但它仍然显示

 error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\cl.exe' failed with exit status 2

所以我下载了Windows的wheel包,正如其他一些stackoverflow答案所说。每当我运行它时,我都会得到

我尝试下载Python 64位下载轮子,将

cl.exe
添加到路径中。

Collecting mysqlclient
  Using cached https://files.pythonhosted.org/packages/4d/38/c5f8bac9c50f3042c8f05615f84206f77f03db79781db841898fde1bb284/mysqlclient-1.4.4.tar.gz
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: 'C:\Program Files (x86)\Python\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Patrik\\AppData\\Local\\Temp\\pip-install-0vmm7fdx\\mysqlclient\\setup.py'"'"'; __file__='"'"'C:\\Users\\Patrik\\AppData\\Local\\Temp\\pip-install-0vmm7fdx\\mysqlclient\\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\Patrik\AppData\Local\Temp\pip-wheel-o3ba369p' --python-tag cp37
       cwd: C:\Users\Patrik\AppData\Local\Temp\pip-install-0vmm7fdx\mysqlclient\
  Complete output (30 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating 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\_exceptions.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
  running build_ext
  building 'MySQLdb._mysql' extension
  creating build\temp.win-amd64-3.7
  creating build\temp.win-amd64-3.7\Release
  creating build\temp.win-amd64-3.7\Release\MySQLdb
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(1,4,4,'final',0) -D__version__=1.4.4 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include\mariadb" "-IC:\Program Files (x86)\Python\include" "-IC:\Program Files (x86)\Python\include" /TcMySQLdb/_mysql.c /Fobuild\temp.win-amd64-3.7\Release\MySQLdb/_mysql.obj /Zl /D_CRT_SECURE_NO_WARNINGS
  _mysql.c
  MySQLdb/_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\\cl.exe' failed with exit status 2
  ----------------------------------------
  ERROR: Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... error
    ERROR: Command errored out with exit status 1:
     command: 'C:\Program Files (x86)\Python\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Patrik\\AppData\\Local\\Temp\\pip-install-0vmm7fdx\\mysqlclient\\setup.py'"'"'; __file__='"'"'C:\\Users\\Patrik\\AppData\\Local\\Temp\\pip-install-0vmm7fdx\\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\Patrik\AppData\Local\Temp\pip-record-nk0ejlkz\install-record.txt' --single-version-externally-managed --compile
         cwd: C:\Users\Patrik\AppData\Local\Temp\pip-install-0vmm7fdx\mysqlclient\
    Complete output (30 lines):
    running install
    running build
    running build_py
    creating build
    creating 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\_exceptions.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
    running build_ext
    building 'MySQLdb._mysql' extension
    creating build\temp.win-amd64-3.7
    creating build\temp.win-amd64-3.7\Release
    creating build\temp.win-amd64-3.7\Release\MySQLdb
    C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(1,4,4,'final',0) -D__version__=1.4.4 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include\mariadb" "-IC:\Program Files (x86)\Python\include" "-IC:\Program Files (x86)\Python\include" /TcMySQLdb/_mysql.c /Fobuild\temp.win-amd64-3.7\Release\MySQLdb/_mysql.obj /Zl /D_CRT_SECURE_NO_WARNINGS
    _mysql.c
    MySQLdb/_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\\cl.exe' failed with exit status 2
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'C:\Program Files (x86)\Python\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Patrik\\AppData\\Local\\Temp\\pip-install-0vmm7fdx\\mysqlclient\\setup.py'"'"'; __file__='"'"'C:\\Users\\Patrik\\AppData\\Local\\Temp\\pip-install-0vmm7fdx\\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\Patrik\AppData\Local\Temp\pip-record-nk0ejlkz\install-record.txt' --single-version-externally-managed --compile Check the logs for full command output.

它应该可以正常工作,并且只需使用 pip 安装包

mysqlclient
,这样我就可以将它与 django 一起使用。

python mysql django windows cmd
5个回答
9
投票

当前版本 1.4.4 没有官方的轮子,当您尝试从源代码安装时会遇到错误。

由于您在 Windows 上使用 64 位 Python,因此您可以安装

1.4.2.post1
,因为该版本有官方的 Wheel。

python -m pip install mysqlclient==1.4.2.post1

或者,您可以从 https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient 下载非官方的轮子,其中包含适用于 Windows 上的 32 位和 64 位 Python 的最新版本的轮子。除非你有特殊原因需要使用 32 位 Python,否则我建议你使用 64 位。

例如,您可以下载

mysqlclient‑1.4.4‑cp37‑cp37m‑win_amd64.whl
(适用于 Windows 的 Python 3.7 64 位),然后执行以下操作:

python -m pip install mysqlclient‑1.4.4‑cp37‑cp37m‑win_amd64.whl

7
投票

我也遇到过类似的情况,我是这样做的:

sudo apt-get install python3-dev libmysqlclient-dev

之后,在虚拟环境中:

pip3 install mysqlclient

那是因为,我在安装时遇到了另一个问题:

pip3 install flask-mysqldb

2
投票

您可以使用

pymysql
代替
mysqlclient

安装pymysql

pip install pymysql

修改您的

init.py
文件

import pymysql

pymysql.install_as_MySQLdb()

您可以开始您的项目了


1
投票

FWIW:我在 macOS 上也遇到了这个问题,并出现错误:

ld: library not found for -lssl
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    error: command 'xcrun' failed with exit status 1

对我有用的解决方案(借自相关的post)是修复brew安装的openssl链接:

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

0
投票

对于使用 macOS 的用户,

$ brew install mysql pkg-config
$ pip install mysqlclient
© www.soinside.com 2019 - 2024. All rights reserved.