Imports in packages with empty source code don't work

I have a site that I’ve been working on, and today it stopped working with the following error:

ImportError: No module named Pages

This is from the following import:

from ..Pages.Sponsors import Sponsors

When I used autocomplete to recreate the import, it produced the exact same import which also failed.

The problem ended up being that I’d removed the boilerplate content from the package itself. Packages are generated with the following code:

import anvil.google.auth, anvil.google.drive
from anvil.google.drive import app_files
import anvil.users
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
import anvil.server
# This is a package.
# You can define variables and functions here, and use them from any form. For example, in a top-level form:
#
#    from . import Package1
#
#    Package1.say_hello()
#

def say_hello():
  print("Hello, world")

I’d assumed that boilerplate wasn’t necessary, and deleted it. Then the imports stopped working.

To get the imports working again, I had to restore the boilerplate and save and publish a new version.

I don’t know if this indicates a bug somewhere, but it was painful enough to debug that I thought I’d let you all know about it.

1 Like

Which pieces of boilerplate did you remove? I can’t think off the top of my head what would cause an issue like this…

Was this error on the server or the client?

You can put just a comment in place of the boilerplate code, but there has to be something in there.

I discovered that a while back, and I thought I had posted about it, but I can’t find it now…

(edit - found it, it was part of a private conversation)

Yeah, I’d removed the entire boilerplate, leaving an empty module. I’m assuming that means it doesn’t create some file (maybe __init__.py?) and doesn’t recognize the module.

Here’s a clone link to a project that shows the issue: https://anvil.works/build#clone:V7AY234KHNCBIVUH=C2TYUVYILK5VUK7UP2BVU5Y6

Moving to Bug Reports – we’ll get this fixed!