75

I'm trying to install mysql-python in a virtualenv using pip on windows. At first, I was getting the same error reported here, but the answer there worked for me too. Now I'm getting this following error:

_mysql.c(34) : Fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory

If I symlink (Win7) to my regular (not the virtualenv's) python's site-packages/MySQLdb dir I get

Error loading MySQLdb module: No module named _mysql

I'm rather at a loss here. Any pointers?

Community
  • 1
  • 1
saturdayplace
  • 8,370
  • 8
  • 35
  • 39
  • Note: as od 01/2016 there is no MySQL driver for python 3.5.x see http://stackoverflow.com/questions/34456770/python-connector-for-django-1-9-and-python-3-5 So this question is valid only up to Python 3.4. You can still use Django 1.9 (latest stable version as of 01/2016) with it. – Tomas Tintera Jan 03 '16 at 10:07
  • How can this problem be solved with MySQL 8.0? – René Nyffenegger Oct 23 '19 at 08:48

22 Answers22

93

All I had to do was go over to oracle, and download the MySQL Connector C 6.0.2 (newer doesn't work!) and do the typical install.

https://downloads.mysql.com/archives/c-c/

Be sure to include all optional extras (Extra Binaries) via the custom install, without these it did not work for the win64.msi

Once that was done, I went into pycharms, and selected the MySQL-python>=1.2.4 package to install, and it worked great. No need to update any configuration or anything like that. This was the simplest version for me to work through.

starball
  • 20,030
  • 7
  • 43
  • 238
Kevin Mansel
  • 2,351
  • 1
  • 16
  • 15
  • 10
    +1 By far the simplest answer to this problem. A word of caution, select the appropriate architecture (x86/64bit) of the driver. – Karthic Raghupathi Sep 18 '13 at 02:26
  • Thanks Kevin! I knew there must have been an easier way! Worked great. – User Jun 19 '14 at 02:21
  • 3
    Dude, you are a life saver. I've been pulling on my hair for a while because of this error, I tried all other solutions which were more complicated. Thanks for providing such a simple solution. – turingcomplete Aug 12 '14 at 21:58
  • 9
    I had to install the connector for x64 and move the MySql folder from the 'Program Files' to the 'Program Files (x86)' directory since that's where the build script was looking for it – Charlie Dec 09 '14 at 22:34
  • 38
    After installing the MySQL Connector C 6.0.2, I tried "pip install MySQL-python". It still showed the same error. Then I copied the folder "MySQL Connector C 6.0.2" from "C:\Program Files\MySQL" to "C:\Program Files (x86)\MySQL" and it worked perfectly !! – Prasad DLV May 21 '15 at 12:32
  • 2
    Make sure you download the 32bit version of the connector. With the 32 bit connector, it will work with MySQL-python==1.2.5 – Eric Lee Sep 07 '16 at 17:51
  • 2
    Current version of MySQL-python (1.2.5) will not work with latest 6.1 connectors that MySQL installer will install. Make sure to install 6.0.2. connector. – JoKr Nov 08 '16 at 13:54
  • Thanks that worked, i did download x86 and was able to install – Gleb Sevruk Mar 16 '17 at 17:11
  • Quick comment to say that in November 2017, you still need version 6.0.2 to work with MySQL-python 1.2.5. And you still need to copy over from Program Files to Program Files (x86) if you choose the 64bit version. – Nicojo Nov 22 '17 at 16:20
25

The accepted solution no longer seems to work for newer versions of mysql-python. The installer no longer provides a site.cfg file to edit.

If you are installing mysql-python it'll look for C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include. If you have a 64-bit installation of MySQL, you can simply invoke:

  1. mklink /d "C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include" "C:\Program Files\MySQL\MySQL Connector C 6.0.2\include"
  2. Run pip install mysql-python
  3. Delete the symbolic link created in step 1
Gili
  • 86,244
  • 97
  • 390
  • 689
  • 10
    This is the right idea for a solution, however it didn't work for me. To solve the issue I copied the directory C:\Program Files\MySQL\MySQL Connector C 6.0.2\ to C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\. This solved the issue for me – Omer Nov 10 '14 at 09:49
  • symbolic link approach is good but it must be wider as there is more files required thus instead of @Gili symbolic link use this mklink /d "C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2" "C:\Program Files\MySQL\MySQL Connector C 6.0.2" – Zgr3doo May 20 '15 at 11:51
  • 2
    This works OK for include files but not for lib files. You can't just symlink the 32-bit lib folder to the 64-bit equivalent. It's probably better to simply install both the 32-bit and 64-bit SQL Connector packages independently, rather than symlink anything. – jarmod Nov 05 '15 at 22:05
  • 1
    This doesn't work at all. You need to download & install *only the 32 bits version*. Two hours lost in Windows. 3s install on Linux. What a shame. – Olivier Pons Dec 26 '15 at 11:53
  • Works on Windows 10. I had to create a symbolic link to `lib` folder in addition to `include` folder. – coder.in.me Sep 20 '16 at 14:56
  • Thank you my lord! This is the only option that worked for me after searching the whole damn internet for 14 hours. – आनंद Feb 24 '17 at 07:45
  • @Omer your suggestion installed it but when I run my code it still says no module named MySQLdb – Umair Ayub Apr 06 '18 at 17:26
18

Update for mysql 5.5 and config-win.h not visible issue

In 5.5 config-win. has actually moved to Connector separate folder in windows. i.e. smth like:

C:\Program Files\MySQL\Connector C 6.0.2\include

To overcome the problem one need not only to download "dev bits" (which actually connects the connector) but also to modify mysqldb install scripts to add the include folder. I've done a quick dirty fix as that.

site.cfg:

# Windows connector libs for MySQL.
connector = C:\Program Files\MySQL\Connector C 6.0.2

in setup_windows.py locate the line

include_dirs = [ os.path.join(mysql_root, r'include') ]:

and add:

include_dirs = [ os.path.join(options['connector'], r'include') ]

after it.

Ugly but works until mysqldb authors will change the behaviour.


Almost forgot to mention. In the same manner one needs to add similar additional entry for libs:

library_dirs = [ os.path.join(options['connector'], r'lib\opt') ]

i.e. your setup_windows.py looks pretty much like:

...
library_dirs = [ os.path.join(mysql_root, r'lib\opt') ]
library_dirs = [ os.path.join(options['connector'], r'lib\opt') ]
libraries = [ 'kernel32', 'advapi32', 'wsock32', client ]
include_dirs = [ os.path.join(mysql_root, r'include') ]
include_dirs = [ os.path.join(options['connector'], r'include') ]
extra_compile_args = [ '/Zl' ]
...
Bugagotti
  • 228
  • 2
  • 6
  • Thanks a bunch it didn't work for me till i've done what you listed here – Mohamed Faramawi Nov 26 '11 at 13:52
  • as of mysql-python 1.2.4, the default site.cfg has already been updated to reflect this comment. However, the default site.cfg is for 64 bit windows, so the path to the connector will be wrong on 32 bit. Remove (x86) and remove "MySQL" from "MySQL Connector C 6.0.2" – fastmultiplication Feb 10 '13 at 03:13
  • 1
    @fastmultiplication i don't have that folder, what should i install? – Nicola Peluchetti Apr 12 '13 at 12:48
  • 4
    What are "dev bits? Google doesn't even know what are these :( – Shailen Aug 03 '13 at 14:09
  • 1
    @shailenTJ - See chodorowicz's answer below about installing the MySQL developer build. – saturdayplace Aug 06 '13 at 16:35
  • If you've made it this far, you probably realize all this won't work. Besides no one caring to give the command for "dev bits", missing blog references, and the fact site.cfg and windows_setup.py probably doesn't exist in your default Python27 install, if you push past all this it still doesn't work. Ignore all above and go to kevin.mansel's answer below. – kmarsh Sep 30 '15 at 14:59
  • 1
    For your information `MySQL Connector C 6.1` **doesn't have** the required file. `MySQL Connector C 6.0.2` has it. So you have to choose a old version on the MySQL website... – Olivier Pons Dec 26 '15 at 11:05
  • Where does one find mysqldb install scripts when installing through pip in a virtualenv? – RatherBKnitting Apr 02 '17 at 20:51
15

The accepted answer is out of date. Some of the suggestions were already incorporated in the package, and I was still getting the error about missing config-win.h & mysqlclient.lib.

  • Install mysql-connector-c-6.0.2-win32.msi

    There's a zip file for the conenctor too but that didn't work because mysqlclient.lib is in lib directory whereas the installer expects it in lib/opt. Instead of hacking site.cfg or setup_windows.py, the msi does the job.

  • pip install mysql-python

P.S. Since I don't use MySQL anymore, my answer may be out of date as well.

user
  • 17,781
  • 20
  • 98
  • 124
  • Is this for version 3.3 as well? I did as you mentioned by downloading and installing the msi and then pip script, but I'm still getting the error in pip about config-win.h missing. – Ryan Dec 08 '13 at 20:10
  • It looks like in my case, I see in the installation error C:\program files (x86). I use Windows 32-bit which does not use "(x86)" in the path. This is I think why the error is happening for me. Only problem is that I don't know where the file is located in order to correct this path setting. – Ryan Dec 08 '13 at 20:25
  • 2
    Nice. I installed the x64 version but did not worked. Then I followed your advice and installed the x86 and voilà! I'm using Python 3.4.3 and runned `pip install mysql-python` and it works. ;-) – Matheus Santos May 14 '15 at 02:48
11

I know this post is super old, but it is still coming up as the top hit in google so I will add some more info to this issue.

I was having the same problems as OP but none of the suggested answers seemed to work for me. Mainly because "config-win.h" didn't exist anywhere in the connector install folder.

I was using the latest Connector C 6.1.6 as that was what was suggested by the MySQL installer.

This however doesn't seem to be supported by the latest MySQL-python package (1.2.5). When trying to install it I could see that it was explicitly looking for C Connector 6.0.2.

"-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include"

So by installing this version from https://dev.mysql.com/downloads/file/?id=378015 the python package installed without any problem.

jimmy
  • 1,981
  • 3
  • 19
  • 28
  • 1
    As of today (2017/7/2), this is the only option that worked for me on Win10 x64, with Python 2.7. I have tried the connector 6.1, but obviously it does not work. – BrandonSun Jul 02 '17 at 02:16
  • Thanks.this post worked for me. Python 2.7 mysql-python-1.2.5 – Abhishek L Jun 26 '19 at 09:50
7

Most probably the answer is to install MySQL Developer Build and selecting "C headers\libs" option during configuration. (as reported in this entry: Building MySQLdb for Python on Windows on rationalpie.wordpress.com)

Maybe even better solution is to install a precompiled build: http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe

chodorowicz
  • 1,062
  • 1
  • 15
  • 19
  • Can you install the binary into a virtualenv? When I tried, it only gave me the option of my system python installation. – rocketmonkeys Dec 16 '10 at 23:31
  • No, I haven't tried. Here you can find a whole set of precompiled MySQLdb packages for Windows - maybe one them will work for you http://www.codegood.com/archives/4 – chodorowicz Dec 23 '10 at 00:53
  • I havent been able to do this in a virtualenv (well buildout but yeah). I also tried with lower versions (1.2.1, 1.2.2) without any success. Its a dependency on an app of mine so i would really like to see a more robust solution. I submitted a bug report here https://sourceforge.net/tracker/?func=detail&aid=3153396&group_id=22307&atid=374932 – Dave Jan 08 '11 at 18:00
  • 2
    I used this exe file with easy_install under virtualenv and it works well under windows XP – laurent Aug 17 '12 at 10:10
  • @Rocketmonkeys Yes, you can install binary into a virtualenv - see [Can I install Python windows packages into virtualenvs?](http://stackoverflow.com/q/3271590/95735) – Piotr Dobrogost Nov 05 '12 at 19:53
  • @laurent, You Sir, deserve a medal. – Shailen Aug 13 '13 at 13:31
  • This requires python v2.6, so for anyone not using that, you're SOL – canadiancreed Jul 04 '15 at 04:37
5

If pip fails to install "MySQLdb", a workaround is to download and install it on your machine first from this link

http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

then copy all MySQL* and _mysql* files and directories from your system Python to your Virtualenv dir:

c:\Python27\Lib\site-packages (or similar path to your system Python) to c:\my_virtenv\Lib\site-packages (path to your virtualenv)

Dmitriy
  • 340
  • 2
  • 9
4

I had a lot of headache with MySQLdb too.

Why not use the official MysQL Python Connector?

easy_install mysql-connector-python

Or you can download it from here: http://dev.mysql.com/downloads/connector/python/

Documentation: http://dev.mysql.com/doc/refman/5.5/en/connector-python.html

4

Well, if you are still having the problem, you can download the installer from http://code.google.com/p/soemin/downloads/detail?name=MySQL-python-1.2.3.win32-py2.7.exe

Ethan
  • 4,915
  • 1
  • 28
  • 36
3

Installing dev bits for mysql got rid of the config-win.h error I was having, and threw another. Failed to load and parse the manifest. The system cannot find the file specified. I found the answer to my problem in this post: http://www.fuyun.org/2009/12/install-mysql-for-python-on-windows/.

I copied the file 'C:\Python26\Lib\distutils\msvc9compiler.py` into my virtualenv, made the edit suggested in the above link, and things are working fine.

saturdayplace
  • 8,370
  • 8
  • 35
  • 39
  • 1
    Quick question about "Installing dev bits for mysql got rid of the config-win.h error". What are "dev bits" and where can I get them? =) I'm having a similar issue. – John Mar 05 '10 at 18:30
  • So do I. I've done mySql 5.5 complete installation. fixed site.cfg. But still getting the error: _mysql.c(34) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory – Shrike Jan 21 '11 at 17:02
  • I'm reinstalling my whole python dev setup, and I'm stuck at this point. Where in the world can I find the "config-win.h" file??? – rocketmonkeys Jan 26 '11 at 02:14
  • This is covered in the link above: "Download MySQL msi installer version, select “Developer Components” in Custom Setup. These aren't installed by default." – saturdayplace Jan 26 '11 at 21:53
3

Simplest working solution:

Download the MySQL Connector C 6.0.2 from below link and Install.

http://dev.mysql.com/downloads/connector/c/6.0.html#downloads

After installing the MySQL Connector C 6.0.2, copy the folder "MySQL Connector C 6.0.2" from "C:\Program Files\MySQL" to "C:\Program Files (x86)\MySQL".

Then type

pip install MySQL-python

It will definitely work.

Gil Baggio
  • 13,019
  • 3
  • 48
  • 37
  • 1
    thanks man I had tried every thing and nothing worked but when I copied my 'MySQL Connector C' directory from program files into program files (x86) the problem solved you saved my day thank you again – Code_Worm Nov 25 '18 at 19:34
  • Happy to help you bro. Cheers! @Code_Worm – Gil Baggio Nov 26 '18 at 07:48
2

In my case, my fix was copying the folder created from mysql-connector-c-6.0.2-win32.msi (referenced from username buffer in a previous post), which is located at c:\Program Files\MySQL\MySQL Connector C 6.0.2 and creating a new path with Program Files (x86) and pasting the content there since the installation is not properly checking between 32-bit and 64-bit machines.

So, the new path is C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2. This is the path that the installer is looking to find, so I pasted the files there to help the installer to find the files, which was causing the error about missing config-win.h

Ryan
  • 3,475
  • 2
  • 16
  • 12
2

Steps for Window10:

  • Go to https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python
  • Download the right version according to python version and hardware specs: for my case, mysqlclient‑1.4.2‑cp37‑cp37m‑win32.whl works for python3.7 and Intel CPU.
  • While your env is still activated, go to the download folder and run pip install mysqlclient-1.4.2-cp37-cp37m-win32.whl
David P
  • 71
  • 3
  • Most voted answer didn't work for me (using Python 2.7), this one moved me along to the [next error](https://stackoverflow.com/q/14262379/1831722) which seems to stem from the fact i'm using MSYS Python which may be 64 bit instead of the required 32 bit. Yay. – vesperto Feb 22 '22 at 12:41
1

I did follow the answer from Bugagotti, And it does not work in my windows (Win7 64 bit, py27 and have mysql connector 6.1 installed) for mysql-python-1.2.5, so I made some even dirty changes inside mysql-python-1.2.5:

First, the site.cfg:

connector = C:\Program Files\MySQL\MySQL Connector C 6.1

Second, the _mysql.c :

#if defined(MS_WINDOWS)
#include <config-win.h>
#else
#include "my_config.h"
#endif

To:

#if 0 /*defined(MS_WINDOWS)*/
#include <config-win.h>
#else
#include "my_config.h"
#endif

And with these changes ,the config_win.h issue will gone, but there is still a link issue:

LINK : fatal error LNK1181: cannot open input file 'mysqlclient.lib'

For this, I changed the setup_windows.py:

library_dirs = [ os.path.join(connector, r'lib\vs9') ]  ## the original value was r'lib\opt'

Then it worked finally.

Mingcai SHEN
  • 117
  • 1
  • 8
1

I followed Mingcai SHEN's method.

But in my case, I changed the connector to

connector = C:\Program Files\MySQL\MySQL Connector.C 6.1

And the library_dirs is changed to

library_dirs = [ os.path.join(connector, r'lib\vs10') ]

because I don't have a vs9 directory. It works, but I don't know why.

I have vs2012 installed, and the lib directory of the connector only has vs10 and vs11, in which vs11 doesn't work. The VCForPyhton27.mis I installed seems to support vs9.

Anyway, this works. And if you want to risk it, you can try.

zijuexiansheng
  • 337
  • 3
  • 14
1

Solution that worked for me on Windows: Install both the 32-bit and 64-bit versions of the MySQL Connector/C 6.0.2. Open Command Prompt and run:

pip install mysql-python
raul
  • 1,209
  • 7
  • 20
  • 36
1

For me the following approach solved the issue (Python 3.5.2; mysqlclient 1.3.9):

  1. Dowload latest MySQL C Connector http://dev.mysql.com/downloads/connector/c/ (for me was Windows (x86, 64-bit), MSI Installer)
  2. Copy c:\Program Files\MySQL\MySQL Connector C 6.0.2\ directory to c:\Program Files (x86)\MySQL\MySQL Connector C 6.1\
  3. Run pip install mysqlclient
  4. [optional] delete c:\Program Files (x86)\MySQL\MySQL Connector C 6.1\

The issue here is only for x64 bit installation owners, since build script is trying to locate C connector includes in x86 program files directory.

Dagaz
  • 353
  • 4
  • 15
1
building '_mysql' extension
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\Release
C:\Users\TimHuang\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include" -Ic:\python27\include -Ic:\python27\PC /Tc_mysql.c /Fobuild\temp.win-amd64-2.7\Release\_mysql.obj /Zl
_mysql.c
_mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory

If you see this when you try pip install mysql-python, the easiest way is to copy

C:\Program Files\MySQL\MySQL Connector C 6.0.2 to C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2

I tried to create the symbolic link but Windows keeps throwing me C:\WINDOWS\system32>mklink /d "C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include" "C:\Program Files\MySQL\MySQL Connector C 6.0.2\include" The system cannot find the path specified.

timbuntu2
  • 111
  • 1
  • 2
1

Try ActivePython,

pypm -E C:\myvirtualenv install mysql-python
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
0

If you are doing this in a virtual environment whether using Visual Studio or otherwise, try easy_install MySQL-python

Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
0
  1. Download from this

  2. Then install this whl by pip installer. Shift + right click in Downloaded folder and choose Open powershell here. Then install using command: pip install package_name.whl

Jacky Pham
  • 481
  • 5
  • 9
0

Download the version of Connector from https://dev.mysql.com/downloads/connector/c/6.0.html For my case I had installed 64 bit of connector and my python was 32 bit. So I had to copy MySQL from program files to Program Files(86)

Ishwar Rimal
  • 1,071
  • 11
  • 19