Airflow:How do I use PyMySQL with MySQL Hook? - python

I am trying to use MySQL in Apache Airflow. I already use PymySQL to query MySQL databases and did not have MySQLDb installed. Thus I got the error:
from airflow.hooks.mysql_hook import MySqlHook
File "/anaconda3/anaconda/lib/python3.6/site-packages/airflow/hooks/mysql_hook.py", line 15, in <module>
import MySQLdb
ModuleNotFoundError: No module named 'MySQLdb'
If I go into mysql_hook.py class I already see they are using MySQLDB
import MySQLdb
import MySQLdb.cursors
What alternative I have other than importing pymysql manually instead of in a hook?
I am using Python 3.6
Thanks

In order for the MySQL hook to work in Airflow, you need to install the MySQL extra package. If you have installed Airflow with pip, then the following command will do:
pip install apache-airflow[mysql]
This will install the mysqlclient package, which is the one required here.

Related

ModuleNotFoundError: No module named 'mysql'| python-mysql

i have this error:
import mysql.connector
ModuleNotFoundError: No module named 'mysql'
I already install via pip3 mysql and mysql-connector
and of course also call import mysql.connector
im using xampp version 8.0.0 in my m1 I don't know if that will be the problem
pip3 list
error

How to solve 'No module named pymysql'

I use computer that is Window 10 and my Python version is 3.6.
I installed pymysql and MySQLdb as in the picture below.
But When I tried to run python, I got this error : No module named 'pymysql'
also, MySQLdb...
How can I solved it?
Try to use pip3 install PyMySQL and import pymysql

Python not finding MySQLdb on OSX..?

Using pip I installed mysql-python, got:
Successfully installed MySQL-python-1.2.5
However when I try to import MySQLdb I get:
ImportError: No module named MySQLdb
locate MySQLdb gives me:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb
Why is Python not finding the module?
Thanks!
Gil.

(Python 3.6.1) Pandas df to MySQL db error, 'ModuleNotFoundError: No module named 'MySQLdb'

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

ImportError: No module named flask.ext.mysql

Hi I am trying to run a python file that has :
from flask import Flask, render_template, json, request
from flask.ext.mysql import MySQL
from werkzeug import generate_password_hash, check_password_hash
I started in windows, I got the same error :
ImportError: No module named flask.ext.mysql
Someone told me don't develop python in windows, it's lot of headache, so I started the same project in Ubuntu, but I got the same problem :
vagrant#precise32:/vagrant/FlaskMysql/FlaskApp$ python app.py
Traceback (most recent call last):
File "app.py", line 2, in <module>
from flask.ext.mysql import MySQL
File "/usr/local/lib/python2.7/dist-packages/flask/exthook.py", line 87, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named flask.ext.mysql
Please your help is appreciated.
Here is the official Flask-MySQL documentation:
https://flask-mysql.readthedocs.org/en/latest/#
You'll find that the current import syntax is as follows:
from flaskext.mysql import MySQL
Be sure that you install Flask-MySQL:
$ pip install flask-mysql
commenter, meta
It's flaskext, so...
Change
from flask.ext.mysql import MySQL
to
from flaskext.mysql import MySQL
I had the same problem, so I installed flask-mysql using:
$ pip install flask-mysql
and the module name is flaskext.mysql
It seems like a virtualenv config problem
you should get rid of old virtualenv and make a new one like this
virtualenv yournewvirtualenv --python=/usr/bin/python3.4
This is the link I referenced:
https://www.pythonanywhere.com/forums/topic/2877/
First from flaskext.mysql import MySQL worked then pip install Flask-MySQL also worked finally... it was driving me nuts! I had taken to many routes to fix it.
On Windows:
pip install Flask-MySQL
On Mac:
pip install flask-mysql
I was facing the same error.
First I installed flask-MySQL
pip install flask-mysql
Then added below-line in app.py file
from flaskext.mysql import MySQL
It worked.

Categories

Resources