You have to install it first through PIP.

pip install mysql-connector-python

Then you can use this in your code

from mysql.connector

Find more here:

https://pypi.org/project/mysql-connector-python/

https://dev.mysql.com/doc/connector-python/en/connector-python-introduction.html

Answer from jwill on Stack Overflow
๐ŸŒ
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
The bottom left bar should now displays the correct Python version that was used to create the environment. To choose a different interepreter you can click this bar and Select a different one. 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. ... The MySQL connector is now available inside this project.
Discussions

ModuleNotFoundError when importing mysql.connector in for python VS Code - Stack Overflow
So, check path of both python and Mysql. It is very important step. You might be using idle saved on desktop as shortcut. So don't and open from start. As many people suggested, use pip method to install connector and also check path again. ApexSQL Database Power Tools ..Install this extension if you are using VScode... More on stackoverflow.com
๐ŸŒ stackoverflow.com
VSCODE - mysql connector not found
I don't know much about mysql, but for me it seems like that you're trying to import a different module than the one you installed. "mysql.connector" != "mysql-connector" More on reddit.com
๐ŸŒ r/learnpython
13
3
April 22, 2021
How do I connect a python code on Visual Studio Code to MySQL Workbench?
You are asking entirely the wrong question. You should be asking what you need to do to view the database you created here in MySQL Workbench. And the answer to that is nothing. You would just need to put in the details you placed in this line: db=pymysql.connect(host='localhost', user='root', password='1234') but since that is already pointing at the local host with the default port, you should see the db there - assuming Workbench is also running on your local machine. I think the problem you are having is in thinking of "VS Code" and "MySQL workbench" as somehow being their own entire systems. That's not what they are. VS Code is an editor only. MySQL Workbench is a utility for viewing databases. They both work on the system you are running them on, ie your computer. More on reddit.com
๐ŸŒ r/learnpython
5
2
March 3, 2024
mysql in python with visual studio code - Stack Overflow
I installed python 2.7.15 version on my computer (windows 10 64 bit) and installed mysql using pip install mysql-connector and from command prompt i can see mysql-connector working fine and i can e... More on stackoverflow.com
๐ŸŒ stackoverflow.com
July 31, 2019
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to Install MySQL Connector/Python in VS Code | Connect to MySQL Databases with Python - YouTube
Are you ready to connect your Python projects to MySQL databases using VS Code? In this step-by-step tutorial, I'll show you how to install MySQL Connector/P...
Published ย  March 19, 2025
๐ŸŒ
Medium
medium.com โ€บ codex โ€บ connect-to-mysql-with-python-with-simple-example-9fd4eb242ba8
Connect to MySQL with Python! with Simple Example | by Sree Vishnu Vardhan Dharmavaram | CodeX | Medium
March 19, 2022 - pip install mysql-connector-pythonorconda install -c anaconda mysql-connector-python ยท Open the vscode and add a new folder to the workspace for all the workings we are going to do in the process.
๐ŸŒ
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 ยท
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to connect MYSQL in Python using Visual Studio Code - YouTube
This video explains how to connect your mysql database in python and execute a simple query. The implementation is done in VScode.
Published ย  May 27, 2021
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
๐ŸŒ
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?

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how do i connect a python code on visual studio code to mysql workbench?
r/learnpython on Reddit: How do I connect a python code on Visual Studio Code to MySQL Workbench?
March 3, 2024 -

I'm testing with a log-in template I've downlaoded from Github and I want to modify it by having it create a database with my own fields and attributes. However, the code "imports pymysql" and from a video I've watched that discusses about this code, made by the person who wrote said code, it uses the MySQL terminal to view the database. I want to change it in a way that the database can be created and viewed in MySQL Workbench, as I'm more accustomed with that tool.

Here's a sample of the code where the database is created:

#######################################################################

import pymysql

def submit():

if firstnameEntry.get()=='' or lastnameEntry.get()=='' or emailentry.get()=='' or gender.get()=='' \

or variable1.get()=='' or usernameEntry.get()=='' or passwordEntry.get()=='' or \

renterpasswordEntry.get()=='':

messagebox.showerror('Alert!', 'All Fields must be entered') #this is to check if all the entry fields are

# empty if it is true it'll show a msgbox error

elif passwordEntry.get() != renterpasswordEntry.get():

messagebox.showerror('Alert!', 'Passwords didn\'t Match')

else:

db=pymysql.connect(host='localhost', user='root', password='1234')

cur=db.cursor()

#

#1st section

try:

query='create database p_registration'

cur.execute(query)

query='use p_registration'

cur.execute(query)

query='create table personaldata (id int auto_increment primary key not null, firstname varchar(40), ' \

'lastname varchar(40), email varchar(40), gender varchar(40), country varchar (40), ' \

'username varchar(40), passwrd varchar(10), ' \

'confirmpasswrd varchar(10))'

cur.execute(query)

# messagebox.showinfo('Success','Database was created successfully')

except:

cur.execute('use p_registration')

#2nd section

