Install & Compatibility
Where this runs
tested against v1.2.0.20260807 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
pymysql-stubs
✓ import pymysql-stubs
✗ import pymysql-stubs
This quickstart demonstrates how to use PyMySQL with type hints. Installing `types-pymysql` allows your type checker to understand the types returned by `pymysql.connect`, `conn.cursor()`, and methods like `cursor.execute()` and `cursor.fetchall()`, improving code quality and maintainability. Remember that `types-pymysql` provides *stubs*; you still need to `pip install pymysql` to run this code at runtime.
import pymysql
from pymysql.cursors import DictCursor
import os
# These environment variables simulate configuration for a MySQL database.
# Replace with your actual database details or set environment variables.
DB_HOST = os.environ.get('MYSQL_HOST', 'localhost')
DB_USER = os.environ.get('MYSQL_USER', 'root')
DB_PASSWORD = os.environ.get('MYSQL_PASSWORD', 'password')
DB_NAME = os.environ.get('MYSQL_DB', 'test_db')
def get_example_data() -> list[dict]:
"""Connects to a MySQL database and fetches some example data."""
try:
# types-pymysql provides type hints for pymysql.connect's return (Connection)
with pymysql.connect(
host=DB_HOST,
user=DB_USER,
password=DB_PASSWORD,
database=DB_NAME,
cursorclass=DictCursor # Type stubs help with DictCursor return types
) as conn: # 'conn' is typed as 'pymysql.connections.Connection'
with conn.cursor() as cursor: # 'cursor' is typed as 'pymysql.cursors.DictCursor'
# Type stubs ensure 'execute' and 'fetchall' method signatures are known
cursor.execute("SELECT 1 as id, 'hello' as message")
result: list[dict] = cursor.fetchall()
return result
except pymysql.Error as e:
print(f"Database connection or query error: {e}")
return []
if __name__ == "__main__":
# This code requires 'pymysql' to be installed and a running MySQL server.
# 'types-pymysql' enhances static analysis of this code.
data = get_example_data()
if data:
print("Fetched data:", data)
else:
print("Failed to fetch data. Check database connection and `pymysql` installation.")
Upgrade
Version history
1.2.0.20260807latest on PyPI · released Aug 7, 2026
Audit
Dependencies
pymysqloptionalThe runtime library for which these stubs provide type hints.