Importing python packages to Streamlit in Snowflake

Hello,

I am trying to import duckdb to use in Streamlit in Snowflake however it is not available on the Anaconda packages. I tried downloading and zipping the required files and adding them to a stage and running the code below

import streamlit as st
from snowflake.snowpark.context import get_active_session

session = get_active_session()

# ===============Import third-party python packages===============
import fcntl
import os
import sys
import threading
import zipfile

list_of_packages = ["httpx", "sniffio", "httpcore", "h11", "ollama", "duckdb"]

for pkg in list_of_packages:
    session.file.get(f"@PYTHON_PACKAGES_STREAMLIT/{pkg}.zip", os.getcwd())

# File lock class for synchronizing write access to /tmp
class FileLock:
    def __enter__(self):
        self._lock = threading.Lock()
        self._lock.acquire()
        self._fd = open('/tmp/lockfile.LOCK', 'w+')
        fcntl.lockf(self._fd, fcntl.LOCK_EX)

    def __exit__(self, type, value, traceback):
        self._fd.close()
        self._lock.release()

# Get the location of the import directory.
import_dir = os.getcwd()

# Get the path to the ZIP file and set the location to extract to.
extracted = '/tmp/python_pkg_dir'

# Extract the contents of the ZIP. This is done under the file lock
# to ensure that only one worker process unzips the contents.
with FileLock():
    for pkg in list_of_packages:
        if not os.path.isdir(extracted + f"/{pkg}"):
            zip_file_path = import_dir + f"/{pkg}.zip"
            with zipfile.ZipFile(zip_file_path, 'r') as myzip:
                myzip.extractall(extracted)

# Add path to new packages
sys.path.append(extracted)
# ================================================================
import ollama
import duckdb

However Im getting this error:

ModuleNotFoundError: No module named 'duckdb.duckdb'
Traceback:
File "/usr/lib/python_udf/24632422f624b8b191f434d68ca081f5077aff8f2ab3ba315c5eaa3322d03c76/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 600, in _run_script
    exec(code, module.__dict__)
File "/tmp/appRoot/streamlit_app.py", line 49, in <module>
    import duckdb
File "/tmp/python_pkg_dir/duckdb/__init__.py", line 4, in <module>
    import duckdb.functional as functional
File "/tmp/python_pkg_dir/duckdb/functional/__init__.py", line 1, in <module>
    from duckdb.duckdb.functional import (

If you need to use packages outside of the snowflake conda channel, I recommend using Snowpark Container Services instead. One of the limitations of Streamlit in Snowflake is the limitation on Python packages. If you use a Snowpark Container, you can use any version of Streamlit and any Python package.