ImportError: No Module Named 'pysqlite2'

2020-04-03 12:00发布

问题:

I have written a program in Python which was done on windows. And in the windows test environment worked fine. Now I am setting up a linux server to internally host the program. I have installed all the dependencies etc from a generated requirements file but when I run it I come on a problem,

ImportError: No Module Named 'pysqlite2'.

I have extensively googled this issue and have not found a solution. Can anyone tell me how to fix this problem from code below? I cannot upload an image due to reputation isnt high enough. Any help would be greatly appreciated. If any other information is needed just comment and I will upload.

File "/home/ryan/python_p/venv/lib/python3.4/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py", line 334, in dbapi
    from pysqlite2 import dbapi2 as sqlite
ImportError: No Module named 'pysqlite2'

As far as I understand it sqlite either is not compatible or has compatibility issues?

Another issue that I think is directly related is when inside the virtual environment and I try pip3.4 install pysqlite i get

SyntaxError: Missing Parenthesis in call to 'Print

Its suggests install Sphinx which I did but did not cure.

I think these two issues are directly related and by curing ine should be able to cure the other.

回答1:

You could probably just use sqlite3 which is now part of the standard library and should work exactly the same as pysqlite2 does. You can try to modify the file mentioned from:

from pysqlite2 import dbapi2 as sqlite

to

from sqlite3 import dbapi2 as sqlite


回答2:

Try pip search sqlite, you may find many candidates. Pick something like this one:

 pip install pysqlite


回答3:

For people on CentOS 6 and Python 2.6:

Executing pip install pysqlite directly would result in a gcc error, you would have to yum install sqlite-devel first, before installing pysqlite.

After that, ImportError may still persist, if you are using a Python version different from the Python 2.6 that's shipped with CentOS 6. The error message I got is like:

ImportError: /usr/local/lib/python2.7/site-packages/pysqlite2/_sqlite.so: undefined symbol: sqlite3_stmt_readonly

This is a linking issue, copying below compiled library files from old Py2.6 directory to Py2.7 solved my problem, as inspired by this Github discussion.

cp /usr/lib64/python2.6/lib-dynload/_sqlite3.so /usr/local/lib/python2.7/sqlite3/
cp /usr/lib64/python2.6/lib-dynload/_sqlite3.so /usr/local/lib/python2.7/lib-dynload/