0

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 "script.py", line 2, in <module>
    import mysql.connector
  File "/home/pi/.local/lib/python2.7/site-packages/mysql/connector/__init__.py", line 53, in <module>
    from .connection import MySQLConnection
  File "/home/pi/.local/lib/python2.7/site-packages/mysql/connector/connection.py", 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
5
  • 1
    Perhaps related to your use of Python 2.7? Commented Aug 11, 2021 at 14:04
  • I'm completely new to python - what do you mean with "my use of python 2.7" and what should I change? Commented Aug 11, 2021 at 14:13
  • No-one should start new projects with Python 2.x as it was originally sunset in 2015 and finally sunset in Jan 2020. It's no longer supported. All new projects should use Python 3.x. All existing Python 2.x projects should migrate to Python 3.x. Note: I'm not saying that your problem is because you are using Python 2.7, it likely isn't, but I'm just raising it as one potential cause. Commented Aug 11, 2021 at 14:22
  • Note: Raspbian likely has both Python 2.x (available via python and pip) and Python 3.x (available via python3 and pip3) pre-installed. Commented Aug 11, 2021 at 14:27
  • Using python 3 everything works now fine, thank you. I still don’t have a clue why python 2 doesn’t work, though. Commented Aug 12, 2021 at 11:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.