ImportError: attempted relative import with no known parent package

What I’m trying to do:
Trying to connect with the anvil server

What I’ve tried and what’s not working:

import anvil.server

anvil.server.connect("<key>")

Code Sample:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-2231455c3d87> in <module>
----> 1 import anvil.server
      2 
      3 anvil.server.connect("server_7YHWASN2CLEDY36YXVAVQFSG-TY7UA3ICKFK5DRLO")

D:\**********\middle_layer\anvil.py in <module>
----> 1 from ._anvil_designer import Form1Template
      2 from anvil import *
      3 import anvil.server
      4 
      5 class Form1(Form1Template):

ImportError: attempted relative import with no known parent package`` 

Clone link:
share a copy of your app

This line should only be executed on the client, never on the uplink. Forms are only used on the client, not on the uplink.

What is the code that you are running?

So this the anvil code:

from ._anvil_designer import Form1Template
from anvil import *
import anvil.server

class Form1(Form1Template):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run before the form opens.

  def primary_color_1_click(self, **event_args):
    # Access the value from the app
    question_text = self.question.text

    # Call the jupyter notebook uplink method
    result = anvil.server.call('answer_question', question_text)
    
    # Show the result to the user
    self.answers.text = result 

And I am trying to run a code block in Jupyter:

# Tells the jupyter server that this is a an Anvil callable function
@anvil.server.callable
# Define the function that is going to do our NLP
def answer_question(user_question):
    # Encode the user question and dataset questions
    question_embedding = model.encode([user_question])
    dataset_embeddings = model.encode(df['questions'].tolist())
    # Compute cosine similarity between the user question and dataset questions
    similarities = cosine_similarity(question_embedding, dataset_embeddings)[0]
    # Find the most similar questions
    top_indices = similarities.argsort()[-6:-1][::-1]  # Get the top 5 similar questions
    top_similar_questions = df.iloc[top_indices]['questions'].tolist()
    top_similar_values = similarities[top_indices]
    # Find the most similar question
    most_similar_index = similarities.argmax()
    highest_similarity = similarities[most_similar_index]
    # If the similarity is above a threshold, return the corresponding answer
    threshold = 0.7  # Adjust the threshold as per your preference
    if highest_similarity > threshold:
        answer = df.iloc[most_similar_index]['answers']
        return answer
    else:
        return "Sorry, I couldn't find an answer to your question."

The first snippet defines a form, that code can only run on the client, not on jupyter, uplink or any other platform.

1 Like

I know that. Hence I am using anvil.server.connect to connect the jupyter notebook to the uplink server.

And this code was working yesterday. And from today itself it is not working.

This code cannot work on the uplink side. The uplink doesn’t know what to do with a form. Only the client can show the forms.

If you know that it doesn’t work, why are you trying to do it?

Solved the error. My client side as well as server side both were kept separate. However I had downloaded the anvil code and stored it in my local directory and had named it anvil.py. This anvil.py was getting imported instead of the actual anvil library.

After resolving the naming conflict, I was able to import the anvil.server module in my Jupyter notebook without any issues.

2 Likes