I have a .csv file that I would like to first store in a pandas dataframe, and then import into a MySQL database table.
I have followed this posting for initial setup: pandas dataframe to mysql db error database flavor mysql is not supported
My .py code is as follows:
from sqlalchemy import create_engine
import pandas
engine = create_engine("mysql+mysqldb://root:password@locahost/new_schema")
df = pandas.read_csv('items.csv')
df.to_sql('items', con=engine, flavor='mysql', if_exists='append')
The error message I get is this:
/PyCharmProjects/venv2/bin/python /PyCharmProjects/venv2/tutorial/tutorial/other/csvToMySQL.py
Traceback (most recent call last):
File "/PyCharmProjects/venv2/tutorial/tutorial/other/csvToMySQL.py", line 4, in <module>
engine = create_engine("mysql+mysqldb://root:password@locahost/new_schema")
File "/PyCharmProjects/venv2/lib/python3.6/site-packages/sqlalchemy/engine/__init__.py", line 387, in create_engine
return strategy.create(*args, **kwargs)
File "/PyCharmProjects/venv2/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 80, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/PyCharmProjects/venv2/lib/python3.6/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 110, in dbapi
return __import__('MySQLdb')
ModuleNotFoundError: No module named 'MySQLdb'
What have I done wrong in my engine setup?
Tech specs:
- Mac OSX 10.11 (El Capitan)
- Python 3.6.1
- PyCharm CE 2017.1.2
- 5.7.18 MySQL Community Server (GPL)
Update #1
I tried 'pip install MySQL-python' based on the recommendation below. This is the error I received:
Collecting MySQL-python
Using cached MySQL-python-1.2.5.zip
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/5y/zqsn7cwx5dv_ntvdrwr08l100000gn/T/pip-build-umrr237y/MySQL-python/setup.py", line 13, in <module>
from setup_posix import get_config
File "/private/var/folders/5y/zqsn7cwx5dv_ntvdrwr08l100000gn/T/pip-build-umrr237y/MySQL-python/setup_posix.py", line 2, in <module>
from ConfigParser import SafeConfigParser
ModuleNotFoundError: No module named 'ConfigParser'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/5y/zqsn7cwx5dv_ntvdrwr08l100000gn/T/pip-build-umrr237y/MySQL-python/
Update #2
I tried 'pip install mySQLdb' based on the recommendation below. This is the error I received:
Collecting MySQLdb
Could not find a version that satisfies the requirement MySQLdb (from versions: )
No matching distribution found for MySQLdb