簡體   English   中英

Python pip 安裝mysqlclient

[英]Python pip install mysqlclient

所以我第一次下載了 Python 32 位,然后我做了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 下載了輪子 package,正如其他一些 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.

它應該可以正常工作,package mysqlclient只需使用 pip 安裝,這樣我就可以將它與 django 一起使用。

當前版本 1.4.4 沒有官方輪子,當您嘗試從源代碼安裝時會遇到錯誤。

由於您在 Windows 上使用 64 位 Python,您可以安裝1.4.2.post1 ,因為該版本有一個官方輪子。

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

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

一個對我有用的解決方案(從這篇相關帖子中借用)是修復 brew 安裝的 openssl 鏈接:

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

我有類似的情況,我這樣做了:

sudo apt-get install python3-dev libmysqlclient-dev

之后,在虛擬環境中:

pip3 install mysqlclient

那是因為,我在安裝時遇到了另一個問題:

pip3 install flask-mysqldb

您可以使用pymysql而不是mysqlclient

安裝pymysql

pip install pymysql

修改你的init.py文件

import pymysql

pymysql.install_as_MySQLdb()

你可以開始你的項目

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM