It could be the case where you are running the Python file with the wrong Python installation. In VS Code we can choose the interpreter using which we want to run our Python file.

By default, the Python extension looks for and uses the first Python interpreter it finds in the system path. To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P).

Just choose Python 3.8.9 from the list of interpreters. For complete guide you can refer to the documentation: https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment

Answer from anirudh sharma on Stack Overflow
๐ŸŒ
HatchJS
hatchjs.com โ€บ home โ€บ import mysql.connector could not be resolved (pylance: report missing imports)
Import mysql.connector could not be resolved (PyLance: report missing imports)
December 25, 2023 - Import mysql.connector could not ... imports. * This common error can be fixed by updating your pip installation or by adding the mysql connector library to your Python path....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ vscode - mysql connector not found
r/learnpython on Reddit: VSCODE - mysql connector not found
April 22, 2021 -

I am starting with Python and did short script in VScode. When I try to run it I get this error:

PS C:\Users\spider> & C:/Python39/python.exe c:/Users/spider/drek.py
Traceback (most recent call last):
File "c:\Users\spider\drek.py", line 5, in <module>
import mysql.connector
ModuleNotFoundError: No module named 'mysql'

I ran pip install mysql-connector and it installed it. If I run pip list I get this:

PS C:\Users\spider> pip list

Package Version
---------------------- -------
mysql-connector 2.2.9
mysql-connector-python 8.0.24
pip 21.0.1
protobuf 3.15.8
six 1.15.0
wheel 0.36.2

PS C:\Users\spider>

Why I can't use MySql commands and how to fix it?

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 70375630 โ€บ import-error-i-cant-import-mysql-connector-in-python
Import Error. I can't import mysql-connector in Python - Stack Overflow
import mysql.connector ModuleNotFoundError: No module named 'mysql.connector'; 'mysql' is not a package ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... 63 'Import "Path.to.own.script" could not be resolved Pylance (reportMissingImports)' in VS Code using Python 3.x on Ubuntu 20.04 LTS
๐ŸŒ
Reddit
reddit.com โ€บ r/mysql โ€บ python can not import mysql
r/mysql on Reddit: Python can not import mysql
June 22, 2022 - For anyone facing this issue today.. in command prompt or whatever u used to install (idk im a newbie) instead of pip install i tried pip3 install mysql-connector-python and then imported mysql.connector in python and it worked then. Earlier it wasn't as I only used pip install.
๐ŸŒ
GitHub
github.com โ€บ microsoft โ€บ pylance-release โ€บ issues โ€บ 940
Code marked unreachable after start_transaction() from mysql.connector ยท Issue #940 ยท microsoft/pylance-release
February 11, 2021 - Environment data Language Server version: 2021.2.0 (pyright e278f28d) OS and version: Windows 10 18363.1139 / WSL2 Debian GNU/Linux 10 (buster) Python version (& distribution if applicable, e.g. Anaconda): 3.7.3 mysql-connector: 2.2.9 Ex...
Author ย  microsoft
Top answer
1 of 6
5

There's two potential solutions to this, but first a piece of advice: never run the command pip directly. What you really want is <path to python> -m pip, replacing <path to python> with the path to the specific Python you want to install for. That way you guarantee that pip will install into the environment/interpreter you expect instead of just whatever pip happens to be first on your PATH.

With that out of the way, option one is to make sure that the Python environment you have selected in the Python extension matches the one you have been installing into. Simply running pip does not guarantee this, and so it's quite possible you have been installing into one version of Python but connecting the Python extension to another. You can run Python: Select interpreter in the command palette to choose the proper environment (or click on the Python interpreter details down in the status bar).

The second option -- and in my opinion the better one -- is to create a virtual environment and do the installation in there. So you could do <path to python> -m venv .venv in the directory of your workspace and the Python extension will pick this up and ask if you want to use that virtual environment. When you open a new terminal it should activate that virtual environment, letting you run python -m pip to install into that virtual environment (you can also do the activation manually or simply specify the path to the Python interpreter in the virtual environment directly when running -m pip).

2 of 6
3

