0

I am using python 3.6 within Anaconda. I am trying to connect to mySQL 5.7 through SQLAlchemy as mysql.connector flavor='mysql' is deprecated in the newest version.(I am able to connect to the database when I use python 3.4 and am able to use the flavor=mysql optoin to do inserts into the database through pandas).

I am trying to do the same via this SQLAlchemy engine method. But I am not able to connect.

Here is my code:

import sqlalchemy as sa
engine= sa.create_engine("mysql+mysqldb://user:password@localhost/databasename")

I get the following error:

ModuleNotFoundError: No module named 'MySQLdb'

Please help.

Thanks!

Dishant shah
  • 19
  • 2
  • 6
  • for **Python 3** you should use [`PyMySQL`](http://pymysql.readthedocs.io/en/latest/index.html) instead of `MySQLdb` – Azat Ibrakov Jun 14 '17 at 15:06
  • yes...thank you ...that worked like a charm! :) – Dishant shah Jun 15 '17 at 05:32
  • The basic problem is that the MySQLDb1 adapter does not support Python3. The answers to [this SO question](https://stackoverflow.com/questions/43102442/whats-the-difference-between-mysqldb-mysqlclient-and-mysql-connector-python) discuss the different adapters available instead and more of the context of this situation. I'd recommend looking at that and deciding which alternative best suits you. – Sam Mar 19 '19 at 08:32

1 Answers1

4

This is extremely late in the game, I had the same issue with python3 and Flask-SQLAlchemy.

I fixed this by installing pymysql in my virtualenv

pip install pymysql 

And then use the database_uri as

'mysql+pymysql://{username}:{password}@{host}:{port}/{db_name}'

instead of

'mysql+mysqldb...'
she12
  • 91
  • 4