Python AttributeError:“模块”对象没有属性“连接” [英] Python AttributeError: 'module' object has no attribute 'connect'

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

问题描述

我正在尝试使用Ubuntu(Xubuntu 14.04)和预安装的Python版本使用sqlite3数据库创建程序。我试过第一行是否工作,但已经有错误。我安装了 python-sqlite和 sqlite3。有人可以帮忙吗?

I'm trying to create a program with sqlite3 database using Ubuntu (Xubuntu 14.04) and the pre-installed version of Python. I tried if the first lines are working but there is already an error. I installed "python-sqlite" and "sqlite3". Can anyone help?

import sqlite3 

connection = sqlite3.connect('test.db')
cursor = connection.cursor()

cursor.execute('CREATE TABLE test ( id INTEGER, first INTEGER, second TEXT, third TEXT, other INTEGER)')

connection.commit()

输出为:

user@device:~/folder$ python sqlite3.py 
Traceback (most recent call last):
File "sqlite3.py", line 1, in <module>
import sqlite3 
File "/home/michael/ownCloud/sqlite3.py", line 3, in <module>
connection = sqlite3.connect('test.db')
AttributeError: 'module' object has no attribute 'connect'

谢谢!

推荐答案

错误消息表明您已命名文件 sqlite3.py

The error message shows you've named a file sqlite3.py:

/home/michael/ownCloud/sqlite3.py"

其中屏蔽具有相同名称的标准模块。您的 sqlite3.py 没有定义 connect ,因此出现错误
解决方案是将文件重命名为其他名称。

which masks the standard module of the same name. Your sqlite3.py does not define connect, hence the error. The solution is to rename your file to something else.

正如Jim Raynor指出的那样,导入 sqlite3 也会在<$ c中创建一个 .pyc 文件$ c> / home / michael / ownCloud / ,在找到标准库中的 sqlite3 模块之前,也必须删除它。

As Jim Raynor points out, importing sqlite3 will also create a .pyc file in /home/michael/ownCloud/ which would also have to be deleted before the sqlite3 module in the standard lib can be found.

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

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