I am trying to run Python script under Windows which accesses a MySQL database online. When I run the script, I get the error “No module named mysql”

The error “No module named mysql” means Python cannot find the MySQL connector library in your current environment.

On Windows, this usually happens because the MySQL package isn’t installed in the Python interpreter you’re actually using.


✅ Step 1 — Check which Python is running

Open Command Prompt and run:

python --version
where python

If you use python3, check that too:

python3 --version
where python3

This confirms which interpreter your script is using.


✅ Step 2 — Install a MySQL connector

The most common and reliable option is:

pip install mysql-connector-python

If pip isn’t recognised:

python -m pip install mysql-connector-python

If you have multiple Python versions installed:

py -m pip install mysql-connector-python

✅ Step 3 — Verify installation

Run:

pip show mysql-connector-python

or test in Python:

import mysql.connector
print("MySQL connector working")

⚠️ Common Causes

1️⃣ Installed into the wrong environment

If you’re using:

  • VS Code
  • PyCharm
  • A virtual environment (venv)
  • Anaconda

You must install the package inside that same environment.

If using a virtual environment:

venv\Scripts\activate
pip install mysql-connector-python

2️⃣ Using the wrong import

Correct import for mysql-connector-python:

import mysql.connector

If your script says:

import mysql

That will fail.


Alternative MySQL Libraries

Other valid connectors (but install matching package):

  • pip install pymysql → import pymysql
  • pip install mysqlclient → import MySQLdb

By Admin

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.