Cant import sql.connector

Asked by Paul Fenwick

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:
Last query:
Last reply:
Revision history for this message
Geert JM Vanderkelen (geertjmvdk) said :
#1

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

Revision history for this message
Paul Fenwick (paul-fenwick) said :
#2

Thanks that worked

Revision history for this message
saruque ahamed mollick (saruque) said :
#3

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