284
pip install pymysql
Then, edit the __init__.py file in your project origin dir(the same as settings.py)
add:
import pymysql
pymysql.install_as_MySQLdb()
27
Faced same problem after migrating to python 3. Apparently, MySQL-python is incompatible, so as per official django docs, installed mysqlclient using pip install mysqlclient
on Mac. Note that there are some OS specific issues mentioned in docs.
Quoting from docs:
Prerequisites
You may need to install the Python and MySQL development headers and libraries like so:
sudo apt-get install python-dev default-libmysqlclient-dev
# Debian / Ubuntu
sudo yum install python-devel mysql-devel
# Red Hat / CentOS
brew install mysql-connector-c
# macOS (Homebrew) (Currently, it has bug. See below)On Windows, there are binary wheels you can install without MySQLConnector/C or MSVC.
Note on Python 3 : if you are using python3 then you need to install python3-dev using the following command :
sudo apt-get install python3-dev
# debian / Ubuntu
sudo yum install python3-devel
# Red Hat / CentOSNote about bug of MySQL Connector/C on macOS
See also: https://bugs.mysql.com/bug.php?id=86971
Versions of MySQL Connector/C may have incorrect default configuration options that cause compilation errors when
mysqlclient-python
is installed. (As of November 2017, this is known to be true for homebrew’smysql-connector-c
and official package)Modification of
mysql_config
resolves these issues as follows.Change
# on macOS, on or about line 112:
# Create options
libs="-L$pkglibdir"
libs="$libs -l "
to
# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"
An improper ssl configuration may also create issues; see, e.g,
brew info openssl
for details on macOS.Install from PyPI
pip install mysqlclient
NOTE: Wheels for Windows may be not released with source package. You should pin version in your
requirements.txt
to avoid trying to install newest source package.Install from source
- Download source by
git clone
or zipfile.- Customize
site.cfg
python setup.py install
- [Django]-Annotate with latest related object in Django
- [Django]-How does the get_or_create function in Django return two values?
- [Django]-Include intermediary (through model) in responses in Django Rest Framework
19
For pip
pip install pymysql
For pip3 you should use
python3 -m pip install PyMySQL
Then, edit the init.py file in your project origin directory (the same as settings.py). Add:
import pymysql
pymysql.install_as_MySQLdb()
- [Django]-Get object by field other than primary key
- [Django]-Django: TemplateDoesNotExist (rest_framework/api.html)
- [Django]-Where does pip install its packages?
- [Django]-Django Rest Framework pagination extremely slow count
- [Django]-How to delete project in django
- [Django]-Complete django DB reset
12
Use the below command to solve your issue,
pip install mysql-python
apt-get install python3-mysqldb libmysqlclient-dev python-dev
Works on Debian
- [Django]-How can I check the size of a collection within a Django template?
- [Django]-Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist."
- [Django]-Getting Django admin url for an object
10
If you already have mysqlclient
installed (i.e. you see Requirement already satisfied
) and are getting Error loading MySQLdb Module
, the following worked for me:
pip uninstall mysqlclient
export LDFLAGS="-L/usr/local/opt/openssl/lib"
andexport CPPFLAGS="-I/usr/local/opt/openssl/include"
as explained herepip install mysqlclient
That reinstalls mysqlclient
and for whatever reason solved my problem.
- [Django]-CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False
- [Django]-How to get form fields' id in Django
- [Django]-How does the get_or_create function in Django return two values?
3
For MAC os user:
I have faced this issue many times. The key here is to set these environment variables before installing mysqlclient by pip command. For my case, they are like below:
export LDFLAGS="-L/usr/local/opt/protobuf@3.7/lib"
export CPPFLAGS="-I/usr/local/opt/protobuf@3.7/include"
- [Django]-Django rest framework: query parameters in detail_route
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
- [Django]-How to serve media files on Django production environment?
2
edit the init.py file in your project origin directory
import pymysql
pymysql.install_as_MySQLdb()
- [Django]-Using Django time/date widgets in custom form
- [Django]-What are the differences between django-tastypie and djangorestframework?
- [Django]-Django – Static file not found
2
Installing mysqlclient
using anaconda
is pretty simple and straight forward
.
conda install -c bioconda mysqlclient
and then, install pymysql
using pip.
pip install pymysql
- [Django]-How can I list urlpatterns (endpoints) on Django?
- [Django]-Django-social-auth django-registration and django-profiles — together
- [Django]-Annotate a queryset with the average date difference? (django)
2
I am using python 3 in windows.
I also faced this issue. I just uninstalled ‘mysqlclient’ and then installed it again.
It worked somehow
- [Django]-Constructing Django filter queries dynamically with args and kwargs
- [Django]-Add rich text format functionality to django TextField
- [Django]-What's the idiomatic Python equivalent to Django's 'regroup' template tag?
1
Inspired by @elad silver’s answer (thanks).
For OSx (Monterey) this worked for me:
- Install protobuf
brew install protobuf
- edit your .bashrc, or .zshenv file with these lines:
export LDFLAGS="${LDFLAGS} -L/usr/local/opt/protobuf/lib"
export CPPFLAGS="${CPPFLAGS} -I/usr/local/opt/protobuf/include"
- Reinstall
mysqlclient
pip uninstall mysqlclient
pip install mysqlclient
- [Django]-How to properly use the "choices" field option in Django
- [Django]-Remove Labels in a Django Crispy Forms
- [Django]-Django form fails validation on a unique field
0
I had this issue just recently even with using the python 3 compatible mysqlclient
library and managed to solve my issue albeit in a bit of an unorthodox manner. If you are using MySQL 8, give this a try and see if it helps!
I simply made a copy of the libmysqlclient.21.dylib
file located in my up-to-date installation of MySQL 8.0.13 which is was in /usr/local/mysql/lib
and moved that copy under the same name to /usr/lib
.
You will need to temporarily disable security integrity protection on your mac however to do this since you won’t have or be able to change permissions to anything in /usr/lib
without disabling it. You can do this by booting up into the recovery system, click Utilities on the menu at the top, and open up the terminal and enter csrutil disable
into the terminal. Just remember to turn security integrity protection back on when you’re done doing this! The only difference from the above process will be that you run csrutil enable
instead.
You can find out more about how to disable and enable macOS’s security integrity protection here.
- [Django]-Suddenly when running tests I get "TypeError: 'NoneType' object is not iterable
- [Django]-Django project models.py versus app models.py
- [Django]-Logging in Django and gunicorn
- [Django]-AngularJS with Django – Conflicting template tags
- [Django]-How to run own daemon processes with Django?
- [Django]-Django urlsafe base64 decoding with decryption
0
To solve the problem run: pip install mysql-connector-python==8.0.23
Faced this issue with mysql.connector version 8.0.24 on mac(if code base is same then the issue should happen in windows as well). This file on line 51 imports "from django.db.backends.mysql.base import DatabaseWrapper as MySQLDatabaseWrapper". The imported file has following code 14-20(exact code and error that you received is part of code
try:
import MySQLdb as Database
except ImportError as err:
raise ImproperlyConfigured(
'Error loading MySQLdb module.\n'
'Did you install mysqlclient?'
) from err
The error is formed here. Not sure why this import keeps coming back in different version of mysql connector but 8.0.23 does not have the import, so I reverted to that version and error was gone… This is incase you wish to continue to work with mysql.connector.python. Not sure of logic and why this error is open for last 5 years…
- [Django]-Django CSRF Cookie Not Set
- [Django]-Django Sitemaps and "normal" views
- [Django]-Django template tag to truncate text
0
[NEW]
Try pip install mysqlclient
if not working, and if you are getting below error
MySQLdb/_mysql.c(29): fatal error C1083: Cannot open include file: 'mysql.h': No such file or directory
Then visit this site and download the supported wheel.
Now how to know the supported wheel. For example there are multiple wheel files like below:
mysqlclient-1.4.6-cp36-cp36m-win_amd64.whl
See the bold version cp36 which indicates CPython version 3.6
Just install it and this will resolve your issue.
Oh! now if you come across the another problem how to install the wheel file then just refer below line.
python -m pip install "C:\Users\Abhijit\Downloads\mysqlclient-1.4.6-cp36-cp36m-win_amd64.whl"
- [Django]-Django: How can I create a multiple select form?
- [Django]-How to pass information using an HTTP redirect (in Django)
- [Django]-Django rest framework, use different serializers in the same ModelViewSet
0
For now because default mysql server on Debian is MariaDB.
user@cl1-preprod ~ % mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 39
apt-get install mariadb
doesn’t exist. To install it you need to install this package apt-get install mariadb-server
but this is only the mysql server the client is also required apt-get install mariadb-client
.
Then pip install mysqlclient
user@cl1-preprod ~ % apt-get remove libmysqlclient-dev python-dev
Lecture des listes de paquets... Fait
Construction de l'arbre des dépendances
Lecture des informations d'état... Fait
Le paquet « libmysqlclient-dev » n'est pas installé, et ne peut donc être supprimé
Le paquet « python-dev » n'est pas installé, et ne peut donc être supprimé
- [Django]-How to format time in django-rest-framework's serializer?
- [Django]-Django count RawQuerySet
- [Django]-Pylint "unresolved import" error in Visual Studio Code
-1
On Windows, I ran
pip install mysqlclient
instead of
pip install pymysql
and it worked fine.
- [Django]-Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist."
- [Django]-How to use pdb.set_trace() in a Django unittest?
- [Django]-Django Rest Framework pagination extremely slow count
- [Django]-Why is factory_boy superior to using the ORM directly in tests?
- [Django]-Getting TypeError: __init__() missing 1 required positional argument: 'on_delete' when trying to add parent table after child table with entries
- [Django]-Suppress "?next=blah" behavior in django's login_required decorator
- [Django]-How do I get the object if it exists, or None if it does not exist in Django?
- [Django]-How to add multiple objects to ManyToMany relationship at once in Django ?
- [Django]-Django Rest Framework pagination extremely slow count