Forums

Using mySql with python 3.4

I have recently been porting a Django site from python 2.7 to python 3.4 but I'm running into some problems with getting MySQL working...

First of all I'm using a virtualenv for all the python related files and I'm trying to get this to work with Django 1.7

First I had to make a few changes to the WSGI file to get it to work with python 3:

import os
import sys

activate_this = '/home/janis/.virtualenvs/footbagsite/bin/activate_this.py'
with open(activate_this) as f:
    code = compile(f.read(), activate_this, 'exec')
    exec(code, dict(__file__=activate_this))


## assuming your django settings file is at '/home/janis/dev-site/footbag_site/settings.py'
path = '/home/janis/footbagsite/dev-site/'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'footbag_site.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

First I started with what was suggested at the documentation here: https://www.pythonanywhere.com/wiki/UsingMySQL I tried installing the mysql connector with:

pip3.4 install https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.0.2.tar.gz

Along with the associated changes to the Django settings: 'ENGINE': 'mysql.connector.django',#backend from mysql. However this didn't work as the mysql module was not being imported successfully, I would post the stacktrace but the error logging is no longer working for me.

I also tried using pip install mysqlclient but had problems with this. I get a 500 server error but nothing appears in the logs for that error. From that point onwards I'm getting 500 server errors but nothing is appearing in the logs anymore. I'm not sure how to proceed from here, any advice would be appreciated.

Also the Django documentation suggests that you install MySQL-python. The problem with this is that when you try pip3.4 install MySQL-python you get an error instaling because it can't find ConfigParser:

File "/home/janis/.virtualenvs/footbagsite/build/MySQL-python/setup.py", line 13, in <module>
from setup_posix import get_config
File "/home/janis/.virtualenvs/footbagsite/build/MySQL-python/setup_posix.py", line 2, in <module>
from ConfigParser import SafeConfigParser
ImportError: No module named 'ConfigParser'

Now I know ConfigParser was changed to configparser for PEP8 compliance, is there some way of doing something like:

import configparser as ConfigParser

Or is the incompatibility more than just the name? Would six.moves take care of the problems?

Is it possible to use some other MySQL package like https://github.com/PyMySQL/PyMySQL?

Hi janis -- sorry for the slow reply!

From the latest version of the Django docs, it looks like they're currently recommending mysqlclient for Python 3, so it's probably best if we try to track down the problems you were having with that.

Could you try again with it and post the errors here? (It may help to reload the web app to force the errors to show.)

I just installed mysqlclient into my virtualenv.

I changed the database settings to:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'janis$footbag-db-dev',
        'USER': 'janis',
        'PASSWORD': 'xxx',
        'HOST': 'mysql.server',
    }
}

This gives me a 500 error when I attempt to access the page, however nothing at all shows up in the error log for this.

Turns out the issue was that I didn't run syncdb again after changing the stuff around.

The combination of mysqlclient along with the settings above appears to have been successful.

That's excellent news. I wonder why you didn't get anything in the error logs, though. Was it a Django 500 error page? Or a PythonAnywhere one?

When I was using the package installed by pip3.4 install https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.0.2.tar.gz it was a pythonanywhere "unhandled exception" page but nothing showed up in the log.

That's really odd. You checked both the error and the server logs, right?

One possibility -- perhaps you were making that change at the time we happened to rotate your log files -- then the error would have been in /var/log/XXX.error.log.1 instead of /var/log/XXX.error.log. It's sometimes worth reloading the web app if you don't see what you expect in the logs, as that will force it to write to the new rotated ones.

I checked both logs and also reloaded the web app so I'm not too sure what caused that problem with mysql-connector-python.

Very odd indeed. I don't think there's anything we can do to track it down now (I've scanned the logs, and while there were a few problems with some web apps' access logs running behind, it didn't affect error or server logs). I guess all I can say is, please do let us know if it happens again :-S

I'll certainly let you know if anything like that happens again.

Making my first Py3 and Django 1.9 project with Virtualenvs.

Really, really, really glad this post exists!

Somebody should update the Virtualenv tutorial with pip3 and mysqlclient!

Rich.

Do the instructions here look correct? https://help.pythonanywhere.com/pages/UsingMySQL If so I'll link to that from the django/virtualenv help pages