745π
You need to use one of the following commands. Which one depends on what OS and software you have and use.
- easy_install mysql-python (mix os)
- pip install mysql-python (mix os/ python 2)
- pip install mysqlclient (mix os/ python 3)
- apt-get install python-mysqldb (Linux Ubuntu, β¦)
- cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD)
- yum install MySQL-python (Linux Fedora, CentOS β¦)
For Windows, see this answer: Install mysql-python (Windows)
- [Django]-In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?
176π
β¦and remember there is no MySQLdb for python3.x
(I know the question is about python2.x but google rates this post quite high)
EDIT: As stated in the comments, thereβs a MySQLdbβs fork that adds Python 3 support: github.com/PyMySQL/mysqlclient-python
- [Django]-South migration: "database backend does not accept 0 as a value for AutoField" (mysql)
- [Django]-What does on_delete do on Django models?
- [Django]-Django url tag multiple parameters
98π
if your python version is 3.5
, do a pip install mysqlclient
, other things didnβt work for me
- [Django]-Django β Rotating File Handler stuck when file is equal to maxBytes
- [Django]-Django select_for_update cannot be used outside of a transaction
- [Django]-Allowing only super user login
49π
mysqldb
is a module for Python that doesnβt come pre-installed or with Django. You can download mysqldb
here.
- [Django]-Complete django DB reset
- [Django]-Django: Open uploaded file while still in memory; In the Form Clean method?
- [Django]-Running Django with FastCGI or with mod_python
38π
Note this is not tested for python 3.x
In CMD
pip install wheel
pip install pymysql
in settings.py
import pymysql
pymysql.install_as_MySQLdb()
It worked with me
- [Django]-Django rest framework change primary key to use a unqiue field
- [Django]-What is "load url from future" in Django
- [Django]-Django-Bower + Foundation 5 + SASS, How to configure?
36π
I am at ubuntu (linux) and what worked for me was
sudo apt-get install python3-dev default-libmysqlclient-dev build-essential
and then finally
pip install mysqlclient
- [Django]-Django Rest Framework Conditional Field on Serializer
- [Django]-Django testing: Test the initial value of a form field
- [Django]-How does one make logging color in Django/Google App Engine?
35π
For anyone coming to this page when trying to find a solution for sqlalchemy
, all you need to do is:
pip install PyMySQL
And adjust your connection string to use PyMySQL, from mysql://
to mysql+pymysql://
.
- [Django]-*_set attributes on Django Models
- [Django]-How to change User representation in Django Admin when used as Foreign Key?
- [Django]-What's the idiomatic Python equivalent to Django's 'regroup' template tag?
- [Django]-Django-allauth: Linking multiple social accounts to a single user
- [Django]-Migrating Django fixtures?
- [Django]-Django: show the count of related objects in admin list_display
23π
for Windows :
pip install mysqlclient pymysql
then:
import pymysql
pymysql.install_as_MySQLdb()
for python 3 Ubuntu
sudo apt-get install -y python3-mysqldb
- [Django]-Resource temporarily unavailable using uwsgi + nginx
- [Django]-How can I upgrade specific packages using pip and a requirements file?
- [Django]-Django β how to visualize signals and save overrides?
20π
pip install PyMySQL
and then add this two lines to your Project/Project/init.py
import pymysql
pymysql.install_as_MySQLdb()
Works on WIN and python 3.3+
- [Django]-Django substr / substring in templates
- [Django]-How to upload a file in Django?
- [Django]-How to run a celery worker with Django app scalable by AWS Elastic Beanstalk?
- [Django]-Django: Grab a set of objects from ID list (and sort by timestamp)
- [Django]-Separation of business logic and data access in django
- [Django]-How to understand lazy function in Django utils functional module
13π
If pip install mysqlclient produces an error and you use Ubuntu, try:
sudo apt-get install -y python-dev libmysqlclient-dev && sudo pip install mysqlclient
- [Django]-Why does Django's render() function need the "request" argument?
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
- [Django]-Django: Example of generic relations using the contenttypes framework?
12π
For Python 3.6+
sudo apt-get install libmysqlclient-dev
pip3 install mysqlclient
does the trick
- [Django]-Django Admin app or roll my own?
- [Django]-Gunicorn Connection in Use: ('0.0.0.0', 5000)
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
10π
Python 3.8
sudo apt-get install libmysqlclient-dev
sudo apt-get install -y python3-mysqldb
pip3 install pymysql
settings.py
import pymysql
pymysql.install_as_MySQLdb()
- [Django]-Referencing multiple submit buttons in django
- [Django]-Django logging of custom management commands
- [Django]-Why there are two process when i run python manage.py runserver
7π
I met the same situation under windows, and searched for the solution.
Seeing this post Install mysql-python (Windows).
It points out installing such a pip environment is difficult, needs many other dependencies.
But I finally know that if we use mysqlclient
with a version down to 1.3.4
, it donβt need that requirements any more, so try:
pip install mysqlclient==1.3.4
- [Django]-Get user profile in django
- [Django]-__init__() got an unexpected keyword argument 'mimetype'
- [Django]-Why is logged_out.html not overriding in django registration?
6π
pip install --user mysqlclient
above works for me like charm for me.I go the error from sqlalchemy actually.
Environment information :
Python : 3.6, Ubuntu : 16.04,conda 4.6.8
- [Django]-Django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
- [Django]-Django 1.8 KeyError: 'manager' on relationship
- [Django]-Django error: got multiple values for keyword argument
6π
I personally recommend using pymysql
instead of using the genuine MySQL connector, which provides you with a platform independent interface and could be installed through pip
.
And you could edit the SQLAlchemy URL schema like this:
mysql+pymysql://username:passwd@host/database
- [Django]-Django limit_choices_to for multiple fields with "or" condition
- [Django]-How to stop autopep8 not installed messages in Code
- [Django]-What is the difference between cached_property in Django vs. Python's functools?
5π
If you are running on Vista, you may want to check out the Bitnami Django stack. It is an all-in-one stack of Apache, Python, MySQL, etc. packaged with Bitrock crossplatform installers to make it really easy to get started. It runs on Windows, Mac and Linux. Oh, and is completely free π
- [Django]-How to completely dump the data for Django-CMS
- [Django]-Django staticfiles not found on Heroku (with whitenoise)
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
5π
- Go to your project directory with
cd
. - source/bin/activate (activate your env. if not previously).
- Run the command
easy_install MySQL-python
- [Django]-How can I avoid "Using selector: EpollSelector" log message in Django?
- [Django]-How to solve "Page not found (404)" error in Django?
- [Django]-Authenticate by IP address in Django
4π
Thanks to derevo but I think thereβs another good way for doing this:
- Download and install ActivePython
- Open Command Prompt
- Type
pypm install mysql-python
- Read the notes specific to this package.
I think pypm
is more powerful and reliable than easy_install
.
- [Django]-How to print BASE_DIR from settings.py from django app in terminal?
- [Django]-Django model one foreign key to many tables
- [Django]-How can i test for an empty queryset in Django?
3π
On OSX these commands worked for me
brew install mysql-connector-c
pip install MySQL-python
- [Django]-Django content-type : how do I get an object?
- [Django]-Django AutoField with primary_key vs default pk
- [Django]-Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist."
3π
For Python 3+ version
install mysql-connector
as:
pip3 install mysql-connector
Sample Python DB connection code:
import mysql.connector
db_connection = mysql.connector.connect(
host="localhost",
user="root",
passwd=""
)
print(db_connection)
Output:
> <mysql.connector.connection.MySQLConnection object at > 0x000002338A4C6B00>
This means, database is correctly connected.
- [Django]-Django: guidelines for speeding up template rendering performance
- [Django]-Sending HTML email in django
- [Django]-Django β {% csrf_token %} was used in a template, but the context did not provide the value
3π
On Debian Buster, the following solution worked for me with python 3.7:
sudo apt-get install libmysqlclient-dev
sudo apt-get install libssl-dev
pip install mysqlclient
- [Django]-Unittest Django: Mock external API, what is proper way?
- [Django]-Django template can't see CSS files
- [Django]-Django Admin Form for Many to many relationship
3π
Python 3
Make sure the import order:
#BAD
import MySQLdb # <------ NOT here
import pymysql
pymysql.install_as_MySQLdb()
#GOOD
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb # <------- HERE!
- [Django]-Django: how to do calculation inside the template html page?
- [Django]-Alowing 'fuzzy' translations in django pages?
- [Django]-Saving ModelForm error(User_Message could not be created because the data didn't validate)
2π
I have tried methods above, but still no module named βMySQLdbβ, finally, I succeed with
easy_install mysql-python
my env is unbuntu 14.04
- [Django]-"<Message: title>" needs to have a value for field "id" before this many-to-many relationship can be used.
- [Django]-Change a field in a Django REST Framework ModelSerializer based on the request type?
- [Django]-Group by Foreign Key and show related items β Django
2π
If your are using SQLAlchemy and the error is in /site-packages/sqlalchemy/dialects/mysql/mysqldb.py
:
from ...connectors.mysqldb import (
MySQLDBExecutionContext,
MySQLDBCompiler,
MySQLDBIdentifierPreparer,
MySQLDBConnector
)
so you may have missed mysqldb connector for SQLAlchemy
and the solution is to re-install sqlalchemy after installing mysql-python
module.
- [Django]-Celery. Decrease number of processes
- [Django]-How to stop autopep8 not installed messages in Code
- [Django]-Django AutoField with primary_key vs default pk
2π
None of the above worked for me on an Ubuntu 18.04 fresh install via docker image.
The following solved it for me:
apt-get install holland python3-mysqldb
- [Django]-ImportError: Failed to import test module:
- [Django]-How do I use django rest framework to send a file in response?
- [Django]-Django Rest Framework β Updating a foreign key
1π
On my mac running Catalina v10.15.2, I had the following MySQLdb version conflict:
ImportError: this is MySQLdb version (1, 2, 5, 'final', 1), but _mysql is version (1, 4, 6, 'final', 0)
To resolve it, I did the following:
pip uninstall MySQL-python
pip install MySQL-python
- [Django]-Get Timezone from City in Python/Django
- [Django]-Update all models at once in Django
- [Django]-Testing nginx without domain name
1π
Faced this issue with mysql.connector.python 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
- [Django]-Django-celery: No result backend configured
- [Django]-Django gunicorn sock file not created by wsgi
- [Django]-Whats the difference between using {{STATIC_URL}} and {% static %}
- [Django]-Adding to the "constructor" of a django model
- [Django]-TypeError: data.forEach is not a function
- [Django]-Group by Foreign Key and show related items β Django
0π
Win10 / Python27
this worked for me:
easy_install mysql-python
all other βpip installβ¦β failed with dependency errors
- [Django]-Images from ImageField in Django don't load in template
- [Django]-Why does Django's render() function need the "request" argument?
- [Django]-How can I temporarily disable a foreign key constraint in MySQL?
0π
When I ran the command apt-get install python-mysqldb, I still encountered the error message below
mysql_config not found when installing mysqldb python interface
So I first had to run the command below
sudo apt-get install libmariadbclient-dev
If you use MySQL instead of MariaDB, run the following instead
Then run
apt-get install python-mysqldb
Then now run
pip install mysqlclient
- [Django]-How to get GET request values in Django?
- [Django]-Django Queryset with year(date) = '2010'
- [Django]-Ignoring Django Migrations in pyproject.toml file for Black formatter
- [Django]-Best way to integrate SqlAlchemy into a Django project
- [Django]-Passing STATIC_URL to file javascript with django
- [Django]-Paginating the results of a Django forms POST request