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
For this example we are install the “Python” extension by Microsoft which includes functions like IntelliSense, code formatting, Debugging and more. Click the “Extensions” icon and install: ... Be sure to experiment with other VS Code extensions, they can save time and improve your ...
Discussions

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 even query data but when i tried in visual studio code i keep getting error "pylint unable to find ... More on stackoverflow.com
🌐 stackoverflow.com
July 31, 2019
python - Visual Studio code Mysql connector pip error - Stack Overflow
sudo easy_install pip I used its code and it loaded successfully.Then I opened visual studio on the terminal pip install mysql-connector I used the code, but it gives an error ImportError: No mod... More on stackoverflow.com
🌐 stackoverflow.com
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.

🌐
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 ·
🌐
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
You can build the distribution with or without support for this extension. 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.
🌐
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?

🌐
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
Find elsewhere
🌐
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.

🌐
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
🌐
MySQL
dev.mysql.com › doc › dev › connector-python › latest › installation.html
Installation — MySQL Connector/Python X DevAPI Reference 9.7.0 documentation
shell> tar xzf mysqlx-connector-python-<version>.tar.gz shell> cd mysqlx-connector-python-<version>.tar.gz shell> python setup.py install --with-protobuf-include-dir=<protobuf-include-dir> --with-protobuf-lib-dir=<protobuf-lib-dir> --with-protoc=<protoc-binary>
🌐
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 - import mechanicalsoup # Don’t forget to import the new module from selenium import webdriver import re from datetime import datetime import urllib import mysql.connector URL = "https://www.samplewebsite.com/Security/Login" LOGIN = "username" PASSWORD = "password" shipdate = "" # Create a browser object browser = webdriver.Firefox() browser.get(URL) browser.find_element_by_id('Username').send_keys(LOGIN) browser.find_element_by_id('Password').send_keys(PASSWORD) browser.find_element_by_id('Login').click() ... Maybe noone answers because noone installs Python 2 today (where the end of life is less than half a year away).
🌐
YouTube
youtube.com › marianna garcia
Instalación MySqlConnector | Python - YouTube
Lenguaje de programación: PythonBD: MySQLIDE: Visual Studio CodeRecursos:https://pypi.org/project/mysql-connector-python/#filesPIP:python -m pip install --up...
Published   March 24, 2022
Views   3K
🌐
Stack Overflow
stackoverflow.com › questions › 67438635 › visual-studio-code-mysql-connector-pip-error
python - Visual Studio code Mysql connector pip error - Stack Overflow
sudo easy_install pip I used its code and it loaded successfully.Then I opened visual studio on the terminal pip install mysql-connector I used the code, but it gives an error ImportError: No mod...
🌐
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
Open Visual Studio Code and select Appearance from the View menu. From the open list, enable: ... Click the Extensions icon in the activity bar to open the view in the side bar. In the EXTENSIONS: MARKETPLACE search, enter "MySQL Shell for VS Code". Click Install on the entry for MySQL Shell ...
🌐
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
🌐
Stack Overflow
stackoverflow.com › questions › 27631023 › how-to-install-mysql-package-for-python-on-visual-studio-2013
How to install Mysql package for python on Visual Studio 2013 - Stack Overflow
December 24, 2014 - ... Show activity on this post. For Python and mySQL on visual Studio you need to work with Python 2.7 and download the mySQL connector from this Link after installation you should be able to use import MySQLdb
🌐
YouTube
m.youtube.com › watch
How to connect MySQL with Visual Studio Code
Share your videos with friends, family, and the world
Published   June 5, 2022