Python 3 mySQL.connector冻结了import lib

时间:2013-04-17 08:33:15

标签: python mysql

我是Python 3的新手并且还在学习,但我需要帮助。脚本的第一部分是:

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()

我已经在Python shell中逐行尝试了以上工作正常:我可以导入连接器以连接到我的数据库并获取数据。但是当我将脚本保存为.py时,import mysql连接器不起作用。我确实有Path变量设置,当我安装mySQL连接器时,它将相关文件放在我的Python安装路径\ Lib \ site-pacakges文件夹中。

我收到以下错误:

>Traceback (most recent call last):
> File "<frozen importlib._bootstrap>", line 1518, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

在处理上述异常期间,发生了另一个异常:

>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

1 个答案:

答案 0 :(得分:5)

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

  

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

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