简体   繁体   English

Python 3.6.3 无法导入 _sqlite3 以使用 sqlalchemy 运行 SQLite 数据库

[英]Python 3.6.3 unable to import _sqlite3 to run a SQlite db with sqlalchemy

I'm running my Fast API project with pipenv and I use Python 3.6.3 as the interpreter.我正在使用 pipenv 运行我的 Fast API 项目,并使用 Python 3.6.3 作为解释器。 For production I use a postgres db which works fine.对于生产,我使用了一个工作正常的 postgres db。 Now I want to test my api with pytest and sqlite.现在我想用 pytest 和 sqlite 测试我的 api。

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:我还尝试了 sqlalchemy 官方文档中的连接字符串:

engine = create_engine('sqlite://') 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 <gh@ghaering.de>
#
# This file is part of pysqlite.

Here the error is cause by这里的错误是由

from _sqlite3 import *

where _sqlite is not found.其中 _sqlite 未找到。 I had a look at the我看了看

dbapi2.py file from pysqlite3.来自 pysqlite3 的 dbapi2.py 文件。 Here sqlite is imported without the "_".这里的 sqlite 是在没有“_”的情况下导入的。 So I thought since python3 the pysqlite3 file should be inbuilt in python.所以我认为从 python3 开始,pysqlite3 文件应该内置在 python 中。

Is there a way to change the pysqlite2 to 3?有没有办法将pysqlite2更改为3? I've read about installing pysqlite3, but this did not change anything in my case.我读过关于安装 pysqlite3 的文章,但这对我来说没有任何改变。

I also tried to update from python 3.6.13 to 3.6.3 as 3.6 is mandatory.我还尝试从 python 3.6.13 更新到 3.6.3,因为 3.6 是强制性的。 This also did not change the behaviour.这也没有改变行为。

I was able to solve it by the information provided by @MatsLindh Here is the original comment with the solution.我能够通过@MatsLindh 提供的信息解决它这是解决方案的原始评论 I have installed我已经安装

sudo apt-get install libsqlite3-dev

and then reinstalled my python version with pyenv然后用 pyenv 重新安装了我的 python 版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM