Python pip ‘mysql’ package installation problems on Python 3

31 Jan

As I do have a python 2.7 project that is using the ‘mysql’ pip package I recently tried running it on a Python 3 system. As I thought I encountered some problems. Sidenote: ‘mysql’ is just a virtual package and includes ‘mysql-Python’ which produces these problems as it is only available for python 2.

Shortcut solution: Use the ‘mysql-connector’ pip package instead.

Anyway while installing the ‘mysql’ pip package I got the following error:

error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

Which I fixed by installing the mentioned C++ compiler from Microsoft.As I was too greedy to save space on the hard drive I didn’t tick the ‘Windows 10 SDK’ which lead to:

fatal error C1083: Cannot open include file: 'basetsd.h': No such file or directory

But even after installing the Windows 10 SDK everything still resolves in linker errors:

build\lib.win32-3.6\_mysql.cp36-win32.pyd : fatal error LNK1120: 2 unresolved externals

As mentioned here the ‘mysql-Python’ package only runs on python 2. Bummer.
Therefore I exchanged and refactored the code to solely rely on ‘mysql-connector’ which runs on python 2 and 3. But as Murphys law dictates there is also a problem with that as it introduces a C++ dependency from version 2.1.6 onwards – which brings trouble. The simple solution is to force the system to use version 2.1.6…

pip install mysql-connector==2.1.6

So for any new projects or old ones you should choose different packages if you need access to a MySQL database from python. A good explanation of the different available packages can be found here.