50

I've spent hours trying to make Django work on my computer. The problem is that I can't install the mysql-python package. I'm running Windows 7 64bit. This is what I've tried:

  1. I have downloaded easy_install
  2. I have downloaded Cygwin64 to be able to run Linux commands (Win cmd was driving me crazy)
  3. I have typed in: easy_install mysql-python (gave me an error message saying it can't find vcvarsall.bat)
  4. I have downloaded Visual Studio 2010. However, I uninstalled it since I found out that I had some other version of it already (it didn't solve the problem)

EDIT: I discovered this: https://pypi.python.org/pypi/MySQL-python/1.2.5. Does this mean I can't run Django with python 3.3? And why bother to go through all this work if there is an .exe-file out there?

starball
  • 20,030
  • 7
  • 43
  • 238
Myone
  • 1,103
  • 2
  • 11
  • 24
  • install PIP http://www.pip-installer.org/en/latest/installing.html and use this to install packages. Also make sure that all the python files that you are running are added to your Path environment variable. That may be why it says that "vcvarsall.bat" is not found. You should be able to use MySQL / Django / Python 3.3 fine with no issues. They are all supported and work together. – Aaron Lelevier Jan 29 '14 at 19:18
  • 1
    Thanks Aron! I have now downloaded PIP. However, I get the exact same error message when typing: pip install mysql-python. What python files do you mean I need to add to my path? – Myone Jan 29 '14 at 19:41
  • Please see my answer below. That should get MySQL running for you. – Aaron Lelevier Jan 29 '14 at 20:50

11 Answers11

87

try running the following command:

pip install mysqlclient
Ortal Blumenfeld Lagziel
  • 2,415
  • 3
  • 23
  • 33
  • 9
    this is the easiest solution and worked out of the box. it should be the top answer. – user1052080 Oct 30 '15 at 16:02
  • If using PyCharm, you can install mysqlclient directly through the Project Interpreter window. – Vlad Schnakovszki Jan 29 '17 at 21:05
  • 1
    I'm pretty sure the OP's problem is that pip install mysqlclient was throwing a bunch of errors. I went through the same thing with VS 2015 and it took me like 5 hours to find the root cause, which was a variety of things, which included needing to update setuptools, update easyinstall, update pip, install wheel, and install c++2010 and change the path of my virtual env to a shorter length. then i found installying a lower version of mysqlclient works in a different env so it was crazy – Tim Feb 06 '17 at 17:09
  • This was the easiest answer for Anaconda Navigator 3.6 Python – Murtaza Kanchwala Aug 14 '17 at 07:00
  • Another possible solution for this under stackoverflow.com/a/51811349/1552410 – kaya Aug 12 '18 at 21:34
  • error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/ – vishwakarma09 Sep 16 '19 at 02:34
60

If you are trying to use mysqlclient on WINDOWS with this failure, try to install the lower version instead:

pip install mysqlclient==1.3.4
Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
  • Worked for me on windows 10 and python 3.4 Thanks – joker.8 Jan 29 '18 at 09:47
  • I get the error "Failed building wheel for mysqlclient" and answer of Laura Chesches worked for me. – Stefan Aug 10 '18 at 09:37
  • this fixed it for me. I needed mysqldb for QGIS plugin, which runs python 2.7 (support for older plugins) at the time of my install. (early 2018) – JoeCrash Jan 17 '19 at 07:08
  • 1
    FYI, this works b/c 1.3.4 is precompiled into a wheel, newer versions only have the source – Charles L. Jan 24 '19 at 23:06
  • thank you. this worked for me on python 2.7 in pycharm. no other solutions, like pip install mysql-python worked for me. – rakesht Feb 18 '21 at 16:40
15

if you use the site http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python , download the file:

  • mysqlclient‑1.3.6‑cp34‑none‑win32.whl or

  • mysqlclient‑1.3.6‑cp34‑none‑win_amd64.whl

depending on the version of python you have (these are for python 3.4) and the type of windows you have (x64 or x32)

extract this file into C:\Python34\Lib\site-packages and your project will work

Laura Chesches
  • 2,493
  • 1
  • 19
  • 15
14

There are windows installers for MySQLdb avaialable for both 32 and 64 bit, supporting Python from 2.6 to 3.4. Check here.

CFreitas
  • 1,647
  • 20
  • 29
  • I had almost the exact same situation as @Myone described. Manually downloading from this link solved it. No modifications to PYTHONPATH necessary. – WAF Dec 16 '14 at 14:38
12

You're going to want to add Python to your Path Environment Variable in this way. Go to:

  1. My Computer
  2. System Properties
  3. Advance System Settings
  4. Under the "Advanced" tab click the button that says "Environment Variables"
  5. Then under System Variables you are going to want to add / change the following variables: PYTHONPATH and Path. Here is a paste of what my variables look like:

PYTHONPATH

C:\Python27;C:\Python27\Lib\site-packages;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\Python27\Scripts

Path

C:\Program Files\MySQL\MySQL Utilities 1.3.5\;C:\Python27;C:\Python27\Lib\site-packages;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\Python27\Scripts

Your Path's might be different, so please adjust them, but this configuration works for me and you should be able to run MySQL after making these changes.

Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111
  • Thank you for your answer. I didn't have a system variable called PYTHONPATH, but I created one and added what you suggested. I did the same thing with the Path-variable. I have checked that all paths exist on my computer. They all do, except for this one: C:\Python33\Lib\lib-tk. Can't find it. However, I ignored that and restarted my computer. I then tried both "easy_install mysql-python" and "pip install mysql-python". They both generated the same error message about not finding vcvarsall.bat. – Myone Jan 30 '14 at 09:25
  • Oh... so now I downloaded another version of Visual Studio (2010 C++, I think I downloaded another version the other time) and the problem is solved! Well, a part of it. Now it gives me another error message though, that somehow seems related to vcvarsall.bat: File "C:\Python33\Lib\distutils\msvc9compiler.py", line 287, in query_vcvarsall raise ValueError(str(list(result.keys()))) ValueError: ['path'] – Myone Jan 30 '14 at 10:07
  • If it's a [Path] error then check your Path again. I don't use Visual Studio with MySQL / Django so I'm not sure if you need that. It might be an extra configuration that you don't need. Did you follow the install PIP instructions in the link? And pip was successfully installed? – Aaron Lelevier Jan 30 '14 at 15:57
  • Hm... yup, I followed the instructions for both pip and easy_install so that shouldn't be the problem. And if you google this problem everyone tells you to download VS. Now I have also tried to add the location of vcvarsall.bat in my path and that didn't help. I've also followed the instructions here: http://stackoverflow.com/questions/2667069/cannot-find-vcvarsall-bat-when-running-a-python-script and that didn't help either. Feels like I've tried everything... I'm not exactly a computer genius so this is very annoying. – Myone Jan 30 '14 at 17:39
  • Well, this is what my paths look like if you want to have a look: Path: (lots of uninteresting stuff...) + C:\Python33;C:\Python33\Scripts;C:\Program Files (x86)\MySQL\MySQL Utilities 1.3.6\;C:\cygwin64;C:\Python33\Lib\site-packages;C:\Python33\Lib;C:\Python33\DLLs;C:\Python33\Lib\lib-tk;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC PYTHONPATH: C:\Python33;C:\Python33\Lib\site-packages;C:\Python33\Lib;C:\Python33\DLLs;C:\Python33\Lib\lib-tk;C:\Python33\Scripts;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC – Myone Jan 30 '14 at 17:39
  • Are you set on using MySQL? I MySQL for my first few projects, but then changed to Postgres SQL because that's what most django devs use, and so far it's way easier. Never ran into any issues like this. – Aaron Lelevier Jan 30 '14 at 19:41
  • Oh, well no I really don't have any preferences so I'm gonna try PostgreSQL. Hope that's easier because this is making me crazy. Thanks :) – Myone Jan 31 '14 at 22:48
  • 2
    way to complicated, just do pip install mysqlclient – user1052080 Oct 30 '15 at 16:03
  • have you checked to see if you're exceeding the long path limitation? – Tim Feb 06 '17 at 17:07
  • Ran into the similar problems and tried many of the suggestions here, the easy solution was here: https://stackoverflow.com/questions/26866147/mysql-python-install-error-cannot-open-include-file-config-win-h – Techniquab Jul 18 '18 at 19:07
4

For folks using Python 3.0+ (which should be everyone now):

Unfortunately, MySQL-Python 1.2.5 does not support Python 3.0+ yet (which is kinda unreasonable IMHO, Python 3+ has been out for a while). Reference : https://pypi.python.org/pypi/MySQL-python/1.2.5

So, my workaround is to use Oracle's MySQL connector. In settings.py, change DATABASE's 'ENGINE' field to: 'ENGINE': 'mysql.connector.django',

More info could be found in the last paragraph of the first answer to this question: Setting Django up to use MySQL

Hope this helps!!

Community
  • 1
  • 1
Vicky Zhang
  • 489
  • 1
  • 6
  • 12
3

I have a slightly different setup, but think my solution will help you out.

I have a Windows 8 Machine, Python 2.7 installed and running my stuff through eclipse.

Some Background:

When I did an easy install it tries to install MySQL-python 1.2.5 which failed with an error: Unable to find vcvarsall.bat. I did an easy_install of pip and tried the pip install which also failed with a similar error. They both reference vcvarsall.bat which is something to do with visual studio, since I don't have visual studio on my machine, it left me looking for a different solution, which I share below.

The Solution:

  1. Reinstall python 2.7.8 from 2.7.8 from https://www.python.org/download this will add any missing registry settings, which is required by the next install.
  2. Install 1.2.4 from http://pypi.python.org/pypi/MySQL-python/1.2.4

After I did both of those installs I was able to query my MySQL db through eclipse.

James Oravec
  • 19,579
  • 27
  • 94
  • 160
1

MySqldb python install windows

MySQL-python 1.2.3 for Windows and Python 2.7, 32bit and 64bit versions

download python mysql-python from here

Ashish Gupta
  • 1,153
  • 12
  • 14
0

If you encounter the problem with missing MS VC 14 Build tools while trying pip install mysqlclient a possible solution for this may be https://stackoverflow.com/a/51811349/1552410

kaya
  • 1,626
  • 1
  • 21
  • 27
0

Just Download mysqlclient from here https://www.lfd.uci.edu/~gohlke/pythonlibs/ be careful while downloading the right version depending on your your python version installed. Then proceed with the import. It worked for me because in my case the error was telling to install Visual Studio C++ 14.0 something which wasted my time and occupied around 10GB of space in my C drive. So recommending installing mysqlclient using pip install mysqlclient

0

For phpmydamin you can use following step

  1. Go to python install path like

     cd C:\Users\Enamul\AppData\Local\Programs\Python\Python37-32\Scripts
    
  2. Run the command pip install PyMySQL

  3. In the python shell import library like import pymysql

  4. connection to databasbe

     db = pymysql.connect(host='localhost',user='root',passwd='yourpassword', database="bd")
    
  5. get cursor cursor = db.cursor()

  6. Create table like

    cursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")
    
Enamul Haque
  • 4,789
  • 1
  • 37
  • 50