Python AttributeError: 'module' object does not have 'connect' attribute

I am trying to create a program with sqlite3 database using Ubuntu (Xubuntu 14.04) and a pre-installed version of Python. I tried if the first lines work, 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() 

Conclusion:

 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' 

Thanks in advance!

+5
source share
1 answer

The error message shows that you named the sqlite3.py file:

 /home/michael/ownCloud/sqlite3.py" 

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

As Jim Raynor points out, importing sqlite3 will also create a .pyc file in /home/michael/ownCloud/ , which must also be deleted before the sqlite3 module is sqlite3 in the standard library.

+6
source

Source: https://habr.com/ru/post/1201990/


All Articles