Trouble with Phyton 3.12 connecting to mySQL on Rocky 9.4

I have a web server running Rocky Linux 9.4 with Virtualmin on it. WIthin Virtualmin, there is a script that will configure Django to install and set up a project for a website. The Django version is 5.0.6, with a requirement of Python 3.10 or newer. I was able to install Python 3.12 on the server without issue, and configure the links so that the default version is now 3.12 - So far, so good.

Where I am running into trouble is getting Python 3.12 to talk to mySQL on my server. When I run the install script, it installs the python3-mysqlclient module, but then when it tries to connect, it can’t establish the connection to the database. I suspect everything is installed, but I need to configure something to get Python to talk to mySQL, and I’m at a loss how to make that happen. I’ve worked with Virtualmin support, but hit the limits of what they can handle - the script works on other Distros that use Python 3.10 or later as default. How do I tell Python to work with mySQL?

Output from the script is below:

Python module MySQLdb is required ..
Installing package python3-mysqlclient ..
Installing package(s) with command /bin/dnf -y update python3-mysqlclient ..
Last metadata expiration check: 0:23:22 ago on Thu Jun 20 11:55:07 2024.
Dependencies resolved.
Nothing to do.
Complete!
.. install complete.
.. done
Now installing Django version 5.0.6 ..
DB initialization install failed :
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/django/db/backends/mysql/base.py", line 16, in <module>
    import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/krdb/domains/experimental.krdb.net/.local/Experimental/manage.py", line 22, in <module>
    main()
  File "/home/krdb/domains/experimental.krdb.net/.local/Experimental/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py", line 416, in execute
    django.setup()
  File "/usr/local/lib/python3.12/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.12/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.12/site-packages/django/apps/config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 994, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/usr/local/lib/python3.12/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/usr/local/lib/python3.12/site-packages/django/contrib/auth/base_user.py", line 59, in <module>
    class AbstractBaseUser(models.Model):
  File "/usr/local/lib/python3.12/site-packages/django/db/models/base.py", line 143, in __new__
    new_class.add_to_class("_meta", Options(meta, app_label))
  File "/usr/local/lib/python3.12/site-packages/django/db/models/base.py", line 371, in add_to_class
    value.contribute_to_class(cls, name)
  File "/usr/local/lib/python3.12/site-packages/django/db/models/options.py", line 243, in contribute_to_class
    self.db_table, connection.ops.max_name_length()
                   ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
                   ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/utils.py", line 193, in create_connection
    backend = load_backend(db["ENGINE"])
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/utils.py", line 113, in load_backend
    return import_module("%s.base" % backend_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/backends/mysql/base.py", line 18, in <module>
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
Installation can be continued manually at https://experimental.krdb.net//
More information on using this script can be found at http://www.djangoproject.com/
.. installation was only partially complete.

Maybe you need python3.12-PyMySQL package?

It’s already installed - I just checked to be sure. I think I need to configure Python to use it somehow?

I think the key to this is the error:

File "/usr/local/lib/python3.12/site-packages/django/db/backends/mysql/base.py", line 18, in <module>
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

python3-mysqlclient does include MySQLdb but it’s for Python 3.9 and not 3.12. So there could be a conflict here, hence the problem.

Might need to do a pip install for mysqlclient for Python 3.12.

OK, digging a bit deeper, I ran:

pip install mysql-python

Which gave an error that ended in:

        File "/tmp/pip-install-wi02nh_f/mysql-python_1a135c1cc647488e9227bea346e1ecd9/setup_posix.py", line 2, in <module>
          from ConfigParser import SafeConfigParser
      ModuleNotFoundError: No module named 'ConfigParser'

I ran pip install ConfigParser, but the error persists. Digging some more…

Try installing python3.12-pip and remove python3-mysqlclient because it’s for Python 3.9. Use the pip for Python 3.12 to install mysqlclient. Installing the packages you have done means you have different packages for different versions of Python, and so they are conflicting and Python 3.12 cannot find what it needs.

I gave this a shot… and the Django install script reinstalled it as a prerequisite. I may need to do this manually, instead of relying on the script.

Yep, python 3.12 cannot be the default, and so any missing modules need to be resolved manually. The system python will always be 3.9. So for this it will require manual installation and disabling of using the script for installing dependencies. Should work after that.

But that one appears to be in “epel”, do you have epel enabled by default?