Updated: 29 September 2026 · Applies to: Python 3.9 and later, mysqlclient 2.2 and PyMySQL 1.1 and 2.2, on cPanel & WHM 138 servers with MariaDB or MySQL (AlmaLinux, Rocky Linux, CloudLinux)

Python programs need a driver to talk to MySQL or MariaDB. The old MySQL-python package this article used to describe only works with Python 2, which reached end of life in 2020, and installing it on a cPanel server caused conflicts with cPanel's database packages. Use one of the two current drivers instead, installed in a virtual environment so nothing changes in the system's own Python or in cPanel's packages. We tested both on AlmaLinux 9 and checked the packages on a cPanel & WHM 138 server with MariaDB 11.4.

Which driver?

  • PyMySQL: written in pure Python, needs no compiler or database headers. The easiest choice, and fast enough for most scripts and websites.
  • mysqlclient: the maintained successor of MySQL-python (same MySQLdb module name, so old code usually runs unchanged). Faster, but it is compiled against the MariaDB or MySQL client library.

1. Create a virtual environment

As the cPanel user who runs the application (or as root for a server tool):

python3 -m venv ~/venvs/myapp
source ~/venvs/myapp/bin/activate
pip install --upgrade pip

For web applications, cPanel's Setup Python App (on CloudLinux) or Application Manager create and manage the environment for you.

2a. Install PyMySQL

pip install PyMySQL

2b. Or install mysqlclient

mysqlclient needs a compiler, the Python headers and the database client headers. As root, install the headers that match your database server. A cPanel server with MariaDB may have MariaDB-devel installed already (our test server did); check with rpm -q MariaDB-devel:

dnf install gcc python3-devel pkgconf-pkg-config
dnf install MariaDB-devel               # MariaDB servers, if not installed yet
dnf install mysql-community-devel       # MySQL 8 servers instead

Stay with the package that matches cPanel's database server; on our MariaDB 11.4 test server, dnf treated a request for the distribution's mariadb-connector-c-devel as already satisfied by MariaDB-devel. Then, in the virtual environment:

pip install mysqlclient

The build finds the client library through pkg-config (libmariadb.pc from MariaDB-devel).

3. Test the connection

python - <<'EOF'
import pymysql            # or: import MySQLdb
conn = pymysql.connect(host="localhost", user="myuser_dbuser", password="...", database="myuser_db")
with conn.cursor() as cur:
    cur.execute("SELECT VERSION()")
    print(cur.fetchone())
conn.close()
EOF

Create the database and user in cPanel's Manage My Databases first; on cPanel, their names start with the account name and an underscore. Keep the password out of your code, for example in an environment variable or a configuration file only the user can read.

Official documentation: mysqlclient on PyPI and PyMySQL documentation.

Ucartz services for this topic

Was this answer helpful? 33 Users Found This Useful (95 Votes)