tawny_burrito
tawny_burrito

Reputation: 61

Cannot connect to a mysql:// instance with SQLAlchemy from my Mac

I'm trying to connect to a mySQL database through SQLAlchemy and I can't get it to work for the life of me. Here's the code I'm running and the error I'm getting.

 import pandas as pd
 import sqlalchemy as sql
 import urllib.parse
 user = 'user'
 pswd = urllib.parse.quote_plus('password')
 connect_string = 'mysql://{}:{}@localhost:8018/test'.format(user, pswd)
 sql_engine = sql.create_engine(connect_string)

 >>> ModuleNotFoundError: No module named 'MySQLdb'

After seeing this, I've tried numerous different ways of installing 'MySQLdb' ( including the obvious 'pip3 install mysqlclient') and when I run that in my terminal, I get the following.

 >>> ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

I know this question has been posted here before but I've spent 2 hours now trying to figure this out with the advice on those other questions and nothing is working. Can anyone help? Below is a picture of the full error I'm getting when I try and install "mysqlclient"

enter image description here

Upvotes: 1

Views: 1106

Answers (2)

phd
phd

Reputation: 94397

SQLAlchemy is a high-level ORM. To connect to backends it needs Python DB API driver(s). You can install SQLAlchemy with a MySQL driver using

pip install -U 'SQLAlchemy[mysql]'

See the list of extra drivers available for such installation.

Most drivers have parts written in C/C++ and thus require compilation. You need to install MySQL-related libraries to compile mysqlclient. See https://stackoverflow.com/search?q=%5Bpip%5D+install+%5Bmysql%5D+%22mysql_config%22+not+found

There are a few pure-Python drivers. They're easier to install. PyMySQL is one such driver. To install it along with SQLAlchemy:

pip install -U 'SQLAlchemy[pymysql]'

Upvotes: 1

Gord Thompson
Gord Thompson

Reputation: 123399

According to

https://pypi.org/project/mysqlclient/#files

mysqlclient only offers pre-compiled wheel files for Windows machines running 64-bit Python 3.6, 3.7, and 3.8. All other configurations require that it be installed from source, and that can be ... complicated.

You may want to try installing PyMySQL instead, and use a connection URI of the form

mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]

Upvotes: 1

Related Questions