Import forms within a package - ModuleNotFoundError

What I’m trying to do:
Hi guys. I’m having difficulty importing forms from within a package. I placed all of my app pages, such as the login form, navbar form etc within the package named ‘App’. See screenshot below:

If the user signs in successfully, I then open the NavBarForm. The code for the loginform which calls the navbar form is shown below:

This used to work fine when all of my forms were at the upper level of my client code. Now that I have moved everything into the ‘App’ package, it no longer works. The autocomplete for the imports still works and gives the current imports as the suggestions. However, as you can see, I’m getting the ModuleNotFoundError.

What am I doing wrong?

Code Sample:

from ._anvil_designer import LoginFormTemplate
from anvil import *
import anvil.server
import anvil.users
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
from ..NavBarForm import NavBarForm

class LoginForm(LoginFormTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
      
  def login_btn_click(self, **event_args):
    """This method is called when the button is clicked"""
    user = anvil.users.login_with_form(allow_cancel=True)
    if user:
      anvil.server.call_s('update_user_login_count')
      Notification('Successfully signed in.').show()
      open_form('NavBarForm')

I think you may need a full import path inside the string. open_form() is not running at LoginForm’s level of the folder tree, but at the top level.

Something like:

open_form("App.NavBarForm")

(It should come up in the autocomplete)

You can also pass an instance of a form to open_form

open_form(NavBarForm())
1 Like

Pro tip: With open_form, calling server functions, and importing from other forms, modules, or dependencies, it’s easiest to let the auto-complete be your guide – it is super accurate for arguments that can be somewhat oblique and/or buggy.

open_form("App.NavBarForm") 

Worked perfectly, thanks @p.colbert

1 Like

Thanks @duncan_richards12 , since I copied the code from one form to the other, the auto-complete didn’t pop up with suggestions. However, you’re completely right, when I retyped it now to give it a try, I saw that it indeed does provide auto-complete inside open_form and provided exactly the suggestions I needed.

Yeah, that’s understandable, super easy to happen!