1

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
slsu
  • 133
  • 3
  • 13
  • Have you tried installing that module? – ForceBru Jun 14 '17 at 16:53
  • @ForceBru - Yes, I tried to install that module via pip (pip install mySQLdb). I updated the original post above, under Update #2. Any ideas? – slsu Jun 14 '17 at 17:59
  • whenever you see an error message telling you that some module isn't installed, you should probably install it with `pip`. – ForceBru Jun 14 '17 at 18:01

2 Answers2

3

After multiple tries, I finally found the way to make things work quickly with Python 3.6.1. Apparently, the fastest way to get this done was to use Homebrew to install mySQL and then install mysql client. I had originally installed MySQL via .dmg, and I was running into a lot of errors when installing various packages.

In Terminal, type:

  • $brew install mySQL
  • $pip install mysqlclient

The .py code can now run:

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, if_exists='append')

(fyi, I dropped 'flavor = mysql' from the original code since it is not a recognized parameter - see https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_sql.html#pandas-dataframe-to-sql)

Reference link: Can't install mysql-python with pip on MacOS 10.12.4

slsu
  • 133
  • 3
  • 13
0

Your Project cannot find MYSQLdb, which is an interface for connecting to a MySQL database server.

You haven't imported the MySQLdb to access the package.

If that's the issue then add the import statement at the top of your program to use the package.

import MySQLdb

If you still couldn't access the package then chances are the MySQLdb package is not installed.

Then in that case, first you need to search the package and then try to install that:

You can use pip to search and install any package.

First to make sure whether you have that package or not.

Run this command in your cmd.

  • pip list (list all the installed packages)

Check whether you have (MySQL-python) package listed or not.

If not then run,

  • pip install MySQL-python ( To install the package )

then try to run your code.


MySQL-python was used with python version 2.*, mysqlclient is the Python 3 compatible fork of MySQL-python.

To install this library, pip install mysqlclient

aMighty
  • 283
  • 4
  • 11
  • Thanks for the tips. I updated my post based on 'pip install MySQL-python' (see Update #1). Still running into issues. Any ideas? – slsu Jun 14 '17 at 17:55