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.
Related
I am very new in Python as wel as in backend develpoment. I want to connect my python code with database so I installed mysql-connector-python using C:\Users\CCU-012\AppData\Local\Programs\Python\Python37\Scripts>python -m pip install mysql-connector-python but when I use import mysql.connector in my python code I am getting error Traceback (most recent call last):
File "C:/Users/CCU-012/PycharmProjects/array/demo_mysql_connection.py", line 1, in <module>
import mysql.connector
ModuleNotFoundError: No module named 'mysql'
You've installed the module globally, but your project is using a virtualenv (as evident from the "Python 3.7 (array)" text in PyCharm's status bar).
Install the module within the virtualenv; the easiest way to do that through PyCharm is to open the Python Console (4th tab at the bottom there) and
pip install mysql-connector-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.
I've just created a Google Cloud compute engine, installed google-cloud package with both pip and pip3, and I'm experiencing the following error when launching a script with python3
from google.cloud import bigquery
File "/usr/local/lib/python3.5/dist-packages/google/cloud/bigquery/__init__.py", line 35,
in <module>
from google.cloud.bigquery.client import Client
File "/usr/local/lib/python3.5/dist-packages/google/cloud/bigquery/client.py", line 36, in
<module>
(more traceback lines..)
from pyasn1_modules.rfc2459 import Certificate
File "/usr/local/lib/python3.5/dist-packages/pyasn1_modules/rfc2459.py", line 20, in <modu
le>
from pyasn1.type import opentype
ImportError: cannot import name 'opentype'
On the compute engine the following packages are installed:
pyasn1==0.1.9
pyasn1-modules==0.2.1
google-cloud==0.30.0
google-cloud-bigquery==0.28.0
Which can be the problem here?
Posting my solution in case it helps someone else - this fixed it for me:
pip install --upgrade google-auth-oauthlib
More details discussed here: https://www.raspberrypi.org/forums/viewtopic.php?f=114&t=198933&p=1241439#p1241439
It looks like you have an issue with pyasn1, so you could try installing a newer version (the latest is 0.4.2), or even reinstalling it manually with:
sudo apt-get --reinstall install python-pyasn1 python-pyasn1-modules
And if you are inside a virtualenv, use instead:
pip install pyasn1 pyasn1-modules
FWIW - Had the same issue - none of the above worked. I eventually discovered that if I did it under sudo it did work.
stracing the original - I found that I had a ~/.local directory which had a pyasn1 directory where it was trying to get the files from, but the opentype.py one did not appear there. When I deleted that whole directory - it started working.
I assume it was some sort of cache that was partial, and out-of-date???
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
I have installed Flask with:
pip install flask-debugtoolbar==0.10.0
When I import flask in Python, it works, but when I do import flask.ext.login, I get
ImportError: No module named flask.ext.login
Why can't I import this?
You haven't installed Flask-Login. When you installed Flask-DebugToolbar, Flask got installed automatically as a required dependency. Flask-Login is an independent extension to Flask, and isn't required by that package.
Install it with
pip install flask-login
Then import it with
import flask_login
You can list all installed modules with pip list.