ImportError: No module named MySQLdb
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
Related videos on Youtube
user3182194
Updated on April 04, 2022Comments
-
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 about 10 yearsYou've installed MySQL but have you installed the MySQLdb package for Python?
-
user3182194 about 10 yearsI used sudo apt-get install mysql-python. When i used pip i got Environment error for mysql_config
-
user3182194 about 10 yearsmysql is installed within my virtual enviroment and working properly and mysqldb has also been installed.
-
-
codersofthedark over 9 yearsI think the answer to the question is this
-
mxi1 about 9 yearsbefore install mysql-python, you still need to install mysql or something containing the msyql_config command.
-
Nima Soroush over 8 yearspython-mysqldb has some dependencies like VC++ library and sometimes doesn't work on 64 bit OS
-
genepool99 almost 8 yearsThis worked for me too. Ubuntu 16.04 with python 2.7
-
Kirby almost 8 years
pip install MySQL-python
yeah, that's the ticket -
Graham Dumpleton over 7 yearsIf 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 over 7 yearsIf 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 over 7 yearsIf you get config parser type of error, then you may look at this. stackoverflow.com/a/23978968/2190689
-
Chen Levy almost 7 yearsFor 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 over 6 yearsMore accurately:
sudo apt-get install python-mysqldb
-
CashIsClay about 6 yearsDid 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 almost 6 yearsNote the db uri is somewhat dependent upon the connector you use :)
-
Rahul Goyal over 5 yearsthis didn't work with python2.7.* for me. use of
pip install MySQL-python
worked. -
ben about 5 yearsthis 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 about 5 yearsWorked on CentOS, seems to be the most non-OS-dependent answer. Thanks!
-
penny chan over 4 yearsbe remember to change ` SQLAlchemy URI ` ! this works for python 3
-
Pramod Sharma over 4 yearsIn my case(Python Anconda 3) , your link which has other connectors worked . I used MySQL-Connector
-
nsssayom over 4 yearsWorks for me on python 3.7.3
-
Bilal over 4 yearsThanks 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 over 4 yearsThis depends on Microsoft Visual C++ libraries in Windows environments
-
Saaru Lindestøkke about 4 yearsWorks on OSX with anaconda and python 3.7.6
-
DachuanZhao about 4 yearsDo not use pymysql driver because it's too slow ! ! !
-
mirekphd over 3 yearsIt works also for MariaDB (tested for v10.4, via SQLAlchemy).
-
M E S A B O about 2 yearssimple! very simple and clear.