ImportError: No module named MySQLdb

264,451

Solution 1

If you're having issues compiling the binary extension, or on a platform where you cant, you can try using the pure python PyMySQL bindings.

Simply pip install pymysql and switch your SQLAlchemy URI to start like this:

SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://.....'

There are some other drivers you could also try.

Solution 2

Or try this:

apt-get install python-mysqldb

Solution 3

may you try

pip install mysqlclient

Solution 4

My issue is :

return __import__('MySQLdb')
ImportError: No module named MySQLdb

and my resolution :

pip install MySQL-python
yum install mysql-devel.x86_64

at the very beginning, i just installed MySQL-python, but the issue still existed. So i think if this issue happened, you should also take mysql-devel into consideration. Hope this helps.

Solution 5

I got this issue when I was working on SQLAlchemy. The default dialect used by SQLAlchemy for MySQL is mysql+mysqldb.

engine = create_engine('mysql+mysqldb://scott:tiger@localhost/foo')

I got the "No module named MySQLdb" error when the above command was executed. To fix it I installed the mysql-python module and the issue was fixed.

sudo pip install mysql-python
Share:
264,451

Related videos on Youtube

user3182194
Author by

user3182194

Updated on April 04, 2022

Comments

  • user3182194
    user3182194 about 2 years

    I am referring the following tutorial to make a login page for my web application. http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982

    I am having issue with the database. I am getting an

    ImportError: No module named MySQLdb
    

    when I execute

    http://127.0.0.1:5000/testdb
    

    I have tried all possible ways to install python mysql, the one mentioned in the tutorial, easy_install, sudo apt-get install.

    I have installed mysql in my virtual env. My directory structure is just the same as whats explained in the tutorial. The module is sucessfully installed in my system and still I am getting this error.

    Please help. What could be causing this.

    • Ffisegydd
      Ffisegydd about 10 years
      You've installed MySQL but have you installed the MySQLdb package for Python?
    • user3182194
      user3182194 about 10 years
      I used sudo apt-get install mysql-python. When i used pip i got Environment error for mysql_config
    • user3182194
      user3182194 about 10 years
      mysql is installed within my virtual enviroment and working properly and mysqldb has also been installed.
  • codersofthedark
    codersofthedark over 9 years
    I think the answer to the question is this
  • mxi1
    mxi1 about 9 years
    before install mysql-python, you still need to install mysql or something containing the msyql_config command.
  • Nima Soroush
    Nima Soroush over 8 years
    python-mysqldb has some dependencies like VC++ library and sometimes doesn't work on 64 bit OS
  • genepool99
    genepool99 almost 8 years
    This worked for me too. Ubuntu 16.04 with python 2.7
  • Kirby
    Kirby almost 8 years
    pip install MySQL-python yeah, that's the ticket
  • Graham Dumpleton
    Graham Dumpleton over 7 years
    If you don't have '-dev' packages and a compiler, you cannot install any Python module which has a C extension component that needs to be compiled. Your only option would be to use a separate build box which has the extra bits and build Python wheel files. Then copy the wheels to your target system and install the packages from wheels instead.
  • ssi-anik
    ssi-anik over 7 years
    If anyone is down here, and getting environment error: mysql_config not found kinda thing, then sudo apt-get install libmysqlclient-dev can help you. Original answer: stackoverflow.com/a/5178698/2190689
  • ssi-anik
    ssi-anik over 7 years
    If you get config parser type of error, then you may look at this. stackoverflow.com/a/23978968/2190689
  • Chen Levy
    Chen Levy almost 7 years
    For those people who this answer didn't resolve their issue this might be due to a virtualenv issue see: stackoverflow.com/a/43984484
  • Tina J
    Tina J over 6 years
    More accurately: sudo apt-get install python-mysqldb
  • CashIsClay
    CashIsClay about 6 years
    Did I miss something? Why isn't this listed here? Specifically that dialect name: docs.sqlalchemy.org/en/latest/core/engines.html. This was exactly what I had wrong though, thanks.
  • Braden Holt
    Braden Holt almost 6 years
    Note the db uri is somewhat dependent upon the connector you use :)
  • Rahul Goyal
    Rahul Goyal over 5 years
    this didn't work with python2.7.* for me. use of pip install MySQL-python worked.
  • ben
    ben about 5 years
    this one helped so much while searching after a solution for flask-sqlalchemy errors. I faced: 1. Error running WSGI application ModuleNotFoundError: No module named 'MySQLdb' 2. (_mysql_exceptions.OperationalError) (2006, 'SSL connection error: SSL_CTX_set_tmp_dh failed') (Background on this error at: sqlalche.me/e/e3q8) thank you very much and I hope, that others will find this solution with this error descriptions faster as I have done!
  • trpt4him
    trpt4him about 5 years
    Worked on CentOS, seems to be the most non-OS-dependent answer. Thanks!
  • penny chan
    penny chan over 4 years
    be remember to change ` SQLAlchemy URI ` ! this works for python 3
  • Pramod Sharma
    Pramod Sharma over 4 years
    In my case(Python Anconda 3) , your link which has other connectors worked . I used MySQL-Connector
  • nsssayom
    nsssayom over 4 years
    Works for me on python 3.7.3
  • Bilal
    Bilal over 4 years
    Thanks for sharing, It worked when I was using Windows but I had some issues with mysqlclient (OSError: mysql_config not found) when I was trying to run the same app on Ubuntu, the accepted answer solved it.
  • Erizo
    Erizo over 4 years
    This depends on Microsoft Visual C++ libraries in Windows environments
  • Saaru Lindestøkke
    Saaru Lindestøkke about 4 years
    Works on OSX with anaconda and python 3.7.6
  • DachuanZhao
    DachuanZhao about 4 years
    Do not use pymysql driver because it's too slow ! ! !
  • mirekphd
    mirekphd over 3 years
    It works also for MariaDB (tested for v10.4, via SQLAlchemy).
  • M E S A B O
    M E S A B O about 2 years
    simple! very simple and clear.