No module named '_mysql_connector' using Django and mysql-connector-python 8.0.32
I have a Django 4.1 website that was running fine in a Docker container with MariaDB 10.5. I updated only the mysql-connector-python
module from 8.0.29 to 8.0.32 but now when I start up the web container that runs Django's dev server I get:
myproject_web | Traceback (most recent call last):
myproject_web | File "/root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 64, in <module>
myproject_web | import _mysql_connector
myproject_web | ModuleNotFoundError: No module named '_mysql_connector'
myproject_web |
myproject_web | The above exception was the direct cause of the following exception:
myproject_web |
myproject_web | Traceback (most recent call last):
myproject_web | File "/root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.8/site-packages/mysql/connector/django/base.py", line 60, in <module>
myproject_web | from mysql.connector.connection_cext import CMySQLConnection
myproject_web | File "/root/.local/share/virtualenvs/code-_Py8Si6I/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 81, in <module>
myproject_web | raise ImportError(
myproject_web | ImportError: MySQL Connector/Python C Extension not available (No module named '_mysql_connector')
myproject_web |
myproject_web | The above exception was the direct cause of the following exception:
...
I'm not sure where the fault lies, having a sketchy understanding of database servers and their connectors etc. If I downgrade mysql-connector-python
to 8.0.29 then it works again (I can't use 8.0.30 or 8.0.31 because of a utf8 issue that I'm hoping is fixed in 8.0.32). Here is its changelog.
My docker-compose.yml
has this for the database container:
services:
db:
container_name: myproject_db
env_file: .env
image: mariadb:10.5
ports:
- 4306:3306
restart: unless-stopped
volumes:
- ./docker/db/init:/docker-entrypoint-initdb.d
- mysql_data:/var/lib/mysql
And my web container's Dockerfile
starts:
FROM python:3.8-slim-bullseye
RUN apt-get update \
&& apt-get install -y build-essential git libpq-dev libmariadb-dev mariadb-client --no-install-recommends \
&& rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man \
&& apt-get clean
Same issue here running Django 3.2-4.1 in Docker, on a Mac M1: "No module named '_mysql_connector'"
mysql-connector-python==8.0.31 and below runs correctly on all systems (native and Docker).
mysql-connector-python==8.0.32 runs correctly in Docker under Win10, and deploys successfully on our native Ubuntu servers. Only the Mac M1 generates the error.
The Win10 Docker container initializes with MySQL Docker Image 8.0.31-1.2.10-server, whereas the same Docker container on the Mac M1 shows "MySQL Docker Image 8.0.32-1.2.11-server".
Sorry these are just clues, but this seems like another M1 dependency issue.