AttributeError: module 'mysql' has no attribute 'connector'

Question:

I’m running Ubuntu 16.04, trying to connect to mysql in python:

    import mysql
    username = 'root'

    cnx = mysql.connector.connect(user=username, database='db')
    cnx.close()

But I get an error:

    File "pysql1.py", line 4, in <module>
      cnx = mysql.connector.connect(user=username, database='db')
    AttributeError: module 'mysql' has no attribute 'connector'

I installed the mysql python module by downloading the package here. I tried sudo apt-get install python-mysql.connector to no avail. Any pointers?

EDIT: after adding import mysql.connector I got an unrelated permissions error which I’ve now resolved, so that’s what I needed ty lots!!!

Asked By: planpony69

||

Answers:

The solution is to execute :

import mysql.connector # or from mysql import connector

Because the module connector is only available when you import it explicitly :

import mysql

print(dir(mysql))
>>> ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', 
'__package__', '__path__', '__spec__']

import mysql.connector

print(dir(mysql))
>>> ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', 
'__package__', '__path__', '__spec__', 'connector']

The __init__ file in the module mysql doesn’t import the module connector.

mysql
|_______ __init__.py # no import at this level
|_______ connector
         |________ __init__.py

This could work implicitly if connector was imported inside __init__ with : from . import connector.

Answered By: PRMoureu

I was still getting the same error after import mysql.connector and checking permissions. I was running my script on MacOS using bash. After hours of searching for a solution, came across this post on the mysql forums: https://forums.mysql.com/read.php?50,584171,584729#msg-584729

The poster mentions that your python script can’t be named mysql.py My script was named types.py, so I renamed it to setup-types.py and that solved my problem. Hopefully this saves others some time when dealing with this same error.

Answered By: Gail Sparks

One more observation. In many tutorials, they didn’t installed the python connector the same way. At those times, the import statement might be the cause of the issue. Try the below import statement.

import mysql.connector as mysql
Answered By: Surya

The problem was that I created a script called "select.py" I renamed it and it works again.

Answered By: Leonardo Nunes

I had "pip install"ed mysql-connector before installing mysql-connector-python. I uninstalled the mysql-connector, and was getting this error.

As this stackoverflow question/answer mentions, uninstalling and then reinstalling the mysql-connector-python had everything working.

Python | MySQL | AttributeError: module 'mysql.connector' has no attribute 'connect'

Answered By: Gerard ONeill

Try with this,

import mysql.connector as mysql

connection = mysql.connect(
   host="localhost", user="root", port='3306', database="database_name")

I had the same problem, but none of the above solutions worked for me. I unfortunately had to try something else. I found out that the package I used wasn’t working anymore. It must have been a mix-up, where I upgraded my package and so my script wasn’t working anymore.

However, I installed this package: https://pypi.org/project/mysql-connector-python/ and my script is now back up and running.

Answered By: Phil

I am using python xenv to connect to the mysql outside the virtual env. After trying to fix the error below a lot of times:

Traceback (most recent call last):
  File "/Users/py/p.py", line 5, in <module>
    db_connection = mysql.connector.connect(
                    ^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'mysql.connector' has no attribute 'connect'

The issue is finally fixed by upgrading my mac OS system, uninstalling mysql-connector-python and mysql-connector, and reinstalling the mysql-connector-python.

Answered By: SHY