0

I'm running my Fast API project with pipenv and I use Python 3.6.3 as the interpreter. For production I use a postgres db which works fine. Now I want to test my api with pytest and sqlite.

Therefore I changed the db connection setup:

engine = create_engine("sqlite:///./test.db")
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()
db = SessionLocal()

I also tried the connection string from the official docs from sqlalchemy:

engine = create_engine('sqlite://')

when I run my test , I get the following error:

   engine = create_engine("sqlite:///./test.db")
../../../../.local/share/virtualenvs/test-hhG2yARU/lib/python3.6/site-packages/sqlalchemy/util/deprecations.py:298: in warned
    return fn(*args, **kwargs)
../../../../.local/share/virtualenvs/test-hhG2yARU/lib/python3.6/site-packages/sqlalchemy/engine/create.py:560: in create_engine
    dbapi = dialect_cls.dbapi(**dbapi_args)
../../../../.local/share/virtualenvs/test-hhG2yARU/lib/python3.6/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py:473: in dbapi
    from sqlite3 import dbapi2 as sqlite
../../../../.pyenv/versions/3.6.3/lib/python3.6/sqlite3/__init__.py:23: in <module>
    from sqlite3.dbapi2 import *
../../../../.pyenv/versions/3.6.3/lib/python3.6/sqlite3/dbapi2.py:27: in <module>
    from _sqlite3 import *
E   ModuleNotFoundError: No module named '_sqlite3'

I have seen in the source code that the error causing module has the following note:

 pysqlite2/dbapi2.py: the DB-API 2.0 interface
#
# Copyright (C) 2004-2005 Gerhard Häring <[email protected]>
#
# This file is part of pysqlite.

Here the error is cause by

from _sqlite3 import *

where _sqlite is not found. I had a look at the

dbapi2.py file from pysqlite3. Here sqlite is imported without the "_". So I thought since python3 the pysqlite3 file should be inbuilt in python.

Is there a way to change the pysqlite2 to 3? I've read about installing pysqlite3, but this did not change anything in my case.

I also tried to update from python 3.6.13 to 3.6.3 as 3.6 is mandatory. This also did not change the behaviour.

3
  • 2
    If you have compiled python3 yourself, the sqlite3 development headers (and library) needs to be available. Depending which distribution you're using, it should be available under libsqlite3-dev or similar. See stackoverflow.com/questions/1210664/no-module-named-sqlite3 Commented Oct 19, 2021 at 9:53
  • I installed python with pyenv. Commented Oct 19, 2021 at 9:57
  • Does that mean I have to add them manally? Commented Oct 19, 2021 at 10:05

1 Answer 1

0

I was able to solve it by the information provided by @MatsLindh Here is the original comment with the solution. I have installed

sudo apt-get install libsqlite3-dev

and then reinstalled my python version with pyenv

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.