Python AttributeError:“模块”对象没有“连接”属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25705671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:31:44  来源:igfitidea点击:

Python AttributeError: 'module' object has no attribute 'connect'

pythonsqliteconnection

提问by mschoenwaelder

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?

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

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()

The output is:

输出是:

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'

Thank's in advance!

提前致谢!

采纳答案by unutbu

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

错误消息显示您已命名文件sqlite3.py

/home/michael/ownCloud/sqlite3.py"

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

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

As Jim Raynor points out, importing sqlite3will also create a .pycfile in /home/michael/ownCloud/which would also have to be deleted before the sqlite3module in the standard lib can be found.

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

回答by mingzhu li

You need to change your script name. sqlite3is the name of your script and of the package you want to import, so Python import your script instead of the package, hence the error.

您需要更改脚本名称。sqlite3是您的脚本和要导入的包的名称,因此 Python 导入您的脚本而不是包,因此出现错误。

回答by Kelvin

Rename your script from 'sqlite3.py' to 'something else.py'.Python interpreter is having a hard time distinguishing your script and the inbuilt module sqlite3. The line 'AttributeError: 'module' object has no attribute 'connect' tells you that when you run your script it reads itself assuming it is the inbuilt sqlite3 module which obviously doesn't contain the attribute connect.

将您的脚本从“sqlite3.py”重命名为“something else.py”。Python 解释器很难区分您的脚本和内置模块 sqlite3。'AttributeError: 'module' object has no attribute 'connect' 这一行告诉你,当你运行你的脚本时,它会读取自己,假设它是内置的 sqlite3 模块,它显然不包含属性 connect。

回答by MrDevAce

I cannot answer to your input in here, but I got it sorted now.

我不能在这里回答你的意见,但我现在已经整理好了。

It is as you say, but renaming the original file is not enough. You have to delete the *.pyc file too. I only renamed the the offendingt filename and since it did not work I searched for other options.

正如你所说,但重命名原始文件是不够的。您也必须删除 *.pyc 文件。我只重命名了有问题的文件名,由于它不起作用,我搜索了其他选项。

This was tricky because the errormessage was missleading.

这很棘手,因为错误消息具有误导性。

Anyway. I have sort it out now. Thanks for your input.

反正。我现在已经解决了。感谢您的输入。

This is it, normaly when you code you often type out the filename to what you are about to code, for example: Hello World.

就是这样,正常情况下,当您编码时,您经常会输入要编码的文件名,例如:Hello World。

So the filename is named helloworld.py

所以文件名被命名为 helloworld.py

And in this spirit of nameconvention I named my Pythonfile

本着命名约定的精神,我将 Pythonfile 命名为

sqlite3.py

sqlite3.py

I had no idea that decision would give me so much grief I wrote the appropriate code and couldn't for some reason get passed the error

我不知道这个决定会给我带来如此多的悲伤,我编写了适当的代码,但由于某种原因无法通过错误

No Module found Error: sqlite3

找不到模块错误:sqlite3

This clearly tells me that, for some reason I don't have that module. Anyway, using pip command freeze, is going to sort this out.

这清楚地告诉我,由于某种原因,我没有那个模块。无论如何,使用 pip 命令冻结,将解决这个问题。

Strangely enough, no module in the list with sqlite3. That send me on a wildgoose-chase. Apparantly I am missig this module.

奇怪的是,列表中没有包含 sqlite3 的模块。这让我大吃一惊。显然我错过了这个模块。

That's strange since I have a Python ver. 3.8.2 and it is equipped with an build-in sqlite3 module.

这很奇怪,因为我有一个 Python 版本。3.8.2,内置sqlite3模块。

But I could not verify it.

但我无法验证。

I searched everywhere, and all suggestions I found was leaning towards that something was missing in my Pythoninstallation. I'll tried everything. To no avail. Then after some 2-3 days of research. I found this thread.

我到处搜索,我发现的所有建议都倾向于我的 Python 安装中缺少某些东西。我会尝试一切。无济于事。然后经过一些 2-3 天的研究。我找到了这个线程。

It said that Python cannot distinguish between a modulename or a filename, if they are having exactly the same names.

它说 Python 无法区分模块名或文件名,如果它们具有完全相同的名称。

By following the solution here. So easy.

通过遵循此处的解决方案。太简单。

Save your sqlite3.py file as a new filename, something different. then find your Python cache-file named *.pyc, delete the *.pyc file.

将您的 sqlite3.py 文件另存为新文件名,有些不同。然后找到名为 *.pyc 的 Python 缓存文件,删除 *.pyc 文件。

Now everything works.

现在一切正常。

So even if the module sqlite3 did not get listed via pip freeze command it was apparently there the whole time build-in to another module.

因此,即使模块 sqlite3 没有通过 pip freeze 命令列出,它显然一直都在内置到另一个模块中。

Note that I did not reinstall anything or added other modules to get this to work. Just renamed the py-filename or saved under a different name and deleted the *.pyc file. No other alternations.

请注意,我没有重新安装任何东西或添加其他模块来使其工作。只是重命名了 py-filename 或以不同的名称保存并删除了 *.pyc 文件。没有其他更替。