Forums

Error loading MySQLdb module

I've just converted my sqlite database to a MySql database and now I can't get the application to function. There error log says

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module

When I try and install mysqlclient I get the message that

Requirement already satisfied: mysqlclient in /usr/local/lib/python3.10/site-packages (from mysql) (2.1.0)

I'm using a free version of pythonanywhere. Is it possible that MySql isn't included with this? I didn't see anything that said that. Also, I'm currently in the Tarpit, could that have something to do with it?

Here's the error log:

2023-04-08 13:11:20,321: Error running WSGI application
2023-04-08 13:11:20,322: django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
2023-04-08 13:11:20,323: Did you install mysqlclient?
2023-04-08 13:11:20,323:   File "/var/www/waynemelnick_pythonanywhere_com_wsgi.py", line 17, in <module>
2023-04-08 13:11:20,323:     application = get_wsgi_application()
2023-04-08 13:11:20,323: 
2023-04-08 13:11:20,323:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2023-04-08 13:11:20,323:     django.setup(set_prefix=False)
2023-04-08 13:11:20,324: 
2023-04-08 13:11:20,324:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
2023-04-08 13:11:20,324:     apps.populate(settings.INSTALLED_APPS)
2023-04-08 13:11:20,324: 
2023-04-08 13:11:20,324:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/apps/registry.py", line 116, in populate
2023-04-08 13:11:20,324:     app_config.import_models()
2023-04-08 13:11:20,325: 
2023-04-08 13:11:20,325:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/apps/config.py", line 269, in import_models
2023-04-08 13:11:20,325:     self.models_module = import_module(models_module_name)
2023-04-08 13:11:20,325: 
2023-04-08 13:11:20,325:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/contrib/auth/models.py", line 3, in <module>
2023-04-08 13:11:20,325:     from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
2023-04-08 13:11:20,325: 
2023-04-08 13:11:20,326:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
2023-04-08 13:11:20,326:     class AbstractBaseUser(models.Model):
2023-04-08 13:11:20,326: 
2023-04-08 13:11:20,326:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/db/models/base.py", line 141, in __new__
2023-04-08 13:11:20,326:     new_class.add_to_class("_meta", Options(meta, app_label))
2023-04-08 13:11:20,326: 
2023-04-08 13:11:20,326:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/db/models/base.py", line 369, in add_to_class
2023-04-08 13:11:20,326:     value.contribute_to_class(cls, name)
2023-04-08 13:11:20,327: 
2023-04-08 13:11:20,327:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/db/models/options.py", line 231, in contribute_to_class
2023-04-08 13:11:20,327:     self.db_table, connection.ops.max_name_length()
2023-04-08 13:11:20,327: 
2023-04-08 13:11:20,327:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/utils/connection.py", line 15, in __getattr__
2023-04-08 13:11:20,327:     return getattr(self._connections[self._alias], item)
2023-04-08 13:11:20,327: 
2023-04-08 13:11:20,327:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/utils/connection.py", line 62, in __getitem__
2023-04-08 13:11:20,327:     conn = self.create_connection(alias)
2023-04-08 13:11:20,328: 
2023-04-08 13:11:20,328:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/db/utils.py", line 193, in create_connection
2023-04-08 13:11:20,328:     backend = load_backend(db["ENGINE"])
2023-04-08 13:11:20,328: 
2023-04-08 13:11:20,328:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/db/utils.py", line 113, in load_backend
2023-04-08 13:11:20,328:     return import_module("%s.base" % backend_name)
2023-04-08 13:11:20,328: 
2023-04-08 13:11:20,328:   File "/home/WayneMelnick/.virtualenvs/mysite-virtualenv/lib/python3.10/site-packages/django/db/backends/mysql/base.py", line 17, in <module>
2023-04-08 13:11:20,328:     raise ImproperlyConfigured(

[edit by admin: formatting]

From your traceback, it looks like you're using a virtualenv:

/home/WayneMelnick/.virtualenvs/mysite-virtualenv

However, it looks like you ran the pip install command when you were not working in the virtualenv:

Requirement already satisfied: mysqlclient in /usr/local/lib/python3.10/site-packages (2.1.0).

In order to install something in a virtualenv like the one that you have (inside the .virtualenvs directory, so created with mkvirtualenv) you need to first activate it for the Bash console that you're using:

workon mysite-virtualenv

...and then run the pip install command.

My aplication run rigth with sqlite. When i change the configuration in order to use mysql database i recive that error:

"django.core.exceptions.ImproperlyConfigured: mysqlclient 1.4.3 or newer is required; you have 1.0.3."

I reinstall the mysqlclient with pip install mysqlclient==1.4.3 but nothing change. I tried the mysql conection in mysql bash and this connects well.

Did you have your virtualenv activated when you ran the command to update mysqlclient? And are you working in the virtualenv when you see that error?

Yes, my virtualenv (kernell) is active when a i run my app. I asure the mysqlclient is installed, trating to reinstall it and i recive the following message.

(kernell) 15:23 ~/CONDOMINIUM/conjuntos (main)$ pip install mysqlclient==1.4.3 Requirement already satisfied: mysqlclient==1.4.3 in /home/condominium/.virtualenvs/kernell/lib/python3.8/site-packages (1.4.3)

But when i run my app

Error running WSGI application 2023-05-15 10:58:53,699: django.core.exceptions.ImproperlyConfigured: mysqlclient 1.4.3 or newer is required; you have 1.0.3. 2023-05-15 10:58:53,700: File "/var/www/condominium_pythonanywhere_com_wsgi.py", line 10, in <module> 2023-05-15 10:58:53,700: application = StaticFilesHandler(get_wsgi_application())

I don't know what's happend.

Are you using that virtualenv to run your web app?

Yes that´s my virtualenv. (kernell)

That is my virtualenv:

Virtualenv: Use a virtualenv to get different versions of flask, django etc from our default system ones. More info here. You need to Reload your web app to activate it; NB - will do nothing if the virtualenv does not exist.

/home/condominium/.virtualenvs/kernell

Start a console in this virtualenv

I'd find the error: https://code.djangoproject.com/ticket/34511

When use Django-4.2-py3-none-any and PyMySQL-1.0.3-py3-none-any, django won't load PyMySQL correctly but 4.1.8 is Ok. Error message is django.core.exceptions.ImproperlyConfigured: mysqlclient 1.4.3 or newer is required; you have 1.0.3..

I'd remove pymysql and ready! Thanks for all

Glad to hear that you made it work!