ImportError: No module named 'mysql' in Python Django

Dung Do Tien Nov 16 2021 132

Hello Guys, I'm just studying Python Django one week before. I used the MySQL database for my project.

I have created some line code to help connect to the database, it's like that

import mysql.connector as sql
con = sql.connect(host="127.0.0.1", user="sa", passwd="my_pass", database="Students")

if con.is_connected() == false
   print("Cant not connect to My SQL")

-- TODO get data here.
   
con.close()

But when running this code I got an exception ImportError: No module named 'mysql'.

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    import mysql.connector
ImportError: No module named 'mysql'

Do you have any suggestions for me? I'm using MySQL 8.0

Thanks so much.

Have 2 answer(s) found.
  • D

    Devx Mon Nov 16 2021

    You need to install mysql-connector package first. You can install as below:

    Python 3:

    pip3 install mysql-connector

    Python 2:

    pip install mysql-connector

    And it's worked for you.

  • P

    Palakorn Sangketchon Nov 16 2021

    I got the same problem and I solved it by running two commands below:

    pip3 install mysql-connector
    pip3 install mysql-connector-python-rf

    I hope it's helpful for you.

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