0

I tried conda install -c anaconda mysql-connector-python from here but after installing it I tried to run import MySQLdb as db buy it give me error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-5-08dd46cbf704> in <module>
      1 ## package used to connect the database
----> 2 import MySQLdb as db
      3 import pandas as pd
      4 import numpy as np
      5 import warnings

ModuleNotFoundError: No module named 'MySQLdb'

How I can install MySQLdb for Anaconda?

Shadow
  • 33,525
  • 10
  • 51
  • 64
vasili111
  • 6,032
  • 10
  • 50
  • 80

2 Answers2

5

For MySQLdb, you have to install mysql-python for Python 2:

conda install -c anaconda mysql-python

mysqlclient is the recommended library to use moving forward for Python 2 and 3:

conda install -c anaconda mysqlclient

Here's what Django recommends for mysql libraries https://docs.djangoproject.com/en/3.0/ref/databases/#mysql-db-api-drivers

Thierry Lam
  • 45,304
  • 42
  • 117
  • 144
  • How that command is different from `conda install mysqlclient`? I just did it and it installed MySQLdb – vasili111 Jan 28 '20 at 20:58
  • `mysqlclient` should be good too but `mysql-connector-python` as specified in your question won't give you `MySQLdb`. – Thierry Lam Jan 28 '20 at 21:04
  • So for Python 3 best option is `mysqlclient`? Also what is the difference between `conda install -c mysqlclient` and `conda install mysqlclient` and which one is preferable? – vasili111 Jan 28 '20 at 21:18
  • Why u change from `conda` to `anaconda` in install command? Why it is better? – vasili111 Jan 28 '20 at 21:25
  • `-c` is used to install a package from a specific channel. For your case, `anaconda` is the name of the channel. You can still install a package without specifying the channel. See documentation at https://docs.conda.io/projects/conda/en/latest/commands/install.html – Thierry Lam Jan 28 '20 at 21:25
0

As per mysqldb's pypi page, you need to install mysqldb as

pip install MySQL-python

In summary: you installed the wrong python library and the correct way is:

conda install -c anaconda mysql-python
Shadow
  • 33,525
  • 10
  • 51
  • 64