Skip to content

Exception when trying to import python mysql connector on raspberry pi

I’m trying to use the python MySQL connector on my Raspberry PI but I’m getting this error as soon as I add the import statement to the script.

import os
import mysql.connector

The same happens when I try to import only parts of it.

import os
from mysql.connector import connect, Error

The error:

Traceback (most recent call last):
  File "", line 2, in <module>
    import mysql.connector
  File "/home/pi/.local/lib/python2.7/site-packages/mysql/connector/", line 53, in <module>
    from .connection import MySQLConnection
  File "/home/pi/.local/lib/python2.7/site-packages/mysql/connector/", line 442
    f"This connection is using {tls_version} which is now "

I used PIP to install the connector …

pip install mysql-connector-python

… and also already tried to force-reinstall it:

pip install --upgrade --force-reinstall mysql-connector-python



This problem appears to be caused by using the default Raspbian install of Python, which is Python 2.7.

Use Python 3 instead. It is also installed on Raspbian. Use python3 and pip3 in place of python and pip.

User contributions licensed under: CC BY-SA
8 People found this is helpful