Unable to import mysql-python connector Py3.8, MySQL8.0.19

Viewed 998

I'm running Python 3.8 and MySQL 8.0.19 and installed the mysql-connector-python-8.0.19 on Mac 10.15.4. I didn't get any errors on the installation of any of them. I can create a database through MySQL but when I try to import the connector, I get this error:

import mysql.connector Traceback (most recent call last): File "", line 1, in import mysql.connector File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/init.py", line 41, in import dns.resolver

Jaclyn Horton

2 Answers

Try uninstalling it and reinstalling it again and importing it.
Run the following code on your IDLE Shell:

>>> import os
>>>
>>> os.system('pip uninstall mysql.connector & pip install mysql.connector')
>>>
>>> import mysql.connector as msc
>>>
>>> con_obj = msc.Connect(host='<host_name>', user='<user_name>',
password='<your_password>', autocommit=True)

If you don't encounter an error, you are all good!

And one more thing,

If you don't remember any of these parameters (kwargs), and you have a default installation of MySQL, then, try:

>>> con_obj = msc.Connect(host='localhost', user='root',
password='tiger', autocommit=True)

This is a bug in mysql.connector. The workaround is to manually install dnspython using pip or your OS's package manager.

For example:

python3.8 -m pip install dnspython
Related