Error loading MySQLdb module. Did you install mysqlclient? on MacOS - mysql

Am Trying to connect to my mysql db from django app.
I get the below error during migration:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb
module. Did you install mysqlclient?
I've already installed mysqlclient as below:
Requirement already satisfied: mysqlclient in
/usr/local/lib/python3.7/site-packages (1.4.2.post1)
I've also tried with pymysql and adding below code to ini.py file:
import pymysql
pymysql.install_as_MySQLdb()
Gives me some other errors.
What could be wrong?
Python 3.7 , mysql 5.7 and Django 2.2 are my setup versions.

I had the same issue. Running the below command fixed it for me.
pip install --force-reinstall --ignore-installed --no-binary :all: mysqlclient

I had the same issue. The thing that worked for me is the following:
https://stackoverflow.com/a/54521244/12497648,
except when I did brew install mysql-client
I got the message Warning: mysql-client 5.7.23_1 is already installed and up-to-date
To reinstall 5.7.23_1, run "brew reinstall mysql-client" so I ran brew reinstall mysql-client after which I continued with the instructions from the link above (export PATH... etc.)
(also don't forget to do the pip wheel mysqlclient / pip install mysqlclient)

If the error includes a Reason: image not found error, then it can be solved with symlinks like this:
Library not loaded: #rpath/libmysqlclient.21.dylib Reason: image not found Django migrate error using mysqlclient DB driver and MySQL 8 with macOS

Related

cannot connect to mysql database ubuntu 20.4 (VPS) "ModuleNotFoundError: No module named 'MySQLdb'"

I am working on the setup of my first unmanaged vps(ubuntu 20.4) and following the document to connect my django site to mysql server "https://www.digitalocean.com/community/tutorials/how-to-create-a-django-app-and-connect-it-to-a-database"
able to setup
python/django/virtualenv
mysql server - tested from mysql workbench on my local machine-Ok
apache2 working ok. able to see the welcome screen with sqlite3.
now moving to mysql. i tried both the way('ENGINE': 'django.db.backends.mysql' and option file '/etc/mysql/my.cnf' to connect to mysql server from django project.
None of the alternatives working. activated virtualenv, installed mysqldb using
a) sudo apt install libmysqlclient-dev default-libmysqlclient-dev
b) pip install wheel
c) pip install mysqlclient
while trying to migrate following error encountered. tried with all the option available on stakoverflow, i found none of them working.
Error:
"
(envFusion) worker#server:~/fusion/fusionerp$ python manage.py makemigrations
Traceback (most recent call last):
File "/home/worker/fusion/envFusion/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 15, in
import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'
The above exception was the direct cause of the following exception:
.
.
File "/home/worker/fusion/envFusion/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 17, in
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient? "
with ubuntu 20.04 deafult python version is 3.8 and connecting to mysql with 3.8 is a bit challenge so you need to downgrade python to 3.6 or downgrade to ubuntu 18 if you are starting a fresh.
Found a tutorial to downgrade using alternatives and it worked. But had my other dependencies where i need python >= 3.7. So i had no other choice but refresh my ubuntu 20.04 on vps and configured with postgres. it is working way better than i expected with apache2/restframework/redis.
i suppose that i have a solution to this problem, so when we use a Ubuntu OS, i think that we need to run the next commands to work that package:
sudo apt-get install default-libmysqlclient-dev -y
sudo apt-get install gcc -y
pip3 install mysqlclient
Even if you already tried the first one, it might work. I have a Dockerfile in my GitHub of a Django app, if you want to test it later to see if it works on your machine, it took me a week to find the problem and deploy this app to Oracle Cloud because of this error.
Dockerfile

Problem installing mysqlclient for Django project (macOS)

When connecting my Django project to a database hosted on a remote VM, when I run python3 manage.py run server I get the error
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
when I try python3 -m pip install mysqlclient to resolve the dependency, I get the error
ERROR: Could not find a version that satisfies the requirement mysqlclient (from versions: 1.3.0, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.3.5, 1.3.6, 1.3.7, 1.3.8, 1.3.9, 1.3.10, 1.3.11rc1, 1.3.11, 1.3.12, 1.3.13, 1.3.14, 1.4.0rc1, 1.4.0rc2, 1.4.0rc3, 1.4.0, 1.4.1, 1.4.2, 1.4.2.post1, 1.4.3, 1.4.4, 1.4.5, 1.4.6, 2.0.0, 2.0.1, 2.0.2, 2.0.3)
ERROR: No matching distribution found for mysqlclient
I'd like to understand what is causing these issues and how to resolve it.
Thanks in advance!
I got it to work! For anyone facing the same issues run brew install mysql then try python3 manage.py runserver
For what it's worth, you might try installing the mysql-connector-c first. It seems heavy-handed to have to install MySQL to use a driver.
From:
How to install Python MySQLdb module using pip?
Mac OS
brew install mysql-connector-c
if that fails, try
brew install mysql

Fresh python 3.7 / django 2.2.1 installation not recognising that mysqlclient is installed

