I'm running Ubuntu 16.04, trying to connect to mysql in python:
我正在运行Ubuntu 16.04,试图用python连接到mysql:
import mysql
username = 'root'
cnx = mysql.connector.connect(user=username, database='db')
cnx.close()
But I get an error:
但我犯了一个错误:
File "pysql1.py", line 4, in <module>
cnx = mysql.connector.connect(user=username, database='db')
AttributeError: module 'mysql' has no attribute 'connector'
I installed the mysql python module by downloading the package here. I tried sudo apt-get install python-mysql.connector
to no avail. Any pointers?
我在这里下载了这个包,安装了mysql python模块。我尝试了sudo apt-get install python-mysql。连接器都无济于事。指针吗?
EDIT: after adding import mysql.connector
I got an unrelated permissions error which I've now resolved, so that's what I needed ty lots!!!
编辑:添加导入mysql后。连接器我有一个不相关的权限错误,我现在已经解决了,所以我需要大量的ty !!
2 个解决方案
#1
4
The solution is to execute :
解决办法是:
import mysql.connector # or from mysql import connector
Because the module connector
is only available when you import it explicitly :
因为模块连接器只有在显式导入时才可用:
import mysql
print(dir(mysql))
>>> ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__',
'__package__', '__path__', '__spec__']
import mysql.connector
print(dir(mysql))
>>> ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__',
'__package__', '__path__', '__spec__', 'connector']
The __init__
file in the module mysql
doesn't import the module connector
.
mysql模块中的__init__文件没有导入模块连接器。
mysql
|_______ __init__.py # no import at this level
|_______ connector
|________ __init__.py
This could work implicitly if connector
was imported inside __init__
with : from . import connector
.
如果连接器在__init__中导入:from,则可以隐式地工作。进口连接器。
#2
0
I was still getting the same error after import mysql.connector
and checking permissions. I was running my script on MacOS using bash. After hours of searching for a solution, came across this post on the mysql forums: https://forums.mysql.com/read.php?50,584171,584729#msg-584729
我在导入mysql后仍然得到相同的错误。连接器和检查权限。我使用bash在MacOS上运行脚本。在花了几个小时寻找解决方案之后,我在mysql论坛上看到了这篇文章:https://forums.mysql.com/read.php?
The poster mentions that your python script can't be named mysql.py
My script was named types.py
, so I renamed it to setup-types.py
and that solved my problem. Hopefully this saves others some time when dealing with this same error.
海报中提到您的python脚本不能命名为mysql。我的脚本被命名为types。py,所以我将它重命名为setup-type。这就解决了我的问题。希望这能在处理相同的错误时为其他人节省一些时间。
#1
4
The solution is to execute :
解决办法是:
import mysql.connector # or from mysql import connector
Because the module connector
is only available when you import it explicitly :
因为模块连接器只有在显式导入时才可用:
import mysql
print(dir(mysql))
>>> ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__',
'__package__', '__path__', '__spec__']
import mysql.connector
print(dir(mysql))
>>> ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__',
'__package__', '__path__', '__spec__', 'connector']
The __init__
file in the module mysql
doesn't import the module connector
.
mysql模块中的__init__文件没有导入模块连接器。
mysql
|_______ __init__.py # no import at this level
|_______ connector
|________ __init__.py
This could work implicitly if connector
was imported inside __init__
with : from . import connector
.
如果连接器在__init__中导入:from,则可以隐式地工作。进口连接器。
#2
0
I was still getting the same error after import mysql.connector
and checking permissions. I was running my script on MacOS using bash. After hours of searching for a solution, came across this post on the mysql forums: https://forums.mysql.com/read.php?50,584171,584729#msg-584729
我在导入mysql后仍然得到相同的错误。连接器和检查权限。我使用bash在MacOS上运行脚本。在花了几个小时寻找解决方案之后,我在mysql论坛上看到了这篇文章:https://forums.mysql.com/read.php?
The poster mentions that your python script can't be named mysql.py
My script was named types.py
, so I renamed it to setup-types.py
and that solved my problem. Hopefully this saves others some time when dealing with this same error.
海报中提到您的python脚本不能命名为mysql。我的脚本被命名为types。py,所以我将它重命名为setup-type。这就解决了我的问题。希望这能在处理相同的错误时为其他人节省一些时间。