Forums

Index view fails with error "'NoneType' object has no attribute 'converter'"

Hi everyone,

Whenever I start working again on my project and I try to load my index page I get the following error:

File "/home/PrimeResults/.local/lib/python3.3/site-packages/mysql/connector/django/base.py" in value_to_db_datetime
return self.connection.connection.converter._datetime_to_mysql(value)

Exception Type: AttributeError at /reg/ClientRegistration
Exception Value: 'NoneType' object has no attribute 'converter'

If I reload my page via Reload Button, the page works again until my next break. Does anybody has some ideas?

My guess is that your connection is being closed due to inactivity -- database connections close after a couple of minutes. If you're writing your own DB connectivity directly, you'll need to code to allow for that. Alternatively, you could use something like SQLAlchemy, which will handle that kind of thing for you.

OK, could you give my some hints how to do that? Maybe some links where to find more information. I'm quite new in that field..

Which web framework are you using?

I'm using Django 1.6 and Python3.3

Hmm, I'm a bit confused then -- Django should handle all of that for you. Could you give a bit more of the stack trace?

This is a bug report I found, which seems to show same error I always get:

https://code.djangoproject.com/ticket/21226

AttributeError at /reg/ClientRegistration
'NoneType' object has no attribute 'converter'
Request Method: GET
Request URL:    http://primeresults.pythonanywhere.com/reg/ClientRegistration
Django Version: 1.6
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute 'converter'
Exception Location: /home/PrimeResults/.local/lib/python3.3/site-     packages/mysql/connector/django/base.py in value_to_db_datetime, line 398
Python Executable:  /usr/local/bin/uwsgi
Python Version: 3.3.2
Python Path:    
['/var/www',
'.',
'',
'/home/PrimeResults/.local/lib/python3.3/site-packages',
'/usr/local/lib/python3.3/dist-packages/setuptools-2.1.2-py3.3.egg',
'/usr/local/lib/python3.3/dist-packages/pip-1.5.2-py3.3.egg',
'/var/www',
'/usr/lib/python3.3',
'/usr/lib/python3.3/plat-x86_64-linux-gnu',
'/usr/lib/python3.3/lib-dynload',
'/usr/local/lib/python3.3/dist-packages',
'/usr/lib/python3/dist-packages',
'/home/PrimeResults/project_vitamine']
Server time:    Fri, 11 Apr 2014 18:30:13 +0000

Ok I realized thatbefore I get the NonType error, I first get an error report in the error logfile, which says:

File "/home/PrimeResults/.local/lib/python3.3/site-packages/mysql/connector/connection.py", line 1231, in cursor
2014-04-11 19:23:04,587 :    raise errors.OperationalError("MySQL Connection not available.")
2014-04-11 19:23:04,588 :django.db.utils.OperationalError

I changed to sqlite3 database and the error is gone, so it has to do something with the mysql.connector.django engine.

As long as there is no reliable MySQL engine for python 3 I look for alternatives

Which MySQL engine were you using? The correct one is documented here.

Yes that's the one I was using too. Anyway I switched now to an sqlite3 and MongoDB alternative

OK, so long you have something that works. It's strange, though, the MySQL energy we talk about on that page should work fine.

Problem was reported to MySQL/Oracle long time ago:

http://bugs.mysql.com/bug.php?id=71438

Interesting, thanks. It looks like it's fixed in that bug report, though.

MySQL connector checks connection in is_connect() (.local/lib/python3.3/site-packages/mysql/connector/connection.py) but it doesn't try to reconnect. I add reconnect() in is_connect() and now I have no problem with "MySQL Connection not available."

before:

def is_connected(self):
    try:
        self.cmd_ping()
    except:
        return False  # This method does not raise
    return True

after:

def is_connected(self):
    try:
        #print("DEBUG: cmd_ping()")
        self.cmd_ping()
    except:
        try:
            print("DEBUG: reconnect()")
            #self.reconnect(attempts=3, delay=1)
            self.reconnect()
        except:
            print("DEBUG: is_connected(): False")
            return False  # This method does not raise
    print("DEBUG: is_connected(): True")
    return True

You can see DEBUG messages in [YOUR_LOGIN].pythonanywhere.com.server.log .

Now is_connected() looks similar to ping() so maybe it can be done this way:

def is_connected(self):
    try:
        self.ping(True) # ping() in place of cmd_ping()
    except:
        return False  # This method does not raise
    return True

Thera are two problems.

  1. is_connected() (and ping()) calls reconnect() and reconnect() calls is_connected() - it is recursion. It can create almost infinitive loop if mysql is turned off. Python will raise exception "maximum recursion depth exceeded" but after almost 1000 recursions.

  2. is_connected() is not the best place for reconnect(). I think is_connected() shouldn't make reconnection. But it is the simplest solution.

I'm getting the same problem. Also using the documented setup prescribed above.

At this point I'm having so many issues with mysql, py3.3, and django 1.6 & 1.7 that I may just roll back to 2.7 and 1.6.5. I'm spending too much time fiddling w frameworks and installations, and not enough time making my app work.

I'm having the same issue too with python3.3, django1.6, and mysql

From the bit of reading I've done, it looks like Django on Python 3 with MySQL isn't really well supported at the moment. MySQLdb for Python 3 is still in alpha. As an alternative, the Django docs suggest using MySQL Connector/Python, but also state that it may not support the most recent versions of Django.

Okay, thanks.

I really recommend to have a look at MonogoDB alternatives. The PyMongo Library works really nicely (http://api.mongodb.org/python/current/)

You can use MongoDB as your main Django database. Check this out: http://staltz.github.io/djangoconfi-mongoengine/#/

I fell on the exact same bug! I was following the tutorial here : https://www.pythonanywhere.com/wiki/VirtualEnvForNewerDjango

I think this tut is a bit misleading, or at least should warn that Python3 will probably not work properly with Django on Pythonanywhere.

It can work (on my local branch, it's working perfectly with MySQL, Python3.4, Django1.7) but with pyMySQL and not the connector from Oracle. But you can't use pyMYSQL on pythonanywhere

It looks like you can install pyMySQL, though I don't know if it works -- just run this from a bash console:

pip3 install --user pyMYSQL