I am using Python version 2.5.4 and install MySQL version 5.0 and Django. Django is working fine with Python, but not MySQL. I am using it in Windows Vista.
30 Answers
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)
...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
mysqldb
is a module for Python that doesn't come pre-installed or with Django. You can download mysqldb
here.
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
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
.
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.
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 :)
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.
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
pip install mysql-python
orpip install mysqlclient
– Cœursudo apt-get install libmysqlclient-dev
to make it work. – b00r00x0sudo apt-get install libmysqlclient-dev
andpip3 install mysqlclient
worked for me – Umair Ayub