Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)

I'm not able to deploy Django to AWS Lambda using Zappa connecting to RDS, it is showing me below error:

NameError: name '_mysql' is not defined:

Here are my AWS CloudWatch logs:

name '_mysql' is not defined: NameError Traceback (most recent call last): File "/var/task/handler.py", line 609, in lambda_handler return LambdaHandler.lambda_handler(event, context) File "/var/task/handler.py", line 240, in lambda_handler handler = cls() File "/var/task/handler.py", line 146, in init wsgi_app_function = get_django_wsgi(self.settings.DJANGO_SETTINGS) File "/var/task/zappa/ext/django_zappa.py", line 20, in get_django_wsgi return get_wsgi_application() File "/var/task/django/core/wsgi.py", line 12, in get_wsgi_application django.setup(set_prefix=False) File "/var/task/django/init.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/var/task/django/apps/registry.py", line 114, in populate app_config.import_models() File "/var/task/django/apps/config.py", line 211, in import_models self.models_module = import_module(models_module_name) File "/var/lang/lib/python3.6/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 955, in _find_and_load_unlocked File "", line 665, in _load_unlocked

File "", line 678, in exec_module File "", line 219, in _call_with_frames_removed File "/var/task/django/contrib/auth/models.py", line 2, in from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "/var/task/django/contrib/auth/base_user.py", line 47, in class AbstractBaseUser(models.Model): File "/var/task/django/db/models/base.py", line 121, in new new_class.add_to_class('_meta', Options(meta, app_label)) File "/var/task/django/db/models/base.py", line 325, in add_to_class value.contribute_to_class(cls, name) File "/var/task/django/db/models/options.py", line 208, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "/var/task/django/db/init.py", line 28, in getattr return getattr(connections[DEFAULT_DB_ALIAS], item) File "/var/task/django/db/utils.py", line 207, in getitem backend = load_backend(db['ENGINE']) File "/var/task/django/db/utils.py", line 111, in load_backend return import_module('%s.base' % backend_name) File "/var/lang/lib/python3.6/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/var/task/django/db/backends/mysql/base.py", line 16, in import MySQLdb as Database File "/var/task/MySQLdb/init.py", line 24, in version_info, _mysql.version_info, _mysql.file NameError: name '_mysql' is not defined

And below is my setting.py config:

DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.mysql',

        'NAME': 'raags-instance', # DB Instance created in AWS RDS

        'USER': 'xyz',

        'PASSWORD': 'passwordpassword',

        'HOST': '*********.us-east-2.rds.amazonaws.com',

        'PORT': 3306,

    }

}

Any solution for this?

1 Answer

0 votes
by (12.4k points)

Here the problem is because the ".mysql" file is missing in the MySQL dB directory.

You can add the below lines in code and try to deploy again, it should work fine.

import pymysql

pymysql.install_as_MySQLdb()

Do checkout aws certification training to learn more about AWS.

For more details on AWS Lambda, you can check out the video tutorial below.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

Browse Categories

...