I have a brand new django 2.2.1 project I have just installed into a python 3.7 virtualenv on OS X (10.14.4). After some frustrations I got mysqlclient to install but when I run the django dev server it doesn't recognise that it is installed:
Here are the steps I've taken so far:
brew install mysql
pipenv --three
pipenv install django==2.2.1
pipenv install mysqlclient
brew uninstall mysql
brew install mysql-connector-c
pipenv install mysqlclient
brew unlink mysql-connector-c
brew install mysql
django-admin startproject projectname
Now, the only change I have made to the out-of-the-box django installation is to change the default database backend to django.db.backends.mysql and when I run the django server I get the following:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
However, going back to the virtualenv and doing "pip install mysqlclient" gives:
Requirement already satisfied: mysqlclient in /Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages (1.4.2.post1)
Any ideas why django can't see the mysqlclient installed in the virtualenv? I can confirm that all of the above has been run in the virtualenv. I suspect it has something to do with the faff that OSX makes you go through to install it but I'm not sure how to pick it apart.
I have also tried to replace mysqldb with pymysql as suggested here but that causes the server to give a different error:
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
Mysql is a given for this project so use of an alternative backend is not an option. Any help very gratefully received, many thanks in advance.
UPDATE: Output of pip freeze:
Django==2.2.1
django-auth-ldap==1.7.0
Markdown==3.1
mysql-connector-python==8.0.16
mysqlclient==1.4.2.post1
protobuf==3.7.1
pyasn1==0.4.5
pyasn1-modules==0.2.5
PyMySQL==0.9.3
pyodbc==4.0.26
python-ldap==3.2.0
pytz==2019.1
simplejson==3.16.0
six==1.12.0
sqlparse==0.3.0
xmltodict==0.12.0
UPDATE 2:
Following conor's post (thanks conor) I started again with a fresh virtualenv and again successfully installed mysqlclient1.4.2.post1. Here's the output from pip freeze now:
Django==2.2.1
mysqlclient==1.4.2.post1
pytz==2019.1
sqlparse==0.3.0
Still getting the same error when I do python manage.py runserver though:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
UPDATE 3:
Thought I would try importing the MySQLdb module on the python command line to see if I can get any more insight and it returns the following:
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
from . import _mysql
ImportError: dlopen(/Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Symbol not found: _mysql_affected_rows
Referenced from: /Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
Expected in: flat namespace
in /Users/<username>/.local/share/virtualenvs/<projectname>-KrUE_JNo/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
SOLVED
So it looks like the issue was that on initial installation the mysqlclient library had compiled against the wrong version of mysql (not sure how that happened), so I had to force it to recompile.
Here are the steps:
brew uninstall mysql
brew uninstall myysql-connector-c
pipenv uninstall mysqlclient
brew install mysql-connector-c
At this point we need to update /usr/local/bin/mysql_config as per the instructions that conor linked to (thanks again conor), i.e. change the line that read
libs="$libs -l "
to
libs="$libs -lmysqlclient -lssl -lcrypto "
Then, to fix the resultant "library not found for -lssl" error I used the answer from this question:
export PATH="/usr/local/opt/openssl/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
Then finally force mysqlclient to recompile and reinstall mysql:
pip install --force-reinstall --ignore-installed --no-binary :all: mysqlclient
brew unlink mysql-connector-c
brew install mysql
Thanks to everyone who took time to help out!
If you're on macos do this
$ brew uninstall mysql
$ brew install mysql-connector-c
$ brew unlink mysql-connector-c
$ brew install mysql
$ pip install mysql-python
and follow the instructions here: https://pypi.org/project/mysqlclient/
Downgrading from python 3.7.4 to python 3.6 solved the issue for me on windows 10.

Apache superset on Mac osx

Hi all I am tried to install the superset on OSX using the Python3. After the installation finished when I tried to add the Database using the mysql:// it said error No Module name MySQLDb. I tried to explore how to solved this, one of tutorial said try to install mysqlclient using pip3 install mysqlclient failed to install with error code mysql.h not found.
Than I following another tutorial used the mysql-connector. After I installed it, finally I can connect to mysql DB and insert table to the system. But when I tried to run the analysis from superset it said no data. Also I tried using SQL Lab and got the error args.
Updated: on my superset currently I am used the mysql+mysql-connector as URI Database connected properly, but when I tested to run a query it said execute() got an unexpected keyword argument 'args'. How to solve this?
Anyone have experience with this problem?
Thanks
Here is what works for me:
brew install python || true
brew install mysql || true
# Required for mysqlclient, see brew info openssl
echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
# And now it works
pip3 install mysqlclient
Cheers!
Finally I got it working now.
What I am doing is reinstall the superset, run the brew install mysql-connector-c than run pip install mysqlclient

Django: cannot connect to mysql

I am new to Django and I try to follow the official tutorial. since I want to connect to mysql (installed on my computer, and i checked mysql module does exit in python command line), I set the ENGINE in setting.py to be django.db.backends.mysql . and then I tried to run
python manage.py syncdb
then I got error message like this:
Error loading MySQLdb module
and I cannot run
pip install mysql-python
the error msg is:
Unable to find vcvarsall.bat
so what is this error? and honestly I am not sure about the difference between mysql-python and mysql-connector-python. Since i tried with "pip install mysql-connector-python" and it tells me that requirement already satisfied...
You need to download the windows binary installer for the MySQL drivers for Python. Installing from source will not work since you do not have the development headers in Windows.
You need to install the mysql python connector
sudo apt-get install python-mysqldb