pip install mysqlclient returns "fatal error C1083: Cannot open file: 'mysql.h': No such file or directory

PythonMysqlSqlPip

Python Problem Overview


Here is this issue: I attempt to install mysqlclient like so

C:\Users\amccommon349>pip install mysqlclient
Collecting mysqlclient
  Using cached https://files.pythonhosted.org/packages/ec/fd/83329b9d3e14f7344d1
cb31f128e6dbba70c5975c9e57896815dbb1988ad/mysqlclient-1.3.13.tar.gz
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... error
    Complete output from command c:\users\amccommon349\appdata\local\programs\python\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\AMCCOM~1\\AppData\\Local\\Temp\\pip-install-qcgo48hf\\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\AMCCOM~1\AppData\Local\Temp\pip-record-q4yoftj8\install-record.txt --single-version-externally-managed --compile:

c:\users\amccommon349\appdata\local\programs\python\python36\lib\distutils\dist.py:261: UserWarning: Unknown distribution option: 'long_description_content_type'
warnings.warn(msg)
running install
running build
running build_py
creating build
creating build\lib.win-amd64-3.6
copying _mysql_exceptions.py -> build\lib.win-amd64-3.6
creating build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\__init__.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\compat.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\connections.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\converters.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\cursors.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\release.py -> build\lib.win-amd64-3.6\MySQLdb
copying MySQLdb\times.py -> build\lib.win-amd64-3.6\MySQLdb
creating build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\CR.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\ER.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.6\MySQLdb\constants
copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-3.6\MySQLdb\constants
running build_ext
building '_mysql' extension
creating build\temp.win-amd64-3.6
creating build\temp.win-amd64-3.6\Release
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.14.26428\bin\HostX86\x64\cl.exe /c/nologo/Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(1,3,13,'final',0) -D__version__=1.3.13 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include" -Ic:\users\amccommon349\appdata\local\programs\python\python36\include -Ic:\users\amccommon349\appdata\local\programs\python\python36\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.14.26428\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\um" "-IC:\ProgramFiles (x86)\Windows Kits\10\include\10.0.17134.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\cppwinrt" /Tc_mysql.c /Fobuild\temp.win-amd64-3.6\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)\\Microsoft Visual Studio\\2017\\BuildTools\\VC\\Tools\\MSVC\\14.14.26428\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2

I made sure I had all of the files needed from visual studios build tools, I downloaded the mysql-python connector, and updated my pip and setup tools. I am a complete beginner to this and would appreciate any input as to how to go about fixing this error.

Python Solutions


Solution 1 - Python

You can download unofficial windows binaries for your python version using https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient. Then install it using pip. This way you'll be able to avoid the hassle of dealing with visual studio build tools.

Just download the mysqlclient.whl file most applicable to you. I think in your case it'll be

> mysqlclient‑1.3.13‑cp36‑cp36m‑win_amd64.whl

and run

pip install "path to the downloaded .whl file"

Solution 2 - Python

This error occurs when trying to install mysqlclient for python32 in 64 bit environments. Uninstall python and re-install 64 bit version. Then pip install mysqlclient will run with no errors.

Solution 3 - Python

Well probably,this is one of the stupidest errors i have ever witnessed.
I have Python 3.7.3 32bit along with latest pip3 and i was installing wheels to my Python....and i stumbled on this STUPID error....
Probably my situation is a bit different but you can resolve it just by looking carefully at the error.... at first it seems something is missing and you start blaming you, Windows ,32/64bit and the list goes on and on..
Take a step back...just take a step back and just read the nasty red error message.... read it really well.
In my case it was

