Celery, Django and PyMySQL

1.1k views Asked by At

I have a django application hooked to a MySQL database, which uses PyMySQL rather than MySQLdb. See the popular answer on this question for more details on that.

Now, it is necessary for me to create a periodic task using Celery. I have followed the documentation for periodic tasks here.

When I run the command celery -A proj beat I get the following error:

  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/forms.py", line 17, in <module>
    from django.contrib.auth.models import User
  File "/usr/local/lib/python3.4/dist-packages/django/contrib/auth/models.py", line 48, in <module>
    class Permission(models.Model):
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py", line 96, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/base.py", line 264, in add_to_class
    value.contribute_to_class(cls, name)
  File "/usr/local/lib/python3.4/dist-packages/django/db/models/options.py", line 124, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/usr/local/lib/python3.4/dist-packages/django/db/__init__.py", line 34, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 198, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 113, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", line 17, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

It compains about a MySQLdb module, but I don't think I am using MySQLdb. Could anybody offer any advice on how to solve this problem?

2

There are 2 answers

0
tiradoe On

I ran into this same issue and was able to fix it by adding the following to the celery app:

import pymysql
pymysql.install_as_MySQLdb()
2
qin fei On

Install suitable mysql client for you computer operate system will fix this problem.:) in python 2.x you should: sudo pip install MySQL-python in python 3.x you should: pip3 install mysqlclient