ModuleNotFoundError: No module named '_version' when importing mysql - mysql

import mysql.connector
conn = mysql.connector.connect(host="127.0.0.1", user="root", passwd="root", database="Master")
print(conn)
conn.close()

Python needs a MySQL driver to access the MySQL database.
install mysql-connector :
python -m pip install mysql-connector
Start by creating a connection to the database
Use the username and password from your MySQL database:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword"
)
print(mydb)

I had the myaql connector in place but git the following error:
mysql/connector/__init__.py", line 34, in <module>
import _version
ModuleNotFoundError: No module named '_version'
Neither pipreqs --force nor python -m pip install mysql-connector-python could resolve the problem. However, the solution by Arpit Jain solve the issue.

Related

mysql driver not loading on flask_sqlalchemy python3 in venv | No module named 'MySQLdb'

Im trying to make the following combination work.
No luck. Anyone here can help ?
flask_sqlalchemy
mysqlclient
mysql-connector-python
Python 3.9.5
setup.cfg has above package and being installed in a venv
Error
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'], echo = True)
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/__init__.py", line 500, in create_engine
return strategy.create(*args, **kwargs)
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/strategies.py", line 87, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 118, in dbapi
return __import__("MySQLdb")
ModuleNotFoundError: No module named 'MySQLdb'
venv had to be resourced in a shell environment. So packages installed were not in path.
Also, mysql-connector-python is not required in the above setup.
So much steps for a local isolated package installation scope.
python3 -m venv venv
. venv/bin/activate
pip install -e .
cp .env to instance/
flask run

Error ModuleNotFoundError: No module named 'pymysql'

I have a problem with my project, I installed the "pymysql", but i am trying to use the "pymysql", but i try to use and gives the following error: ModuleNotFoundError: No module named 'pymysql'
pip install pymysql
import pymysql
ModuleNotFoundError: No module named 'pymysql'
I fixed this on my pc too it took me 1 day :)
So go to your cmd or terminal and write:
pip uninstall pymysql
python -m pip --upgrade pip
pip install pymysql
This will fix it as the version of pip u are using cant build a wheel for the module
Hope u will fix it :)

How to connect superset with mysql database?

I was trying to connect superset with mysql database with this SQLAlchemy URI:mysql://localhost:3306/mysql/database/table1 but not able to connect.
I was getting this error:
ERROR: {"error": "Connection failed!
The error message returned was:
No module named 'MySQLdb'", "stacktrace": "Traceback (most recent call last):
File \"/Users/akay/venv/lib/python3.7/site-packages/superset/views/core.py\", line 1703, in testconn
engine = database.get_sqla_engine(user_name=username)
File \"/Users/akay/venv/lib/python3.7/site-packages/superset/utils/core.py\", line 131, in __call__
value = self.func(*args, **kwargs)
File \"/Users/akay/venv/lib/python3.7/site-packages/superset/models/core.py\", line 923, in get_sqla_engine
return create_engine(url, **params)
File \"/Users/akay/venv/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py\", line 479, in create_engine
return strategy.create(*args, **kwargs)
File \"/Users/akay/venv/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py\", line 87, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File \"/Users/arkay/venv/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py\", line 118, in dbapi
return __import__(\"MySQLdb\")
ModuleNotFoundError: No module named 'MySQLdb'
"}
Is there any way to connect to a MySQL database?
MySQLdb is not compatible with Python 3, You need to use pymysql.
Install pymysql:
$ pip install pymysql
Connect to mysql data source with below URI string.
mysql+pymysql://user:pass#host/databasename
Configure your database user with hostname=localhost
You need to install mysql-connector-python module:
$ pip install mysql-connector-python
Had similiar issue. I'm sure this will save time for some of you out there:
Use 127.0.0.1 instead of localhost.
Here is the perfect template for MySQL string:
SQLALCHEMY_DATABASE_URI = 'mysql://username:password#127.0.0.1:3306/schema-name'
Install MySQL:
pip3 install mysqlclient
Upgrade SQLAlchemy:
pip install sqlalchemy==1.3.24
Load DB:
superset db upgrade
Create Admin user:
superset fab create-admin
If you are using Docker try using host.docker.internal as host name. It worked for me

Apache Airflow : airflow initdb results in "ImportError: No module named json"

On Ubuntu 16.04 with Python 2.7 default version, I am trying to install Apache airflow but ran into several issues and currently I see on
apache initdb
Traceback (most recent call last):
File "/usr/local/bin/airflow", line 21, in <module>
from airflow import configuration
File "/usr/local/lib/python2.7/dist-packages/airflow/__init__.py", line 40, in <module>
from flask_admin import BaseView
File "/usr/local/lib/python2.7/dist-packages/flask_admin/__init__.py", line 6, in <module>
from .base import expose, expose_plugview, Admin, BaseView, AdminIndexView # noqa: F401
File "/usr/local/lib/python2.7/dist-packages/flask_admin/base.py", line 6, in <module>
from flask import Blueprint, current_app, render_template, abort, g, url_for
File "/usr/local/lib/python2.7/dist-packages/flask/__init__.py", line 20, in <module>
from .app import Flask
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 69, in <module>
from .wrappers import Request
File "/usr/local/lib/python2.7/dist-packages/flask/wrappers.py", line 14, in <module>
from werkzeug.wrappers.json import JSONMixin as _JSONMixin
ImportError: No module named json
Tried these things till now:
$ sudo apt-get install build-essential autoconf libtool pkg-config python-opengl python-imaging python-pyrex python-pyside.qtopengl idle-python2.7 qt4-dev-tools qt4-designer libqtgui4 libqtcore4 libqt4-xml libqt4-test libqt4-script libqt4-network libqt4-dbus python-qt4 python-qt4-gl libgle3 python-dev
sudo pip install -U Werkzeug==0.14.1
sudo pip install -U Jinja2==2.10.1
Also tried
pip install jsonate
pip install json # (this did not work)
python
Python 2.7.12 (default, Nov 12 2018, 14:36:49)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> exit()
Nothing worked so far.
Just upgrade flask. You can use this command:
sudo pip install --upgrade Flask
The problem is known, and is described here, in the ASF Mail Archives
We removed the incompatible Werkzeug library from apache master this
weekend. It caused Airflow installations to fail.
To resolve, manually install the werkzeug library in a correct version:
pip install werkzeug>=0.15.0
As explained in the mail list, this seems to be a temporary problem, and this solution is a workaround. The problem is encountered in version 1.10.3.
On Mac while installing apache-airflow, I got following error:
ERROR: apache-airflow 1.10.3 has requirement jinja2<=2.10.0,>=2.7.3, but you'll have jinja2 2.10.1 which is incompatible.
ERROR: apache-airflow 1.10.3 has requirement werkzeug<0.15.0,>=0.14.1, but you'll have werkzeug 0.15.4 which is incompatible.
Re-installing werkzeug and jinja worked for me.
pip install jinja2>=2.10.0
pip install werkzeug>=0.15.0
sudo pip install --upgrade Flask worked for me

Docker: ModuleNotFoundError: No module named 'MySQLdb'

import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'
I'm using Docker to serve a django app. Using Python3 and tried everything but the container is still not working.
-Tried installing:
pip install pymysql
pip install mysql
pip install mysql-connector
pip install msqlclient
-Added these two lines:
import pymysql
pymysql.install_as_MySQLdb()
to manage.py, init.py and wsgi.py
Also installed libmysqlclient-dev
The app is up and running in my local env, but when I push a container image the app won't start. I'm pretty sure that "pymysql.install_as_MySQLdb()" should do the trick, but for some reasons it is not getting executed.
Any ideas?