1

I am new to Python 3 and still learning but I need help. The first part of the script is:

import mysql.connector #this is failing as a .py but works in the shell

cnx = mysql.connector.connect(user='root', password='mypassword', host='my_ip_address', database'name_of_database') #this works in the shell

cursor = cnx.cursor()

I have tried the above line by line in the Python shell which works fine: I can import the connector to connect to my database and fetch back data. But when I save the script as a .py the import mysql connector does not work. I do have the Path variable setup and when I installed the mySQL connector it placed the relevant files in my Python install path \Lib\site-pacakges folder.

I get 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
4

1 回答 1

6

我和你有同样的问题,我在另一个你问同样问题的网站上找到了答案。我在这里粘贴答案作为参考。

发生上述情况是因为您的脚本名为“mysql.py”,实际上与“mysql”包的名称相同。Python 正确报告 ImportError。只需将您的“mysql.py”重命名为其他名称,例如“mysqlhacks.py”。

取自https://answers.launchpad.net/myconnpy/+question/226992

于 2013-07-11T07:48:52.487 回答