ERROR: Command errored out with exit status 1:
     command: 'c:\users\myUserName\appdata\local\programs\python\python37-32\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\MASTER~1\\AppData\\Local\\Temp\\pip-install-z7x81g2q\\mysqlclient\\setup.py'"'"'; __file__='"'"'C:\\Users\\MASTER~1\\AppData\\Local\\Temp\\pip-install-z7x81g2q\\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\MASTER~1\AppData\Local\Temp\pip-record-z1mvci5v\install-record.txt' --single-version-externally-managed --compile
         cwd: C:\Users\MASTER~1\AppData\Local\Temp\pip-install-z7x81g2q\mysqlclient\
    Complete output (30 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win32-3.7
    creating build\lib.win32-3.7\MySQLdb
    copying MySQLdb\__init__.py -> build\lib.win32-3.7\MySQLdb
    copying MySQLdb\_exceptions.py -> build\lib.win32-3.7\MySQLdb
    copying MySQLdb\compat.py -> build\lib.win32-3.7\MySQLdb
    copying MySQLdb\connections.py -> build\lib.win32-3.7\MySQLdb
    copying MySQLdb\converters.py -> build\lib.win32-3.7\MySQLdb
    copying MySQLdb\cursors.py -> build\lib.win32-3.7\MySQLdb
    copying MySQLdb\release.py -> build\lib.win32-3.7\MySQLdb
    copying MySQLdb\times.py -> build\lib.win32-3.7\MySQLdb
    creating build\lib.win32-3.7\MySQLdb\constants
    copying MySQLdb\constants\__init__.py -> build\lib.win32-3.7\MySQLdb\constants
    copying MySQLdb\constants\CLIENT.py -> build\lib.win32-3.7\MySQLdb\constants
    copying MySQLdb\constants\CR.py -> build\lib.win32-3.7\MySQLdb\constants
    copying MySQLdb\constants\ER.py -> build\lib.win32-3.7\MySQLdb\constants
    copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-3.7\MySQLdb\constants
    copying MySQLdb\constants\FLAG.py -> build\lib.win32-3.7\MySQLdb\constants
    running build_ext
    building 'MySQLdb._mysql' extension
    creating build\temp.win32-3.7
    creating build\temp.win32-3.7\Release
    creating build\temp.win32-3.7\Release\MySQLdb
    C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.21.27702\bin\HostX86\x86\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MT -Dversion_info=(1,4,2,'post',1) -D__version__=1.4.2.post1 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include\mariadb" -Ic:\users\MyUserName\appdata\local\programs\python\python37-32\include -Ic:\users\MyUserName\appdata\local\programs\python\python37-32\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.21.27702\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt" /TcMySQLdb/_mysql.c /Fobuild\temp.win32-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\\2019\\BuildTools\\VC\\Tools\\MSVC\\14.21.27702\\bin\\HostX86\\x86\\cl.exe' failed with exit status 2
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\MyUserName\appdata\local\programs\python\python37-32\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\MASTER~1\\AppData\\Local\\Temp\\pip-install-z7x81g2q\\mysqlclient\\setup.py'"'"'; __file__='"'"'C:\\Users\\MASTER~1\\AppData\\Local\\Temp\\pip-install-z7x81g2q\\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\MASTER~1\AppData\Local\Temp\pip-record-z1mvci5v\install-record.txt' --single-version-externally-managed --compile Check the logs for full command output.

Have you read it well ?...i assume that you have everything ... the Build tools, the MySQL Connector C 6.1...you even moved it to Program Files (x86)...so what it wrong...?? ...
Now take a closer look

C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.21.27702\bin\HostX86\x86\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MT -Dversion_info=(1,4,2,'post',1) -D__version__=1.4.2.post1 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include\mariadb" -

I am pretty sure you saw it...STILL NO ?.... let me bold it for you

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

mariadb
Well it can't be any stupidest than this ...the installer looks for mariadb includes and later libs Just go to the mariadb site and download the MariaDB C/C++ connector and install it...
Well its not over.. Goto to C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include and create folder mariadb...and copy paste the contents from the mariaDb connector installation include folder
again don't go way...go to C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib and do again the same drill for the lib folder
so you should have

C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include\mariadb  

C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib\mariadb  

Now hit the pip3 install mysqlclient
and enjoy installing without a nag...the end

Successfully installed mysqlclient-1.4.2.post1

Solution 4 - Python

This is what solved my issue

  • Install MySQL Connector/C (Archived Versions) from here

It is probably installed in the following location C:\Program Files\MySQL\MySQL Connector C 6.1


  • Copy the entire folder MySQL Connector C 6.1 to C:\Program Files (x86)\MySQL\MySQL Connector C 6.1

  • Try executing the command pip3 install mysqlclient.

Successful? Good Job!


Unsuccessful?


  • Install MariaDB connector from here

Probably installed in the location

C:\Program Files\MariaDB\MariaDB Connector C 64-bit

  • Copy the contents from

C:\Program Files\MariaDB\MariaDB Connector C 64-bit\include

and Paste into a folder. Name it as mariadb and paste it to the location

C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include\


  • Copy the contents from

    C:\Program Files\MariaDB\MariaDB Connector C 64-bit\lib

and Paste into a folder name it as mariadb and paste it to the location

`C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib`

  • Try executing the command pip3 install mysqlclient.

This worked for me

Solution 5 - Python

I had this issue (exactly same error) and tried all the solutions mentioned above, and neither of them worked (at least for me), actually I was about to pull my hairs over this issue till I found the solution in a very weird, simple and stupid way!! .

I did the following steps so that the problem solved and life got sweet again.

First I installed MySQL Connector/C from here

Second (after installation finished) I copied MySQL Connector C 6.1 folder from Program files/MySQL into Program files (x86)/MySQL

note that you might have to create MySQL folder if it doesn't exist in Program files (x86) (it didn't exist for me)

