16

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

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!

mschoenwaelder
  • 163
  • 1
  • 1
  • 4

5 Answers5

19

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

/home/michael/ownCloud/sqlite3.py"

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.

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 1
    It's worth noting that after incorrectly naming file like that, one should also delete the sqlite3.pyc generated in the same folder. – Jim Raynor Apr 10 '15 at 15:26
2

You need to change your script name. sqlite3 is 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.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
mingzhu li
  • 21
  • 1
0

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.

kelvinmacharia254
  • 117
  • 1
  • 3
  • 12
0

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.

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.

So the filename is named helloworld.py

And in this spirit of nameconvention I named my Pythonfile

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

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.

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

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

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.

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

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.

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.

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.

MrDevAce
  • 1
  • 2
  • Would you mind restructuring your answer so it reads better? Starting by betting your code in code blocks, and possibly clearly demonstrating how your suggestions work, would make it a lot easier to read by everyone. – Unpossible Apr 30 '20 at 19:25
0

I was facing same error but finally i got solution that : "Copy and paste your program in another .py file then delete previous file where you had faced error save that and run" And problem will be solved