16

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
  • 197
  • 1
  • 1
  • 10
  • 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

9 Answers9

19

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,817
  • 6
  • 38
  • 48
  • 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
9

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
  • 91
  • 1
  • 2
4

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'

Gerard ONeill
  • 3,914
  • 39
  • 25
2

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
  • 554
  • 5
  • 10
  • 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
1

If you have installed both mysql-connector and mysql-connector-python:

  1. uninstall mysql-connector-python
  2. uninstall mysql-connector
  3. reinstall mysql-connector-python

If your file is named mysql.py or select.py rename it.

Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
0

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

0

Try with this,

import mysql.connector as mysql

connection = mysql.connect(
   host="localhost", user="root", port='3306', database="database_name")
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 06 '22 at 03:17
0

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.

Phil
  • 111
  • 1
  • 4
0

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.

SHY
  • 107
  • 6