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).

Answer from Brett Cannon on Stack Overflow
🌐
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?

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.

Discussions

python - Unable to import mysql.connector in vscode - Stack Overflow
I have installed mysql-connector package as follows: pip install mysql-connector When I run pip show mysql-connector, i get the following output: Name: mysql-connector Version: 2.2.9 Summary: MySQL More on stackoverflow.com
🌐 stackoverflow.com
February 14, 2024
python - why in vscode does not let me import MySQL? - Stack Overflow
Hello everyone how are you, please who can give me a solution to this: import mysql.connector ***Import "mysql.connector" could not be resolved*** when i try to import mysql.connector ... More on stackoverflow.com
🌐 stackoverflow.com
Python modules librarys packages load problems on IDEs Visual Code
Hello, Im using pip to download packages but that dependencies only works when I try from the CLI Type "help", "copyright", "credits" or "license" for more information. >>> import mysql.connector >>> But when I try from Visual Code it does not work. I already tried this matter of selecting ... More on discussion.fedoraproject.org
🌐 discussion.fedoraproject.org
7
1
January 3, 2023
python - Unable to import sql.connector in VScode - Stack Overflow
You could try and install this and try running if it solves your problem. ... Another solution would be restarting your local IDE which is VScode. I used VScode as my main IDE and installing some modules in python makes it raised an error like ModuleNotFound. Restarting it solved the problem. ... Sign up to request clarification or add additional context in comments. ... dev.mysql.com/doc/connector... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 ...
🌐
Stack Overflow
stackoverflow.com › questions › 77996625 › unable-to-import-mysql-connector-in-vscode
python - Unable to import mysql.connector in vscode - Stack Overflow
February 14, 2024 - Appreciate your help ... This problem was usually caused by incorrect python interpreter. You could use shortcuts Ctrl+Shift+P and type "Python: Select Interpreter" to choose the python interpreter which you installed the python package in.
🌐
Stack Overflow
stackoverflow.com › questions › 72022361 › why-in-vscode-does-not-let-me-import-mysql
python - why in vscode does not let me import MySQL? - Stack Overflow
– Kuro Neko Commented Apr 27, ... could not be resolved" The reason for the problem is that there is no customized module in the path folder of python.exe, so Python can't read the code of our customized modul...
🌐
Fedora Discussion
discussion.fedoraproject.org › ask fedora
Python modules librarys packages load problems on IDEs Visual Code - Fedora Discussion
January 3, 2023 - Hello, Im using pip to download packages but that dependencies only works when I try from the CLI Type "help", "copyright", "credits" or "license" for more information. >>> import mysql.connector >>> But when I try from Visual Code it does not work. I already tried this matter of selecting ...
Find elsewhere
🌐
sebhastian
sebhastian.com › no-module-named-mysql
Python ModuleNotFoundError: No module named 'mysql' | sebhastian
February 28, 2023 - You need to use the same version where you installed MySQL Connector so that the module can be found when you run the code from VSCode. Once done, you should be able to import MySQL Connector into your code.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-no-module-named-mysql
ModuleNotFoundError: No module named 'mysql' in Python | bobbyhadz
April 4, 2024 - To solve the error, install the module by running the pip install mysql-connector-python command. Open your terminal in your project's root directory and install the mysql-connector-python module.
🌐
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 ...
🌐
Position Is Everything
positioniseverything.net › home › modulenotfounderror no module named mysql: solved
Modulenotfounderror No Module Named Mysql: Solved - Position Is Everything
June 9, 2025 - However, if Python does not find the module, it will raise a ModuleNotFoundError and this will stop the execution of your program. So, an error like “import mysql.connector ModuleNotFoundError: no module named ‘mysql’” occurs if you’ve not installed the MySQL connector package.
🌐
GitHub
github.com › formulahendry › vscode-mysql › issues › 165
Not able to import · Issue #165 · formulahendry/vscode-mysql
March 11, 2021 - formulahendry / vscode-mysql Public · Notifications · You must be signed in to change notification settings · Fork 60 · Star 226 · New issueCopy link · New issueCopy link · Open · Open · Not able to import #165 · Copy link · mohitpatel17 · opened · on Mar 11, 2021 · Issue body actions · PS D:\Companies\FLEXUS\Python\the_basics> python3 appdict\app.py Traceback (most recent call last): File "appdict\app.py", line 1, in import mysql.connector ModuleNotFoundError: No module named 'mysql' No one assigned ·
Author   formulahendry
🌐
Python.org
discuss.python.org › python help
Windows 11 - Python mysql import error - Python Help - Discussions on Python.org
March 1, 2025 - ‘’’ This code was running with no issues and connecting to a remote mysql DB and not sure why it is failing now with this error. Appreciate any help as I am new to python. The ERROR Traceback (most recent call last): File “C:/PROJECTS/Python/mySQL/mySQL_Forum.py”, line 2, in from mysql.connector import Error ImportError: cannot import name ‘Error’ from ‘mysql.connector’ (unknown location) I have masked out my clients db credentials which are correct, the database server is running.
🌐
Reddit
reddit.com › r/learnpython › unable to import mysql.connector despite installation.
r/learnpython on Reddit: Unable to import mysql.connector despite installation.
October 20, 2016 -

I'm running Ubuntu 12.04 and using pycharm IDE with the anaconda interpreter.

I have installed the mysql.connector module, however whenever I try to import and run the mysql.connector, I get the 'ImportError: No module named mysql.connector'.

If I run import mysql.connector within the python console I receive... Traceback (most recent call last): File "/home/my_name/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-2-8eb88b59635b>", line 1, in <module> import mysql.connector File "/usr/lib/pycharm-community/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, **kwargs) ImportError: No module named mysql.connector

The file path to the mysql connector I can find is /usr/lib/pymodules/python2.7/mysql/connector

Anyone have an idea what is going wrong here?

Thanks in advance to any replies or solutions...

🌐
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, ...
🌐
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
Now that we are working inside our environment (.venv), we can install module that will be used by this project. Specifically we need the MySQL drivers in order to (db tales com) connect to the MySQL Database.
🌐
Stack Overflow
stackoverflow.com › questions › 68094131 › python-vs-code-not-being-able-to-connect-to-mysql
Python VS code , not being able to connect to mysql - Stack Overflow
Copyimport mysql.connector as mycon def CreateDatabase(): try: con=mycon.connect(host="localhost",user="root",password="") mycursor=con.cursor() mycursor.execute("create database myproject") con.commit() except Exception as e: print(e) CreateDatabase()
🌐
Itadminguide
itadminguide.com › visual-studio-code-install-mysql-connector
Visual Studio Code – install mysql connector – ITAdminGuide.com
Install mysql connector package to execute import package in Virtual Studio Code · Navigate to python installation directory · List the installed packages · python.exe -m pip list · If there is no mysql connection, install using this command · python.exe -m pip install mysql-connector-python ·