Reputation: 307
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!!!
Upvotes: 17
Views: 56357
Reputation: 11045
If you have installed both mysql-connector and mysql-connector-python:
If your file are named mysql.py or select.py rename them.
Upvotes: 9
Reputation: 50
This error occurred for me (even after pip install mysql-connector-python
) when attempting to run db = mysql.connector.connect(...)
in the Python IDLE Shell. When I switched to a full .py
file, it worked correctly with the same code. I assume this has to do with what other users have mentioned about forbidden file names.
I believe if you can run import mysql
, check that 'connector' in dir(mysql) == True
, and run import mysql.connector
without errors, it means that it must be something other than a problem with your mysql-connector-python
package, like this.
Upvotes: 0
Reputation: 107
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.
Upvotes: 0
Reputation: 371
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.
Upvotes: 0
Reputation: 9
Try with this,
import mysql.connector as mysql
connection = mysql.connect(
host="localhost", user="root", port='3306', database="database_name")
Upvotes: 0
Reputation: 4092
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'
Upvotes: 4
Reputation: 9
The problem was that I created a script called "select.py" I renamed it and it works again.
Upvotes: 0
Reputation: 652
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
Upvotes: 2
Reputation: 111
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.
Upvotes: 11
Reputation: 13317
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
.
Upvotes: 19