ImportError: No module named MySQLdb in Python Django

Dung Do Tien Nov 20 2021 111

Hello, I am a newbie in Python I was beginning to learn Python two months ago. I have created a simple web application with Python.

I am using Python version 3.9.7 and installing MySQL version 8.0 and Django

I create a file function help connect to MySQL as below:

ImportError: No module named MySQLdb

But when I ran the code above I got an exception throw : ImportError: No module named MySQLdb.

ImportError: No module named MySQLdb

Any suggestions for me?

Have 3 answer(s) found.
  • d

    dang nhat hai long Nov 20 2021

    I saw you import MySQLdb in your code to help connect to the MySQL database.

    You have to install two packages are mysql-connector-python and mysql-python. To install them you can run the command below:

    pip install mysql-connector-python
    pip install mysql-python

    I hope it solve for you.

  • W

    Watcharapong Jakkawannorasing Nov 20 2021

    You need to install libmysqlclient-dev package first, by using command : sudo apt install libmysqlclient-dev and THEN

    For python 2.x run command:

    pip install mysql-python

    For python 3.x run command:

    pip install mysqlclient

    This solution worked for me.

  • R

    Raja T Nov 20 2021

    We have 3 packages that need to install to help connect to MYSQL:

    1. Install the mysqlclient package:

    pip install mysqlclient

    2. Install the mysql-connector-python packaged:

    pip install mysql-connector-python

    3. Install the pymysql package:

    pip install pymysql

    After all is done, you can re-try run your code.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close