Cant import sql.connector

Asked by Paul Fenwick on 2013-04-18

Hi Folks

I am new to Python and still learning.

When I use the shell or Idle in Python3.3.1 I can import mysql.connector and test my script line by line OK but when I save my as a .py the import sql.connector fails with the following error:

Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1518, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Paul\Desktop\scripts\mysql.py", line 2, in <module>
    import mysql.connector
  File "C:\Users\Paul\Desktop\scripts\mysql.py", line 2, in <module>
    import mysql.connector
ImportError: No module named 'mysql.connector'; mysql is not a package

Question information

Language:
Chinese (Simplified) Edit question
Status:
Solved
For:
MySQL Connector/Python Edit question
Assignee:
No assignee Edit question
Solved by:
Paul Fenwick
Solved:
2013-04-18
Last query:
2013-04-18
Last reply:
2013-04-18

Hi Paul,

File "C:\Users\Paul\Desktop\scripts\mysql.py", line 2, in <module>
    import mysql.connector
ImportError: No module named 'mysql.connector'; mysql is not a package

The above happens because your script is named 'mysql.py', which is actually
the same name as the 'mysql' package. Python correctly reports ImportError.

Simply rename your 'mysql.py' to something else, like 'mysqlhacks.py'.

Cheers,
Geert

Paul Fenwick (paul-fenwick) said : #2

Thanks that worked

You can follow this tutorial to install the mysql connector: https://www.codespeedy.com/how-to-install-mysql-connector-in-python/