query='insert into personaldata(firstname, lastname, email, gender, country, username, passwrd, confirmpasswrd)' \

' values(%s,%s,%s,%s,%s,%s,%s,%s)'

cur.execute(query,(firstnameEntry.get(), lastnameEntry.get(), emailentry.get(), gender.get(), variable1.get(),

usernameEntry.get(),passwordEntry.get(), renterpasswordEntry.get()))

db.commit() #to execute the change

db.close()

messagebox.showinfo('Success', 'Successful Registration.')

#to clear the fields after a successful registration

firstnameEntry.delete(0, END)

lastnameEntry.delete(0, END)

emailentry.delete(0, END)

usernameEntry.delete(0, END)

passwordEntry.delete(0, END)

renterpasswordEntry.delete(0, END)

gender.set(0)

variable1.set('Select Country')

windows.destroy()

import loginpg

#######################################################################

What changes should I make to have it connect to MySQL workbench instead? Thanks in advance.

๐ŸŒ
Python Forum
python-forum.io โ€บ thread-22131.html
How to connect Mysql databse with python and VSCode IDE
I am using VSCode ide for my python project. I want to connect python with MySQL database, I already downloaded MySQL connector using pip MySQL connector command and it was successful, but I can't use MySQL. Purely I am a beginner in python coding pl...
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to Connect Python with MySQL in VS Code - YouTube
How to Connect Python with MySQL in VS CodeWant to connect your Python script to a MySQL database in Visual Studio Code? In this step-by-step tutorial, you'l...
Published ย  June 12, 2025
๐ŸŒ
MySQL
dev.mysql.com โ€บ doc โ€บ connector-python โ€บ en โ€บ connector-python-installation-source.html
MySQL :: MySQL Connector/Python Developer Guide :: 4.5 Installing Connector/Python from a Source Distribution
To build Connector/Python with support for the C Extension, the following prerequisites must be satisfied: ... For Linux platforms: A C/C++ compiler, such as gcc. For Windows platforms: Current version of Visual Studio. Python development files. For installing the classic interface only: MySQL Server binaries (server may be installed or not installed on the system), including development files (to obtain the MySQL Server binaries, visit the MySQL download site).
๐ŸŒ
MySQL
dev.mysql.com โ€บ doc โ€บ mysql-shell-gui โ€บ en โ€บ mysql-shell-for-vscode-setup.html
MySQL :: MySQL Shell for VS Code :: 2.1 Set Up MySQL Shell for VS Code
This section describes how to install the MySQL Shell for VS Code for use inside Visual Studio Code and to activate an embedded instance of MySQL Shell if it is not installed on your system. MySQL Shell is a component that you can install separately ยท Download the MySQL Shell for VS Code extension ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
How To Connect MySQL To VsCode.. (For Windows) - YouTube
Learn how to get mysql hooked to vscode and kill some bugs while you're at it๐Ÿ‘Œ๐Ÿพ..Install MySQL๐Ÿ”— https://dev.mysql/downloads/installerCreate User jess_iCRE
Published ย  September 9, 2022
๐ŸŒ
StudyGyaan
studygyaan.com โ€บ home โ€บ blog โ€บ how to connect mysql in vscode (visual studio code)
How to Connect MySQL in VSCode (Visual Studio Code) - 2026
March 26, 2024 - Click on SQLTool Extension and ask you to Add New Database Connection. Now this will ask for Install MySQL Driver.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 57278830 โ€บ mysql-in-python-with-visual-studio-code
mysql in python with visual studio code - Stack Overflow
July 31, 2019 - I installed python 2.7.15 version on my computer (windows 10 64 bit) and installed mysql using pip install mysql-connector and from command prompt i can see mysql-connector working fine and i can e...
๐ŸŒ
Medium
medium.com โ€บ @rupakbhandari โ€บ connecting-to-a-mysql-database-from-visual-studio-code-ca342e520ece
Connecting to a MySQL Database from Visual Studio Code | by Rupak Bhandari | Medium
June 15, 2023 - Finally, exit the MySQL prompt typing EXIT; command on the Terminal. ... Launch your VS Code and open up VS Code extensions. Once there, search for SQL SERVER(mssql) extension added by Microsoft itself. Install this extension on your VS Code.
๐ŸŒ
MySQL
dev.mysql.com โ€บ doc โ€บ mysql-shell-gui โ€บ en โ€บ mysql-shell-vscode-python.html
MySQL :: MySQL Shell for VS Code :: 8 Python
The version of Python used in MySQL Shell for VS Code is 3.10. ... Windows: C:\Users\<user>\.vscode\extensions\oracle.mysql-shell-for-vs-code-1.2.2\shell\lib\Python3.10\Lib
๐ŸŒ
A2 Hosting
a2hosting.com โ€บ kb โ€บ developer-corner โ€บ mysql โ€บ connecting-to-mysql-using-python
Web Hosting Built to Grow With You | hosting.com
Web hosting that takes you from idea to website to a lasting business. Fast and secure, with free migration, real experts 24/7 and a 99.9% uptime SLA.