Dishant shah
Dishant shah

Reputation: 29

Not able to connect to mySQL through SQLAlchemy in Python 3.6

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!

Upvotes: 1

Views: 2350

Answers (1)

she12
she12

Reputation: 111

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...'

Upvotes: 4

Related Questions