ModuleNotFoundError when importing mysql.connector in for python VS Code

Question:

I have downloaded mysql.connector and mysql but still there is the same issue

import mysql.connector
mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="Password"
)

print(mydb)

ModuleNotFoundError: No module named ‘mysql’ – this is the error

Asked By: Prerak

||

Answers:

Go to command line and type the command

pip install mysql

this should work, because you don’t have mysql installed for python

Answered By: Rajat Jog

Try installing like below in CMD or install through IDE.

pip install mysql-connector
Answered By: Gokul nath

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

Answered By: Brett Cannon

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.

Answered By: Observer
pip install mysql-connector or pip3 install mysql-connector

and make sure your file is not named as "mysql.py".

Answered By: Kinjal Pathak
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.