import pysqlite2 or import sqlite3?

H

Hertha Steck

Hello,

I'm using Python 2.5.1, Pysqlite 2.3.5 and SQLite 3.4.1 on Gentoo Linux.
I've always imported pysqlite using

from pysqlite2 import dbapi2

and that works. If I try

import sqlite3

I get

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/usr/lib/python2.5/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3

And I thought that's normal, there is no Python module called sqlite3.

Then, after a discussion in the Gentoo forum, I saw this in the Python
library reference:
To use the module, you must first create a Connection object that
represents the database. Here the data will be stored in the /tmp/example
file:
conn = sqlite3.connect('/tmp/example')

No import statement, though, so the module might have been renamed in that
statement. Possibly not a really good idea in the documentation.

But now I see an old post to c.p.l:
I'm using Ubuntu Feisty:
* Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
* SQLite version 3.3.13

Suppose I run the following program:
import sqlite3

conn = sqlite3.connect('example')
....

And from the rest of the posting that import seems to work. Has that module
different names for different Linux distributions? Or what's the matter
here?
 
H

Hertha Steck

Hertha said:
Hello,

I'm using Python 2.5.1, Pysqlite 2.3.5 and SQLite 3.4.1 on Gentoo Linux.
I've always imported pysqlite using

from pysqlite2 import dbapi2

and that works. If I try

import sqlite3

I get

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/usr/lib/python2.5/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3

And I thought that's normal, there is no Python module called sqlite3.

After a second look at the error message: when I installed Gentoo, Python
2.4 was installed, I got the new version a little later. And I think I
installed Pysqlite 2.3.5 separately.

Python 2.5 comes with pysqlite - should I uninstall my version?
 
M

Mike MacDonald

Hello,

I'm using Python 2.5.1, Pysqlite 2.3.5 and SQLite 3.4.1 on Gentoo Linux.
I've always imported pysqlite using

from pysqlite2 import dbapi2

and that works. If I try

import sqlite3

I get

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/usr/lib/python2.5/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ImportError: No module named _sqlite3

And I thought that's normal, there is no Python module called sqlite3.

Then, after a discussion in the Gentoo forum, I saw this in the Python
library reference:
To use the module, you must first create a Connection object that

represents the database. Here the data will be stored in the /tmp/example
file:


conn = sqlite3.connect('/tmp/example')

No import statement, though, so the module might have been renamed in that
statement. Possibly not a really good idea in the documentation.

But now I see an old post to c.p.l:
I'm using Ubuntu Feisty:
* Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
* SQLite version 3.3.13
Suppose I run the following program:
import sqlite3
conn = sqlite3.connect('example')

...

And from the rest of the posting that import seems to work. Has that module
different names for different Linux distributions? Or what's the matter
here?

Make sure you built python with the "sqlite" USE flag.
 
J

Jan Claeys

Op Wed, 21 Nov 2007 21:11:59 +0100, schreef Hertha Steck:
After a second look at the error message: when I installed Gentoo,
Python 2.4 was installed, I got the new version a little later. And I
think I installed Pysqlite 2.3.5 separately.

Python 2.5 comes with pysqlite - should I uninstall my version?

Some older software might still try to use the "external" pysqlite, so
maybe you want to check that first before removing anything.
 
H

Hertha Steck

Mike said:
....

Make sure you built python with the "sqlite" USE flag.

That was it. I had used the "sqlite3" USE flag, but for python "sqlite" is
needed. And removing pysqlite didn't harm anything.
 
M

MonkeeSage

I use the following for a progam I wrote using sqlite, to ensure
maximum compatibility (since the API is the same, importing them both
as 'sqlite' should be fine):

try:
from sqlite3 import dbapi2 as sqlite # python 2.5
except:
try:
from pysqlite2 import dbapi2 as sqlite
except:
print 'This program requires pysqlite2\n',\
'http://initd.org/tracker/pysqlite/'
sys.exit(1)

Regards,
Jordan
 
N

Nick Craig-Wood

MonkeeSage said:
I use the following for a progam I wrote using sqlite, to ensure
maximum compatibility (since the API is the same, importing them both
as 'sqlite' should be fine):

try:
from sqlite3 import dbapi2 as sqlite # python 2.5

I've been using

import sqlite3 as sqlite

here

sqlite3 and sqlite3.dbapi2 seem to be the same thing

Python 2.5.1 (r251:54863, Aug 17 2007, 00:51:07)
[GCC 4.1.3 20070812 (prerelease) (Debian 4.1.2-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sqlite3 import dbapi2
>>> import sqlite3
>>> set(dir(sqlite3)) ^ set(dir(dbapi2)) set(['__path__', 'dbapi2'])
>>>
 
M

MonkeeSage

MonkeeSage said:
I use the following for a progam I wrote using sqlite, to ensure
maximum compatibility (since the API is the same, importing them both
as 'sqlite' should be fine):
try:
from sqlite3 import dbapi2 as sqlite # python 2.5

I've been using

import sqlite3 as sqlite

here

sqlite3 and sqlite3.dbapi2 seem to be the same thing

Python 2.5.1 (r251:54863, Aug 17 2007, 00:51:07)
[GCC 4.1.3 20070812 (prerelease) (Debian 4.1.2-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
from sqlite3 import dbapi2
import sqlite3
set(dir(sqlite3)) ^ set(dir(dbapi2)) set(['__path__', 'dbapi2'])

Didn't know that! Seeing that only two attributes differed between
them in your demonstration, I looked at the __init__.py for the
package, and all it does is "from dbapi2 import *". Learn something
new every day. :)

Regards,
Jordan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top