简体   繁体   English

无法在 Windows 上安装 mysqlclient Python 包

[英]Unable to install mysqlclient Python package on Windows

I am trying to install the mysqlclient Python package ( https://pypi.python.org/pypi/mysqlclient ) into a virtual Python 2.7 environment on Windows 7 (on a local PC, and on the Appveyor CI) and cannot get it done.我正在尝试将mysqlclient Python 包( https://pypi.python.org/pypi/mysqlclient )安装到 Windows 7(在本地 PC 和 Appveyor CI 上)上的虚拟 Python 2.7 环境中,但无法完成.

I am describing the issues for the local PC with Win 7, but it happens basically the same way on Appveyor.我正在描述带有 Win 7 的本地 PC 的问题,但它在 Appveyor 上的发生方式基本相同。

Packages in my virtualenv when I start:当我开始时,我的 virtualenv 中的包:

pip (9.0.1)
setuptools (36.6.0)
wheel (0.30.0)

pip install mysqlclient initially says: pip install mysqlclient最初说:

. . .
running build_ext
building '_mysql' extension
error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27

Apparently, Python 2.7 requires VC9.显然,Python 2.7 需要 VC9。 After installing it, pip install mysqlclient gets a little further, but misses include files:安装后, pip install mysqlclient会更进一步,但错过了包含文件:

. . .
building '_mysql' extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
C:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include" -IC:\Python27\include -IC:\Users\Andi\virtualenvs\mysqlclient\PC /Tc_mysql.c /Fobuild\temp.win-amd64-2.7\Release\_mysql.obj /Zl
_mysql.c
_mysql.c(29) : fatal error C1083: Cannot open include file: 'mysql.h': No such file or directory
error: command 'C:\\Program Files (x86)\\Common Files\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\cl.exe' failed with exit status 2

In order to get the header files, I installed the "MySQL Connector/C 6.1.10" from https://downloads.mysql.com/archives/get/file/mysql-connector-c-6.1.10-winx64.msi .为了获取头文件,我从https://downloads.mysql.com/archives/get/file/mysql-connector-c-6.1.10-winx64.msi安装了“MySQL Connector/C 6.1.10” . This is admittedly only the second latest version, but I need to install it on Appveyor from the command line, and did not find out how to download the latest version in an unattended manner.诚然,这只是第二个最新版本,但我需要从命令行将其安装在 Appveyor 上,并且没有找到如何以无人值守的方式下载最新版本。

Also, using the MSI installer installs the package in C:\\Program Files\\MySQL\\MySQL Connector C 6.1 but the build step in pip install expects it in C:\\Program Files (x86)\\MySQL\\MySQL Connector C 6.1 , so I softlinked it:此外,使用 MSI 安装程序将软件包安装在C:\\Program Files\\MySQL\\MySQL Connector C 6.1但 pip install 中的构建步骤期望它在C:\\Program Files (x86)\\MySQL\\MySQL Connector C 6.1 ,所以我软链接它:

mklink /D "C:\Program Files (x86)\MySQL\MySQL Connector C 6.1" "C:\Program Files\MySQL\MySQL Connector C 6.1"

When looking at the files in that version of the MySQL Connector/C, it has only lib/vs12 and lib/vs14 directories:查看该版本的 MySQL Connector/C 中的文件时,它只有 lib/vs12 和 lib/vs14 目录:

lib/vs12/mysqlclient.lib
lib/vs14/mysqlclient.lib

As expected, pip install mysqlclient now gets beyond the compiles, but fails when linking:正如预期的那样, pip install mysqlclient现在超出​​了编译范围,但在链接时失败:

. . .
running build_ext
building '_mysql' extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
C:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include" -IC:\Python27\include -IC:\Users\Andi\virtualenvs\mysqlclient\PC /Tc_mysql.c /Fobuild\temp.win-amd64-2.7\Release\_mysql.obj /Zl
_mysql.c
_mysql.c(242) : .... several warnings ...
C:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\link.exe /DLL /nologo /INCREMENTAL:NO "/LIBPATH:C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib\vs9" /LIBPATH:C:\Python27\Libs /LIBPATH:C:\Users\Andi\virtualenvs\mysqlclient\libs /LIBPATH:C:\Users\Andi\virtualenvs\mysqlclient\PCbuild\amd64 /LIBPATH:C:\Users\Andi\virtualenvs\mysqlclient\PC\VS9.0\amd64 kernel32.lib advapi32.lib wsock32.lib mysqlclient.lib /EXPORT:init_mysql build\temp.win-amd64-2.7\Release\_mysql.obj /OUT:build\lib.win-amd64-2.7\_mysql.pyd /IMPLIB:build\temp.win-amd64-2.7\Release\_mysql.lib /MANIFESTFILE:build\temp.win-amd64-2.7\Release\_mysql.pyd.manifest /MANIFEST
LINK : fatal error LNK1181: cannot open input file 'mysqlclient.lib'
error: command 'C:\\Program Files (x86)\\Common Files\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\link.exe' failed with exit status 1181

I did not find a version of the "MySQL Connector/C" package that can still be downloaded and that works with VC9.我没有找到仍可下载且适用于 VC9 的“MySQL Connector/C”软件包版本。 Using the mysqlclient.lib files in the vs12 or vs14 directories in this vc9 build results in unresolved symbols.在此 vc9 构建中使用 vs12 或 vs14 目录中的 mysqlclient.lib 文件会导致无法解析的符号。

My questions are:我的问题是:

  • What package(s) do I need to use in order to get the right header files (eg mysql.h ) and libraries (eg mysqlclient.lib ) for a successful pip install mysqlclient on Windows 7?我需要使用什么包才能获得正确的头文件(例如mysql.h )和库(例如mysqlclient.lib )以在 Windows 7 上成功pip install mysqlclient
  • More generally, what are the prerequisites for a successful pip install mysqlclient on Windows 7?更一般地说,在 Windows 7 上成功pip install mysqlclient的先决条件是什么?

download mysqlclient from http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-pythonhttp://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python下载 mysqlclient

choose the version matching your python version, example mysqlclient‑1.4.2‑cp37‑cp37m‑win_amd64.whl, and then:选择与您的 python 版本匹配的版本,例如 mysqlclient‑1.4.2‑cp37‑cp37m‑win_amd64.whl,然后:

pip install wheel

pip install mysqlclient‑1.4.2‑cp37‑cp37m‑win_amd64.whl

Incidentally I ran into the same issue in July last year and only now finished an entry on my blog about it.顺便说一句,我在去年 7 月遇到了同样的问题,直到现在才在我的博客上完成了一篇关于它的条目

Basically, you're not going to like it, but you need to re-compile mysqlclient from scratch, because -- as you pointed out -- it's not available for Visual Studio 9 since a long time.基本上,您不会喜欢它,但您需要从头开始重新编译 mysqlclient,因为 - 正如您所指出的 - 它在很长一段时间内不适用于 Visual Studio 9。

However, doing it right is somewhat tricky, because you need to fix some compilation issues that apparently aren't documented anywhere in Oracle's official documentation.然而,正确地完成它有点棘手,因为您需要修复一些显然没有在 Oracle 官方文档中记录的编译问题。 There are also issues with the Python setup.py file, as it doesn't care about the platform architecture and always looks in the x86 directory. Python setup.py 文件也存在问题,因为它不关心平台架构并且总是在 x86 目录中查找。 That's why you soft-linked the directory.这就是您对目录进行软链接的原因。 There is an open issue on Github mentioning this. Github 上有一个未解决的问题提到了这一点。

So basically, using the VC++ for Python 2.7 you already installed:所以基本上,使用你已经安装的 VC++ for Python 2.7:

  1. Download the Oracle MySQL C Connector source code.下载 Oracle MySQL C 连接器源代码。
  2. Download and install Microsoft Visual C++ Compiler for Python 2.7.下载并安装适用于 Python 2.7 的 Microsoft Visual C++ 编译器。
  3. Use CMake to generate NMake Makefiles for the MySQL C Connector source code.使用 CMake 为 MySQL C 连接器源代码生成NMake Makefiles Pay attention to environment variables, see this Github issue comment .注意环境变量,见这个 Github 问题评论
  4. Compile the MySQL C Connector mysqlclient library using nmake mysqlclient .使用nmake mysqlclient编译 MySQL C 连接器 mysqlclient 库。
  5. Install the MySQL C Connector binary and add the compiled mysqlclient.安装 MySQL C 连接器二进制文件并添加编译后的 mysqlclient。
  6. Run pip install mysqlclient again.再次运行 pip install mysqlclient 。

Step 4 is tricky, because Microsoft don't supply C99 stdint.h, inttypes.h and stdlib.h is missing lldiv_t.第 4 步很棘手,因为 Microsoft 不提供 C99 stdint.h,inttypes.h 和 stdlib.h 缺少 lldiv_t。 You can use this StackOverflow post to get stdint.h and inttypes.h and then manually define lldiv_t in decimal.h as:您可以使用此 StackOverflow帖子获取 stdint.h 和 inttypes.h,然后在 decimal.h 中手动定义 lldiv_t 为:

typedef struct {
long long quot;
long long rem;
} lldiv_t;

You can also copy these headers from a newer Visual Studio, like 2013 and 2015. After that, as you had already figured out, you need to put the compiled mysqlclient.lib into C:\\Program Files (x86)\\MySQL\\MySQL Connector C 6.1\\lib\\vs9.您还可以从较新的 Visual Studio(例如 2013 和 2015)复制这些标头。之后,正如您已经发现的,您需要将编译后的 mysqlclient.lib 放入 C:\\Program Files (x86)\\MySQL\\MySQL Connector C 6.1\\lib\\vs9.

Then issue another pip install mysqlclient and you're all done.然后发出另一个pip install mysqlclient就大功告成了。

使用来自https://www.lfd.uci.edu/~gohlke/pythonlibs/ 的mysqlclient‑1.4.6‑cp38‑cp38‑win32.whl for python 3.8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM