Search code examples
pythonpippathmysql-connectormodulenotfounderror

in python mysql.connector why i see the this Errors?


I am a new user for python

import mysql.connector

try:

conn = mysql.connector.connect(

host='localhost',

user='root',

passwd=''

)

mycur=conn.cursor()

mycur.execute('creat database rakwan')

except mysql.connector.Error as r:

print(r)

the Error is:-

PS C:\\Users\\computer 8\> & "C:/Users/computer 8/AppData/Local/Programs/Python/Python312/python.exe" e:/PROGRAMING/python/Racoon/Learn/mq.py

Traceback (most recent call last):

File "e:\\PROGRAMING\\python\\Racoon\\Learn\\mq.py", line 1, in \<module\>

import mysql.connector

File "e:\\PROGRAMING\\python\\Racoon\\Learn\\mysql.py", line 1, in \<module\>

import mysql.connector

ModuleNotFoundError: No module named 'mysql.connector'; 'mysql' is not a package

pip install mysql

pip install mysql-connector

pip install mysql-connector-python-rf


Solution

  • Assuming you have the proper modules actually installed via pip, the ModuleNotFoundError: No module named 'mysql.connector'; 'mysql' is not a package error is likely occurring because you named your Python file mysql.py which is conflicting with the mysql-connector module.

    Python will try to import mysql.connector from your script first, which is likely causing the issue.

    Rename your Python file mysql.py to something else and try again.

    Example:

    mysql_connect.py:

    import mysql.connector
    
    try:
        # Create connection
        conn = mysql.connector.connect(
            host='localhost',
            user='root',
            passwd=''
        )
    
        # Create cursor
        mycur=conn.cursor()
        mycur.execute('CREATE DATABASE my_test_database;')
    
    except mysql.connector.Error as r:
        print(r)