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.
JavaScript
x
3
1
import os
2
import mysql.connector
3
The same happens when I try to import only parts of it.
JavaScript
1
3
1
import os
2
from mysql.connector import connect, Error
3
The error:
JavaScript
1
8
1
Traceback (most recent call last):
2
File "script.py", line 2, in <module>
3
import mysql.connector
4
File "/home/pi/.local/lib/python2.7/site-packages/mysql/connector/__init__.py", line 53, in <module>
5
from .connection import MySQLConnection
6
File "/home/pi/.local/lib/python2.7/site-packages/mysql/connector/connection.py", line 442
7
f"This connection is using {tls_version} which is now "
8
I used PIP to install the connector …
JavaScript
1
2
1
pip install mysql-connector-python
2
… and also already tried to force-reinstall it:
JavaScript
1
2
1
pip install --upgrade --force-reinstall mysql-connector-python
2
Advertisement
Answer
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
.