Skip to content

Instantly share code, notes, and snippets.

@sorydi3
Last active July 12, 2024 12:25
Show Gist options
  • Save sorydi3/69713e0f00c60b86a6c8016a1bcef89a to your computer and use it in GitHub Desktop.
Save sorydi3/69713e0f00c60b86a6c8016a1bcef89a to your computer and use it in GitHub Desktop.
fix chromadb sqlite3 issue
#!/bin/bash
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Your script starts here
if [ -z $VIRTUAL_ENV ]; then
echo "create a virtual environment and activate it before running this script"
python3 -m venv .venv
source .venv/bin/activate
#check if chromadb is installed
else
echo "Virtual environment is activated"
fi
if [ -z "$(pip show chromadb)" ]; then
echo "Installing chromadb package"
pip install chromadb
else
echo "chromadb package is already installed"
fi
if [ -d $VIRTUAL_ENV/lib/python3.*/site-packages/chromadb ]; then
echo "Using chromadb package within the virtual environment"
# Find the __init__.py file
cd $VIRTUAL_ENV/lib/python3.*/site-packages/chromadb
INIT_FILE="__init__.py"
# check if backup file already exists
BACKUP_FILE="__init__.bak"
TEMP_FILE=$(mktemp)
if [ -z "$TEMP_FILE" ]; then
echo "TEMP_FILE is not set. Exiting."
exit 1
fi
cat <<EOF > "$TEMP_FILE"
__import__('pysqlite3')
import sys
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
EOF
if [ -z $BACKUP_FILE ]; then # Backup file exists , this will be the case if the script is run multiple times in order to work with the same file before any changes are made
echo "Backup file does not exists"
touch $BACKUP_FILE
cat $INIT_FILE > $BACKUP_FILE
cat $BACKUP_FILE >> $TEMP_FILE
fi
cat $BACKUP_FILE >> $TEMP_FILE
cat $TEMP_FILE > $INIT_FILE
# Optionally, handle the backup for the chromadb package case if needed
else
echo "Using default chromadb database"
# Optionally, handle the backup for the default database case if needed
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment