From 2ef7c0b7365150e93cffdbf4130216fa5fd3651a Mon Sep 17 00:00:00 2001 From: cachho Date: Wed, 9 Aug 2023 09:24:41 +0200 Subject: [PATCH] fix: escape pysqlite swapping (#410) --- embedchain/utils.py | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/embedchain/utils.py b/embedchain/utils.py index 6ca2b9df..5b7cfa60 100644 --- a/embedchain/utils.py +++ b/embedchain/utils.py @@ -60,22 +60,32 @@ def use_pysqlite3(): import sqlite3 if platform.system() == "Linux" and sqlite3.sqlite_version_info < (3, 35, 0): - # According to the Chroma team, this patch only works on Linux - import datetime - import subprocess - import sys + try: + # According to the Chroma team, this patch only works on Linux + import datetime + import subprocess + import sys + + subprocess.check_call( + [sys.executable, "-m", "pip", "install", "pysqlite3-binary", "--quiet", "--disable-pip-version-check"] + ) - subprocess.check_call( - [sys.executable, "-m", "pip", "install", "pysqlite3-binary", "--quiet", "--disable-pip-version-check"] - ) + __import__("pysqlite3") + sys.modules["sqlite3"] = sys.modules.pop("pysqlite3") - __import__("pysqlite3") - sys.modules["sqlite3"] = sys.modules.pop("pysqlite3") - - # Let the user know what happened. - current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S,%f")[:-3] - print( - f"{current_time} [embedchain] [INFO]", - "Swapped std-lib sqlite3 with pysqlite3 for ChromaDb compatibility.", - f"Your original version was {sqlite3.sqlite_version}.", - ) + # Let the user know what happened. + current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S,%f")[:-3] + print( + f"{current_time} [embedchain] [INFO]", + "Swapped std-lib sqlite3 with pysqlite3 for ChromaDb compatibility.", + f"Your original version was {sqlite3.sqlite_version}.", + ) + except Exception as e: + # Escape all exceptions + current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S,%f")[:-3] + print( + f"{current_time} [embedchain] [ERROR]", + "Failed to swap std-lib sqlite3 with pysqlite3 for ChromaDb compatibility.", + f"Error:", + e + )