Back

[SOLVED] Appwrite Function "No Module Found"

  • 0
  • Self Hosted
  • Functions
ZachHandley
26 Oct, 2023, 16:41

so this is my main.py

TypeScript
from appwrite.client import Client
from appwrite.services.databases import Databases
from appwrite.services.users import Users
from appwrite.query import Query
from .claude_functions import ClaudeFunctions
import os


# This is your Appwrite function
# It's executed each time we get a request
def main(context):
    # Why not try the Appwrite SDK?
    client = (
        Client()
        .set_endpoint("https://zappwrite.com/v1")
        .set_project(os.environ["APPWRITE_FUNCTION_PROJECT_ID"])
        .set_key(os.environ["APPWRITE_API_KEY"])
    )
    appwriteusers = Users(client)
    appwritedb = Databases(client)
    personalityAssessments = appwritedb.list_documents(
        "maindb", "personality_assessments", [Query.limit(1000)]
    )

    context.log(
        f"Found {len(personalityAssessments['documents'])} personality assessments"
    )
    claude_functions = ClaudeFunctions.get_instance(context)

    # Go through our personality assessments and index any that need to be indexed
    for assessment in personalityAssessments:
        if assessment.get("indexed") == False:
            claude_functions.index_personality_assessment(
                assessment.get("$id"), assessment.get("description")
            )
    # Get the user ID
    user_id = context.req.body.get("userId")
    chat_id = context.req.body.get("chatId")
    user_message = context.req.body.get("userMessage")
    context.log(f"User ID in request: {user_id}\nChat ID in request: {chat_id}")
    # Get the user
    user = appwriteusers.get(user_id)
    # If we have a user we're good to go
    if user:
        user_messages = appwritedb.list_documents(
            "maindb",
            "chat_messages",
            [Query.limit(1000), Query.equal("chatId", chat_id)],
        ).get("documents")
        response = claude_functions.chat(user, user_message, user_messages)
        return context.res.json(response)
    else:
        context.error("User not found")
        return context.res.json({"error": "User not found"})
TL;DR
The user was facing an issue with the "No Module Found" error in their Appwrite function. They realized that they were using `os.environ` incorrectly and needed to use square brackets instead of parentheses. They also discovered that the default Python code had a problem on line 15. After checking the code in `claude_functions.py`, they found that the error was occurring on line 15 because they were using `os.environ` incorrectly again. The solution was to use square brackets instead of parentheses for `os.environ` in both files. The error persisted even after making these changes. The user shared their `main.py` code,
ZachHandley
26 Oct, 2023, 16:42

still the same error

ZachHandley
26 Oct, 2023, 16:42
TypeScript
Traceback (most recent call last):
  File "/usr/local/server/src/server.py", line 165, in handler
    output = await asyncio.wait_for(execute(context), timeout=safeTimeout)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/tasks.py", line 479, in wait_for
    return fut.result()
           ^^^^^^^^^^^^
  File "/usr/local/server/src/server.py", line 150, in execute
    userModule = importlib.import_module("function." + userPath)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/usr/local/server/src/function/src/main.py", line 5, in <module>
    from .claude_functions import ClaudeFunctions
  File "/usr/local/server/src/function/src/claude_functions.py", line 15, in <module>
    OPENAI_API_KEY = os.environ("OPENAI_API_KEY")
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '_Environ' object is not callable
ZachHandley
26 Oct, 2023, 16:42

only difference is i'm using 3.11 runtime

Drake
26 Oct, 2023, 16:43

what's the code in claude_functions.py?

ZachHandley
26 Oct, 2023, 16:43

uh

ZachHandley
26 Oct, 2023, 16:43

AI code

Drake
26 Oct, 2023, 16:43

line 15...

ZachHandley
26 Oct, 2023, 16:43

oh

ZachHandley
26 Oct, 2023, 16:44
TypeScript
# AI Imports
from llama_index.llms import Anthropic, OpenAI, ChatMessage, MessageRole
from llama_index.chat_engine.types import ChatMode
from llama_index.vector_stores import WeaviateVectorStore
from llama_index import VectorStoreIndex, ServiceContext, Document
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
import anthropic
import openai
from pydantic import BaseModel, Field

# Standard Imports
import os
import weaviate

OPENAI_API_KEY = os.environ("OPENAI_API_KEY")
CLAUDE_API_KEY = os.environ("CLAUDE_API_KEY")
anthropic.Anthropic.api_key = CLAUDE_API_KEY
openai.api_key = OPENAI_API_KEY
weaviate_auth = weaviate.AuthApiKey(api_key=os.environ("WEAVIATE_KEY"))
weaviate_url = os.environ("WEAVIATE_URL")
Drake
26 Oct, 2023, 16:44

there's hte problem...

ZachHandley
26 Oct, 2023, 16:44

why>

ZachHandley
26 Oct, 2023, 16:44

you do realize in the default Python code

ZachHandley
26 Oct, 2023, 16:44

you guys use os.environ

ZachHandley
26 Oct, 2023, 16:44

to access the Appwrite vars

Drake
26 Oct, 2023, 16:44

its with brackets, not parenthesis

ZachHandley
26 Oct, 2023, 16:44

oh

ZachHandley
26 Oct, 2023, 16:44

does os.getenv work?

Drake
26 Oct, 2023, 16:44

yes

ZachHandley
26 Oct, 2023, 16:45

okay, huh, is there a list of tricks to this?

Drake
26 Oct, 2023, 16:45

no this is default python behavior

ZachHandley
26 Oct, 2023, 16:45

like the .module trick to import

ZachHandley
26 Oct, 2023, 16:45

oh I guess I normally use getenv then

ZachHandley
26 Oct, 2023, 16:45

my bad.

ZachHandley
26 Oct, 2023, 16:45

[SOLVED] Appwrite Function "No Module Found"

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more