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 OverflowModuleNotFoundError when importing mysql.connector in for python VS Code - Stack Overflow
VSCODE - mysql connector not found
How do I connect a python code on Visual Studio Code to MySQL Workbench?
mysql in python with visual studio code - Stack Overflow
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).
It took me more than two hours to solve this.
There can be multiple problems:-
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
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.
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. 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.
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.pyTraceback (most recent call last):File "c:\Users\spider\drek.py", line 5, in <module>import mysql.connectorModuleNotFoundError: 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.9mysql-connector-python 8.0.24pip 21.0.1protobuf 3.15.8six 1.15.0wheel 0.36.2
PS C:\Users\spider>
Why I can't use MySql commands and how to fix it?
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.