AttributeError: __enter__ error when trying to query from MySQL

Viewed 3200

The following used to worked for me. For some reason it is not anymore.

>>> import configparser
>>> from mysql.connector import connect, Error
>>> with open('file.cfg', 'r') as f:
...     config_string = '[s]\n' + f.read()
...
>>> config = configparser.ConfigParser()
>>> config.read_string(config_string)
>>> with connect(host=config.get('s','HOST'),user=config.get('s','USER'),password=config.get('s','PASS'),database="db") as connection:
...     with connection.cursor() as cursor:
...             cursor.execute("select dt from tab limit 1")
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: __enter__
>>>

mysql_connector-2.2.9-cp37-cp37m-linux_x86_64.whl

Python 3.7.6 (default, Feb 26 2020, 20:54:15)

1 Answers

Solution:

pip uninstall mysql-connector-python
pip uninstall mysql-connector
pip install mysql-connector-python

The problem, as I see it was that I had installed mysql-connector (which is deprecated) after mysql-connector-python (new the new version of mysql-connector). Thous, making mysql-connector-python the only lib on the box that python can use.

Related