HOWTO · Python

How to Fix ImportError: No Module Named mysql.connector

Fix ImportError: No module named mysql.connector by installing mysql-connector-python in the interpreter that runs your program, verifying the import, and troubleshooting environment mismatches.

On this page

ImportError: No module named mysql.connector means the Python interpreter running your code cannot find the mysql.connector import. The package that provides it is named mysql-connector-python. The usual causes are that this package is not installed, or it was installed for a different Python interpreter than the one running the program.

What ImportError: No module named mysql.connector means

Python imports modules from the packages available to the current interpreter. A successful installation in one environment does not make the package available to another. For example, a terminal’s pip may belong to a system Python while your editor, notebook, scheduled task, or virtual environment runs a different interpreter.

The error concerns finding the module; it does not by itself indicate a MySQL server problem. First install the connector for the interpreter you use, then verify the import with that same interpreter. A database connection is a separate step.

Install mysql-connector-python in the Python environment you use

Use Python’s -m pip form so pip runs under the interpreter named in the command. In a terminal, run python -m pip install mysql-connector-python.

If you start your program with python3, use python3 -m pip install mysql-connector-python. On Windows, the Python launcher can select the interpreter, for example py -m pip install mysql-connector-python. Substitute the command you use to launch your program; installing with a different command can put the package in a different environment.

If your project uses a virtual environment, activate it before installing. For a venv named venv, macOS or Linux shells commonly use source venv/bin/activate; Windows Command Prompt uses venv\Scripts\activate.bat, and PowerShell uses venv\Scripts\Activate.ps1. Then run the install command using the environment’s Python. You can also avoid activation by calling the environment interpreter directly, such as venv/bin/python -m pip install mysql-connector-python on macOS or Linux.

Verify the installation

Run this example with the same interpreter and in the same environment used for your program:

import mysql.connector

print("mysql.connector import succeeded")
mysql.connector import succeeded

The supplied runtime result shows this import check succeeded. Run it with python, python3, py, or the full path to your project’s interpreter, as appropriate. The success message means that interpreter can import mysql.connector; this check does not test server access, credentials, or network connectivity. If the import fails in your environment, continue with the interpreter comparison below.

Troubleshoot the error if it persists

Compare the interpreter used for installation with the one running the failing program. In each place, run python -c "import sys; print(sys.executable)" using the relevant python command. The printed executable paths should identify the same environment. If they differ, install the package using the program’s interpreter, for example /path/to/project/venv/bin/python -m pip install mysql-connector-python, replacing the path with the executable reported by your program.

For an IDE, notebook, or service, check its configured Python interpreter or kernel; a terminal’s activated environment does not automatically change those settings. Select the intended environment in that tool, or install into the interpreter it already uses. Then rerun the import check from that same context.

If the import still fails, confirm that the install command completed and that it used the expected executable. Running python -m pip show mysql-connector-python with that executable can confirm whether pip sees the package in its environment. If it is absent, repeat the installation with that interpreter. Avoid relying on a bare pip command because it may refer to a different Python installation.

A missing top-level module is different from an error raised while loading another dependency. This guarded check reports the missing module name without turning the expected missing-package case into an unhandled traceback; other import failures are reported by Python as well and should be investigated separately.

try:
    import mysql.connector
except ModuleNotFoundError as error:
    print(f"Module not found: {error.name}")
except Exception as error:
    print(f"Import failed: {error}")
else:
    print("mysql.connector import succeeded")

The result depends on the interpreter’s installed packages, so no fixed output is shown. If the reported missing name is mysql or mysql.connector, install mysql-connector-python with the same interpreter used by the program. If another name is reported, investigate that dependency rather than treating it as proof that the connector package itself is missing. Other import failures are printed separately for investigation.

Connect to MySQL after the import succeeds

Once the import check works, you can call mysql.connector.connect(host="<host>", user="<username>", password="<password>", database="<database>") to attempt a database connection. Replace the placeholders with values from your own configuration; do not put real passwords in shared source code. This call requires a reachable MySQL server plus valid connection details, so it is not part of the import check. Import success does not establish that the server is running, that the credentials are accepted, or that the network permits access. Diagnose those connection issues separately from ImportError.