It took me more than two hours to solve this.

There can be multiple problems:-

  1. You r saving that python file as mysql.py which causes this error. solution : change the name. This one is exception. explanation : https://discussion.dreamhost.com/t/helping-installing-a-python-mysql-connection/64222

  2. Path of python is not added in SYSTEM VARIABLES as it may be present in users but may not be updated there. So, check path of both python and Mysql. It is very important step.

  3. You might be using idle saved on desktop as shortcut. So don't and open from start.

  4. As many people suggested, use pip method to install connector and also check path again.

  5. ApexSQL Database Power Tools ..Install this extension if you are using VScode. read https://www.sqlshack.com/visual-studio-code-for-mysql-and-mariadb-development/

Hope any of this works. It took me a lot of googling and reading.

Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ linked โ€บ 65252074
Hot Linked Questions - Stack Overflow
When i develop rest API for Addons odoo, set import and get problem Import "odoo.addons.base_rest.controllers" could not be resolved. Pylance(reportMissingImports) this is a part of my ... ... import workspace2 workspace2.a_func() This works and runs completely fine, however it underlines workspace2 as unresolved and leaves the function un-coloured. ... I have some problems trying to connect Python3 with MySql (8). I'm doing a programming course and the truth is that I still don't understand much. I already have the mysql-connector-python module ...
๐ŸŒ
Latenode
community.latenode.com โ€บ other questions โ€บ mysql
Cannot resolve mysql.connector module import error - MySQL - Latenode Official Community
July 25, 2025 - Hey everyone, Iโ€™m facing a really annoying problem with my Python code. It keeps giving me an import error for the mysql.connector module. I went ahead and installed the package using pip install mysql-connector-python, and the installation went smoothly. Yet, when I try to execute my script, ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ modulenotfounderror-no-module-named-mysql-in-python
Modulenotfounderror: No Module Named Mysql in Python - GeeksforGeeks
July 23, 2025 - Module Not Installed: The MySQL connector module might not be on your system, which means Python canโ€™t find it. Incorrect Module Name: Python is case-sensitive, so even a small typo or wrong capitalization when importing the module can cause ...
๐ŸŒ
Dbtales
dbtales.com โ€บ connect-to-mysql-using-python-and-visual-studio-code
How to Connect to MySQL Using Python and Visual Studio Code โ€“ Database Tales
In this example we will be connecting to a MySQL database using the Visual Studio Code IDE. We will also setup a Python environment and install the MySQL Connector needed to connect to MySQL and MariaDB. The instructions should be similar on Windows, Mac and Linux but will not be exactly the ...
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-no-module-named-mysql
ModuleNotFoundError: No module named 'mysql' in Python | bobbyhadz
April 4, 2024 - Copied!# ๐Ÿ‘‡๏ธ Make sure to use your version of Python, e.g. 3.10 python3 -m pip install mysql-connector-python ยท If the error persists, try restarting your IDE and development server/script.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ answers โ€บ questions โ€บ 1093387 โ€บ import-could-not-be-resolved-in-vs-code
Import could not be resolved in VS Code - Microsoft Q&A
November 17, 2022 - For your second error the error is expect as Mysql-connector is deprecated as per the project description, and you should use mysql-connector-python and when we try to install the package it doesn't found any distributor with that name as is ...
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-gb โ€บ answers โ€บ questions โ€บ 5514512 โ€บ issue-with-connection-to-the-database-through-vs-c
Issue with connection to the database through VS code - Microsoft Q&A
August 5, 2025 - 2. Fix Common VS Code Problems A. Python Environment Issues Press Ctrl+Shift+P โ†’ Choose Python: Select Interpreter Make sure the Python version used by VS Code is the one you expect. Reinstall DB drivers in the correct environment: pip install ...
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ programming โ€บ python
Mysql.connector issue - Python - The freeCodeCamp Forum
July 20, 2021 - I am trying to connect MySQL(8.0.25) and python(3.9.6) using mysql.connector but getting error. Error: mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) Code: import mysql.connector as sql s =sql.connect(host='localhost',...