7

I am working on projects based on Django 1.7 and Python 3.4. However, I had problems installing MySQL/Connector Python with pip3.

According to this document, MySQL/Connector Python supports Python 3. I used to install MySQL-python in Python with command pip install MySQL-python.

This download page only provides .deb files for installation on Ubuntu (btw, the installation also has conflict problems)

I tried to install with:

pip3 install mysql-connector-python --allow-external mysql-connector-python

No error messages. But when I run the Django app, I got the following error message:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

Question: So, how do I install MySQL/Connector Python into a virtual environment with pip3? Or is it supposed to be installed into the system, instead of a virtual environment?

6
  • What is the actual problem? Commented Oct 26, 2014 at 14:34
  • I re-edited the question Commented Oct 26, 2014 at 14:41
  • 1
    Please post the content of the DATABASES section of your settings.py file Commented Oct 26, 2014 at 14:47
  • DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': '<db_name>', 'USER': '<user_name>', 'PASSWORD': '<user_password>', 'HOST': '127.0.0.1', 'PORT': '3306', } } Commented Oct 26, 2014 at 14:48
  • BTW, I tried to change 'ENGING' to 'django.connector.mysql', but Django complaints "No module named 'django.connector' ". Commented Oct 26, 2014 at 14:50

2 Answers 2

23

If you read the documentation, you will see that the native MySQLdb driver doesn't support Python 3. You have two options:

A mysqldb fork

There is a fork that supports Python 3. Read its Github repo to know how to install with your system. For Ubuntu do apt-get install python-mysqldb

MySQL connector

Install from venv as you did with pip install mysql-connector-python --allow-external mysql-connector-python. Then read their documentation for Django and modify your settings.py file to have it like:

DATABASES = {
    'default': {
        'NAME': 'user_data',
        'ENGINE': 'mysql.connector.django',
        'USER': 'mysql_user',
        'PASSWORD': 'priv4te',
        'OPTIONS': {
          'autocommit': True,
        },
    }
}

Pay attention to the connector value mysql.connector.django :-)

Sign up to request clarification or add additional context in comments.

3 Comments

Python-mysqldb installed, but no luck.
@Lucio please, update your answer: mysql-connector-python >= 8.0.13 has a bug, making it impossible to work with Django: code.djangoproject.com/ticket/30469 As a workaround, add 'use_pure': True to the 'OPTIONS'.
4

The version of mysql-connector that pip installs doesn't work with Django 1.8. There is a fixed version on github. The command to install from there is

pip install --allow-all-external git+git://github.com/multiplay/mysql-connector-python

That one works with Django 1.8.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.