As simple as that, it worked for me, it seems for some (or all) 64-bit windows, pip or more probably mysqlclient setup script only searches Program files (x86) folder for some MySQL files (such as 'mysql.h') and doesn't search Program files and since doesn't find anything in Program files (x86) raises the error very dumb!!! isn't it

Solution 6 - Python

This is a problem with the 64 bit version of python on Windows 10. The following repaired the problem for me.

  1. Install the 32 bit version of python for windows (currently version 3.7.1)
  2. Install visual studio build tools https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2017 for C++
  3. Install the 32 bit mysql connector version 6.1.11 from https://downloads.mysql.com/archives/c-c/

After this I was able to install mysqlclient with pip install mysqlclient.

Hopefully someone will come up with a better solution that doesn't require downgrading to 32 bit because this is not a good solution, it just works. I found a way to work around the mysql issue but ran into the problem again while trying to install channels. It seems python 64 bit is looking for 32 bit libraries instead of 64 bit libraries on windows. So far this solution has solved all of my python build problems on windows.

Solution 7 - Python

None of the solutions above worked for me. Apparently I had to downgrade my Python from 3.7.x to 3.6.4 and that solved the problem for me. Downloadlink for python=3.6.4.

I would recommend to create a new virtual environment for this and not to uninstall your main python. Find extensive documentation on how to create a venv here.

Solution 8 - Python

I was trying the same for a Django project. I tried using:
pip install mysql-python
which gave me the following error: 'mysql.h': No such file or directory.
After fiddling with requirements.txt which gave me yet another error

  Downloading https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip (108kB) 
    Complete output from command python setup.py egg_info: 
    Traceback (most recent call last): 
      File "<string>", line 1, in <module> 
      File "/tmp/pip-install-43_6cfva/MySQL-python/setup.py", line 13, in <module> 
        from setup_posix import get_config 
      File "/tmp/pip-install-43_6cfva/MySQL-python/setup_posix.py", line 2, in <module> 
        from ConfigParser import SafeConfigParser 
    ModuleNotFoundError: No module named 'ConfigParser' 

Finally using this worked for me: pip install mysql-connector
import mysql.connector

Solution 9 - Python

In order to solve it, I've gone to this page

enter image description here

and downloaded mysqlclient‑1.4.6‑pp373‑pypy36_pp73‑win32.whl.

Then, placed it in a particular folder, opened the terminal in it, activated the virtual environment venv and ran

pip install mysqlclient-1.4.6-cp39-cp39-win_amd64.whl

enter image description here

Solution 10 - Python

sometimes the error is of interpreter too, it was in my case. I was using 3.8 python and had installed it in D:/, so that's and was not installed properly.

So I uninstalled it and installed it again correctly and it worked.

Solution 11 - Python

Wasted a lot of time to solve this error i found the solution that worked for me at least.

When i tried to install MYSQL Client

pip install mysqlclient

1st error

> MySQLdb/_mysql.c(29): fatal error C1083: Cannot open include file: 'mysql.h': No such file or directory


And also tried to install Django Channels

pip install -U channels

2nd error

> error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/

Download Build tools : Microsoft Build Tools

Stack overflow : How to install/select required Build Tools

Installed build tools. (Happy Coding)


@Ralph Ritoch solution worked for me and as well others those getting Mysql Client error.

But i also installed Django Channels so i followed these steps mentioned below

Follow these steps

  1. Windows 10 64 bit
  2. Python 64 bit 3.7.3
  3. mysql-connector-c-6.1.11-winx64
  4. Build tools

I also installed Django Channels 2.2.0 and not getting any kind of error.

Successfully installed mysqlclient


(Note)

MySQL connector already installed into x86 directory

> Copy c:\Program Files\MySQL\MySQL Connector C 6.0.2\ directory to c:\Program Files (x86)\MySQL\MySQL Connector C 6.1\

So i did not changed the directory of MySQL Connector. (You can also check this)

Maybe work for you!

Other Stack solution to change dirctory


Possible Reasons

Why we getting errors.

  1. Python 32/64 bit Version
  2. MySQL Connector 32/64 bit version
  3. Directory issue
  4. Window 32/64 bit version
  5. Build Tools not installed (If getting Microsoft Visual C++ 14.0 is required)

Thanks to all who posted their solution.

Solution 12 - Python

I wanted to comment on John's answer but my comment was too long so I had to make another answer. Sorry for that.

