属性错误:模块“mysql”没有属性“连接器”

问题描述 投票:0回答:10

我正在运行 Ubuntu 16.04,尝试在 python 中连接到 mysql:

    import mysql
    username = 'root'

    cnx = mysql.connector.connect(user=username, database='db')
    cnx.close()

但是我收到错误:

    File "pysql1.py", line 4, in <module>
      cnx = mysql.connector.connect(user=username, database='db')
    AttributeError: module 'mysql' has no attribute 'connector'

我通过下载包here安装了mysql python模块。我尝试了

sudo apt-get install python-mysql.connector
但没有成功。有什么指点吗?

编辑:添加

import mysql.connector
后,我收到了一个不相关的权限错误,我现在已经解决了,所以这就是我需要的!!!

python mysql ubuntu-16.04 mysql-python
10个回答
19
投票

解决方案是执行:

import mysql.connector # or from mysql import connector

因为模块

connector
仅在您显式导入时才可用:

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

模块

__init__
中的
mysql
文件不会导入模块
connector

mysql
|_______ __init__.py # no import at this level
|_______ connector
         |________ __init__.py

如果将

connector
导入到
__init__
中,则可以隐式工作:
from . import connector


10
投票

import mysql.connector
并检查权限后,我仍然遇到相同的错误。我在 MacOS 上使用 bash 运行我的脚本。经过几个小时的寻找解决方案后,在 mysql 论坛上发现了这篇文章:https://forums.mysql.com/read.php?50,584171,584729#msg-584729

海报提到你的Python脚本不能命名为

mysql.py
,我的脚本被命名为
types.py
,所以我将其重命名为
setup-types.py
,这解决了我的问题。希望这可以为其他人在处理同样的错误时节省一些时间。


4
投票

在安装 mysql-connector-python 之前,我已经“pip install”了 mysql-connector。我卸载了 mysql-connector,并收到此错误。

正如这个 stackoverflow 问题/答案提到的,卸载然后重新安装 mysql-connector-python 一切正常。

Python | MySQL | AttributeError:模块“mysql.connector”没有属性“connect”


3
投票

如果您同时安装了 mysql-connectormysql-connector-python:

  1. 卸载 mysql-connector-python
  2. 卸载 mysql-connector
  3. 重新安装 mysql-connector-python

如果您的文件名为 mysql.pyselect.py 重命名它。


2
投票

再观察一次。在许多教程中,他们没有以相同的方式安装 python 连接器。此时,导入声明可能是问题的原因。尝试下面的导入语句。

import mysql.connector as mysql

0
投票

问题是我创建了一个名为“select.py”的脚本,我将其重命名,然后它再次工作。


0
投票

试试这个,

import mysql.connector as mysql

connection = mysql.connect(
   host="localhost", user="root", port='3306', database="database_name")

0
投票

我遇到了同样的问题,但上述解决方案都不适合我。不幸的是我不得不尝试别的东西。我发现我使用的软件包不再起作用了。这一定是一个混淆,我升级了我的包,所以我的脚本不再工作了。

但是,我安装了这个软件包:https://pypi.org/project/mysql-connector-python/,我的脚本现在已备份并运行。


0
投票

我正在使用 python xenv 连接到虚拟环境之外的 mysql。多次尝试修复以下错误后:

Traceback (most recent call last):
  File "/Users/py/p.py", line 5, in <module>
    db_connection = mysql.connector.connect(
                    ^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'mysql.connector' has no attribute 'connect'

通过升级我的 mac OS 系统,卸载 mysql-connector-python 和 mysql-connector,然后重新安装 mysql-connector-python,问题终于得到解决。


0
投票

当我尝试在 Python IDLE Shell 中运行

pip install mysql-connector-python
时,发生了此错误(即使在
db = mysql.connector.connect(...)
之后)。当我切换到完整的
.py
文件时,它可以使用相同的代码正常工作。我认为这与其他用户提到的禁止文件名有关。

我相信,如果您可以运行

import mysql
,检查
'connector' in dir(mysql) == True
,然后运行
import mysql.connector
而没有错误,则意味着它一定不是您的
mysql-connector-python
包的问题,就像这样。

© www.soinside.com 2019 - 2024. All rights reserved.