AttributeError: 模块 'mysql' 没有属性 'connector' [英] AttributeError: module 'mysql' has no attribute 'connector'

查看:100
本文介绍了AttributeError: 模块 'mysql' 没有属性 'connector'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm running Ubuntu 16.04, trying to connect to mysql in python:

    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'

我通过在此处下载软件包安装了 mysql python 模块.我试过 sudo apt-get install python-mysql.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?

在添加 import mysql.connector 后,我得到了一个无关的权限错误,我现在已经解决了,所以这就是我需要的很多!!!

after adding import mysql.connector I got an unrelated permissions error which I've now resolved, so that's what I needed ty lots!!!

推荐答案

解决方案是:

import mysql.connector # or from mysql import connector

因为模块 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']

<小时>

mysql 模块中的 __init__ 文件没有导入模块 connector.


The __init__ file in the module mysql doesn't import the module connector.

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

如果 connector 被导入到 __init__ 中,这可以隐式地工作: from .导入连接器.

This could work implicitly if connector was imported inside __init__ with : from . import connector.

这篇关于AttributeError: 模块 'mysql' 没有属性 'connector'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