John, this helped me a lot, thanks. Not properly reading the logs is a bad habit for many people, myself included.

But instead of downloading the mariadb connector (or searching the net for a prebuilt wheel), I managed to do it with oracle's mysql connector. And I prefer to do symbolic links instead of copying files. Here's what I did using windows 10 x64, python 3.8 x64 and oracle's mysql connector x64:

# run this on an elevated command prompt if your user does not have permission to make symbolic links
# download and install https://downloads.mysql.com/archives/c-c/
mklink /d "C:\Program Files\MySQL\MySQL Connector C 6.1\include\mariadb" "C:\Program Files\MySQL\MySQL Connector C 6.1\include\mysql"
mklink /d "C:\Program Files (x86)\MySQL" "C:\Program Files\MySQL"
mklink /d "C:\Program Files\MySQL\MySQL Connector C 6.1\lib\mariadb" "C:\Program Files\MySQL\MySQL Connector C 6.1\lib\vs14"
mklink "C:\Program Files\MySQL\MySQL Connector C 6.1\lib\vs14\mariadbclient.lib" "C:\Program Files\MySQL\MySQL Connector C 6.1\lib\vs14\mysqlclient.lib"
SET INCLUDE=C:\Program Files\MySQL\MySQL Connector C 6.1\include;%INCLUDE%
python3 -m pip install mysqlclient

And that's it.

Solution 13 - Python

Thanks for all the answers, but it was not sufficient to solve my problem. However it put me on the right path :). To solve the error of the missing sys/types.h I had to copy this types.h file from "c:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt\sys" to "c:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include\mariadb\sys". Then I could finally install mysqlclient.

Solution 14 - Python

for me, pip install mysqlclient was throwing error...

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\\2019\\Community\\VC\\Tools\\MSVC\\14.20.27508\\bin\\HostX86\\x86\\cl.exe' failed with exit status 2

It seems mysqlclient is 64 bit. So I Forced both Python and pip to execute from the 64 bit directory.

Explicitly...

don't run python -m venv myvemv and pip install -r requirements.txt,

run the following...

C:\Users\mr\AppData\Local\Programs\Python\Python37\python -m venv [path to your virtual env goes here]

and

C:\Users\mr\AppData\Local\Programs\Python\Python37\Scripts\pip install -r requirements.txt

Solution 15 - Python

There are other ways to install mysqlclient into your system, I was not able to install it directly through command prompt it showed:

Fatal error in launcher: Unable to create process

tried couple of other alternatives and got a solution:

  • Click on start
  • Search for python and click on open file location click on open file location
  • 3.Click on python(version e.g=2.7,3.8,3.9...etc) right-click and open file location
  • 4.Click on scripts
  • 5.Got to path on the top of the file explorer and type cmd
    1. After command prompt opens type pip install mysqlclient
  • 7.Installation starts and now you have mysqlclient in your system

Solution 16 - Python

We can do so many things but if we miss to do python 64 bit in the 64 bit environment, the setup will not be completed.

So, we must confirm python 64 bit if our windows is 64 bit.

To see that you are in 64 bit or 32 bit, please run python command in the command prompt and see the result as like below:

To check 32 or 64 bit of python

Solution 17 - Python

In Command Line, Just type:

python --

instead of:

python --version

(This should show more details about the version including whether it is 32 or 64 bit)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAaron McCommonView Question on Stackoverflow
Solution 1 - PythonNipun SampathView Answer on Stackoverflow
Solution 2 - PythonΣωτήρης ΤσακιρίδηςView Answer on Stackoverflow
Solution 3 - PythonJohnView Answer on Stackoverflow
Solution 4 - PythonSohailAQView Answer on Stackoverflow
Solution 5 - PythonCode_WormView Answer on Stackoverflow
Solution 6 - PythonRalph RitochView Answer on Stackoverflow
Solution 7 - PythonErfanView Answer on Stackoverflow
Solution 8 - PythonAmal Matty AntonyView Answer on Stackoverflow
Solution 9 - PythonTiago Martins PeresView Answer on Stackoverflow
Solution 10 - Python56 Tejas PowarView Answer on Stackoverflow
Solution 11 - PythonMuhammad Faizan FareedView Answer on Stackoverflow
Solution 12 - PythongnumaruView Answer on Stackoverflow
Solution 13 - Pythonnerd01View Answer on Stackoverflow
Solution 14 - PythonmrivasView Answer on Stackoverflow
Solution 15 - PythonVaishnavi TyraView Answer on Stackoverflow
Solution 16 - PythonMd. Majharul HaqueView Answer on Stackoverflow
Solution 17 - PythonAhmedakhtar11View Answer on Stackoverflow