-1

im trying to connect my python script to mysql, but any of the mysql python connectors does'nt work.

Here is part of my code:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="",
  database="mydatabase"
)

and here is error:

 Traceback (most recent call last):
   File "type.py", line 1, in <module>
     import mysql.connector
   File "/root/mysql.py", line 1, in <module>
     import mysql.connector
 ImportError: No module named connector
  • Install module using pip command based on your python version. – bkyada Jul 18 '19 at 17:24
  • Try these possible answers: https://stackoverflow.com/a/24272278/8291579 https://stackoverflow.com/questions/12229580/python-importing-a-sub-package-or-sub-module – Marcus Weinberger Jul 18 '19 at 17:26
  • Possible duplicate of [Import mysql.connector Python](https://stackoverflow.com/questions/24272223/import-mysql-connector-python). See the most upvoted answer rather than accepted one. – jpmc26 Jul 18 '19 at 21:45

2 Answers2

0

You may not have Connector installed, I would recommend using pip list to see if it is or pip install mysql-connector-python.

This is a great resource: https://dev.mysql.com/doc/connector-python/en/connector-python-installation-binary.html

jdpy19
  • 375
  • 1
  • 13
0

You can first check whether mysqlconnector is installed on your device or not.
For this the simplest way is

  • to write import mysql.connector on interactive mode and hit enter;
  • if it doesn't give you error then your good.
  • If it does,
    • then go to tools > open system shells >
    • type : pip install mysql.connector > press enter.
    • This is install the connector and try it again after it is installed.
cursorrux
  • 1,382
  • 4
  • 9
  • 20