14

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!!!

planpony69
  • 177
  • 1
  • 1
  • 9
  • 2
    try with `import mysql.connector` – PRMoureu Sep 30 '17 at 14:44
  • Possible duplicate of [Exception 'module has no attribute connector' - Python mysql](https://stackoverflow.com/questions/24062113/exception-module-has-no-attribute-connector-python-mysql) – Kev1n91 Sep 30 '17 at 14:47
  • @kev1n91 that seems to be about `_mysql` which I've never heard of. At the end of the top answer they suggest there's some confusion over libraries – roganjosh Sep 30 '17 at 14:56
  • Nevertheless, the solution shows a way which might solve this issue too: Have you tried to include it in IPython (Spyder or sth similiar) and see with tab auto-completion if connector is maybe available? – Kev1n91 Sep 30 '17 at 15:06
  • Also: https://stackoverflow.com/questions/33562532/mysql-attributeerror-module-object-has-no-attribute-unpack-from – Kev1n91 Sep 30 '17 at 15:07
  • `print(dir(mysql))` and give the result. – roganjosh Sep 30 '17 at 15:20
  • @roganjosh `print(dir(mysql))` gives `['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'connector']` – planpony69 Sep 30 '17 at 15:48
  • @planpony69 i'm curious about what command you're using to avoid password in the terminal ? – PRMoureu Sep 30 '17 at 15:49
  • @PRMoureu `mysql -u root`: I just set a blank password and stepped through the steps in link below to not have to use `sudo` https://stackoverflow.com/questions/39281594/error-1698-28000-access-denied-for-user-rootlocalhost" – planpony69 Sep 30 '17 at 15:54
  • Glad you found a solution, do you still need to input the password ? – PRMoureu Sep 30 '17 at 16:56
  • 1
    @PRMoureu No, ty. I hadn't properly completed the steps in my OWN link :o Following those instructions properly resolved the issue. – planpony69 Sep 30 '17 at 17:02

4 Answers4

17

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.

PRMoureu
  • 12,299
  • 6
  • 35
  • 46
  • yep, the structure stays, only an import at the module level can make it easier – PRMoureu Sep 30 '17 at 15:40
  • 2
    Which makes me wonder why they didn't implement it that way in the first place, but I guess you're not answerable to that :) – roganjosh Sep 30 '17 at 15:43
6

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.

Gail Sparks
  • 61
  • 1
  • 2
0

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
Surya
  • 374
  • 2
  • 8
  • 1
    IMHO, renaming a property of a module to the module itself is a horrible idea. This could easily confuse someone why `mysql` does/doesn't have the expected properties/methods available. Better to use something like `import mysql.connnector as mysqlConnector` or just `import mysql.connector` – Jay Dadhania Jul 13 '21 at 13:40
0

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