1

It's probably something quite easy but I can't figure out why my script won't work. I'm trying to make a connection with my sqlite3 database but eclipse returns the error: "Undefined variable from import: connect". I'm running python 3.3 in a virtualenv on linux. Thanks for your help!

from urllib.request import urlopen
import datetime
import sqlite3

class Crawler():

    def storeContent(self, html, url):
        conn = sqlite3.connect('database.db')
        c = conn.cursor()
        c.execute("INSERT .. ", [item, item])
        c.commit()
        c.close()
Jab
  • 821
  • 3
  • 13
  • 26
  • Normally, `connect` is one of the functions that gets imported from `_sqlite3` and re-exported from `sqlite3`. So, the first thing I'd try is: `import _sqlite3`. If that works, call `help(_sqlite3)` or `dir(_sqlite3)` and see if they look right. – abarnert Jun 18 '13 at 23:06
  • Also, try importing other C-implemented modules. You might have somehow ended up with a venv with a 32-bit Python but 64-bit libs, or something else that would break all C modules. – abarnert Jun 18 '13 at 23:07
  • I think import sqlite3 is correct because when i try print(sqlite3.version_info) it returns correctly the version. The libs I haven't checked yet. – Jab Jun 18 '13 at 23:28
  • 1
    Look at my comment again. `_sqlite3`, with the underscore, is a C extension module. `sqlite3`, without the underscore, is a pure-Python module that wraps up that extension module. Please try the tests I suggested on the one with the underscore. For testing other C extension modules… in 3.3, most of these are wrapped up the same way as `sqlite3`, but there are a few that haven't been. I believe `import audioop; print audioop.__file__` should show a .so/.dll/.pyd file, not a .py file. – abarnert Jun 18 '13 at 23:37
  • When I try to do: import _sqlite3 it says to be a unresolved import. Does this mean the libaries are incorrect? The audioop returns a non .py file like you said. Sorry for my ignorance, im a beginner – Jab Jun 19 '13 at 11:36
  • When I run python3.3 from my virtualenv, import _sqlite3 is working and seems to look allright. – Jab Jun 19 '13 at 17:34
  • Your last two comments seem to conflict with each other. Or maybe not; what does "says to be an unresolved import" mean? Is it an exception saying `ImportError: No module named '_sqlite3_'`, or something different? Anyway, if you can't `import _sqlite3` and see its contents, that's your problem. If you can, see whether `dir` or `help` on it includes a `connect` function. And, in general, when someone offers you a list of things to try to help debug your problem, please try and report on all of them, not half of them; otherwise, you're just doubling the time it will take to find the answer. – abarnert Jun 19 '13 at 18:40
  • @abarnert I actually have the same problem and you seem to have missed the important part of the question that this fails in ECLIPSE. I have pydev and python 3.2.2 and when i'm importing sqlite3 and then trying to connect it tells me exactly what OP is saying (Undefined variable ...). If I'm trying to import _sqlite3 it says that it is an unresolved import. I'm sorry, but your rebuke doesn't seems to be warranted here. – Eugene Sajine Nov 01 '13 at 19:27
  • It seems the problem is in the pydev - see this http://stackoverflow.com/questions/10407970/python-unresolved-import-error-for-sqlite3-in-pydev-in-eclipse – Eugene Sajine Nov 01 '13 at 20:03

1 Answers1

1

It seems like Alex Barcelo resolved this issue here.

What worked for me on Ubuntu was almost the same*:

cd /usr/lib/python2.7/lib-dynload/ 
sudo ln -s _sqlite3.x86_64-linux-gnu.so _sqlite3.so

After that, I had to reconfigure the Python Interpreter for my PyDev project: Project Properties -> PyDev-Interpreter/Grammar -> Click here to configure an interpreter not listed, then delete, run auto-config for the python environment you're using, and hit "Apply".

*Replace "python2.7" with the version of python you're using sqlite3 with, and if "_sqlite3.x86_64-linux-gnu.so" is not the right name of the file for your linux system, you can normally search for it using "locate _sqlite3"

Greg Kramida
  • 4,064
  • 5
  • 30
  • 46
  • In case you're around. I have the same problem but in virtualenv. How can I solve the issue? – Victor M Perez Jan 13 '18 at 13:56
  • @VictorHerasmePerez, sorry, no idea really. I think my solution would work assuming you have root access, but I suppose you ether tried this or don't have root access. – Greg Kramida Jan 13 '18 at 16:36