1

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!

3
  • for Python 3 you should use PyMySQL instead of MySQLdb Commented Jun 14, 2017 at 15:06
  • yes...thank you ...that worked like a charm! :) Commented Jun 15, 2017 at 5:32
  • The basic problem is that the MySQLDb1 adapter does not support Python3. The answers to this SO question 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. Commented Mar 19, 2019 at 8:32

1 Answer 1

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...'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.