2

I want to use SQLAlchemy with MySQL database. I've created the Engine and tried to connect but I get an error as follows.

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
Engine = create_engine('mysql+mysqldb://dbuser:pass@localhost/mydb')
Base = declarative_base(Engine)

That fails with the error:

File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/connectors/mysqldb.py", line 57, in dbapi
return __import__('MySQLdb')
ImportError: No module named MySQLdb

Searching for that I found that you have to have to install MySQLdb for python to play nice with MySQL. So trying to install it with either pip-3.2 or easy_install-3.2 fails as below.

sudo pip-3.2 install MySQL-python
Downloading/unpacking MySQL-python
Running setup.py egg_info for package MySQL-python
Traceback (most recent call last):
  File "<string>", line 16, in <module>
  File "/tmp/pip-build/MySQL-python/setup.py", line 14, in <module>
    from setup_posix import get_config
  File "setup_posix.py", line 2, in <module>
    from ConfigParser import SafeConfigParser
ImportError: No module named ConfigParser
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/tmp/pip-build/MySQL-python/setup.py", line 14, in <module>
from setup_posix import get_config
 File "setup_posix.py", line 2, in <module>
from ConfigParser import SafeConfigParser
ImportError: No module named ConfigParser

I then figure I have to install configparser so when I try to do so it also fails

Original exception was:
Traceback (most recent call last):
 File "<string>", line 3, in <module>
 File "/usr/local/lib/python3.2/dist-packages/distribute-0.6.34-py3.2.egg/setuptools/__init__.py", line 2, in <module>
from setuptools.extension import Extension, Library
File "/usr/local/lib/python3.2/dist-packages/distribute-0.6.34-py3.2.egg/setuptools/extension.py", line 2, in <module>
import distutils.core
File "/usr/lib/python3.2/distutils/core.py", line 19, in <module>
from distutils.config import PyPIRCCommand
File "/usr/lib/python3.2/distutils/config.py", line 8, in <module>
from configparser import ConfigParser
 File "configparser.py", line 122

So, my original issue is trying to get Python to work with MySQL. But trying to resolve that I can't be able to install MySQLdb.

EDIT Gone ahead as instructed by @robertklep below and installed pymsql3.

sudo pip-3.2 install pymysql3

Regards,

0

2 Answers 2

6

MySQLdb doesn't have Python 3 support. Try an alternative which has, like PyMySQL (which is supported by SQLAlchemy as well).

EDIT: there's also this project.

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

3 Comments

Thanks for that. Wasted a whole day yesterday. I see there is pymysql3 and pymysql_sa and it states that pymysql_sa is a PyMySQL dialect for SQLAlchemy. So which should I install? Think the pymysql_sa has more advantages to pymysql3 given that am working specifically with sqlalchemy?
found out the "hardway". Tried installing pymysql_sa and it failed. Most likely coz its not compatible with Python3. So yeah, stuck with pymysql3. Thanks.
I tried using pymysql3 and I run into problems which as pointed out there is a bug in sqlalchemy. Do you have a workaround?
3

https://www.pythonanywhere.com/wiki/UsingMySQL

pip install --user https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-1.1.6.tar.gz

Then, update your settings.py to use the oracle django backend, "mysql.connector.django":

DATABASES = {
    'default': {
        'ENGINE': 'mysql.connector.django',
         ...

1 Comment

After trying this, I got "django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend."

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.