2013-04-17 23 views
1

我是新来的Python 3,仍在学习,但我需要帮助。脚本的第一部分是:Python 3 mySQL.connector冻结导入库

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壳线以上线路的正常工作:我可以导入连接器连接到我的数据库并取回数据。但是,当我将脚本另存为.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 

回答

5

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

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'.

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

采取