__import__() error?

One of the things I’m trying to accomplish is procedural app calls. To do this, I have this bit of code:

from .models import Page

def index(request, pagename):
    # Lots of bits are cut out for sake of relevancy

    page = Page.objects.get(permalink=pagename)

    if page.app != 'pages':
        package = __import__(f'{page.app}.views')
        return package.views.index(request, page)

However, it’s giving me a strange error. When I initially wrote this code, it worked just fine, but after transferring to another computer it throws the following error and stack trace (I tried doing the same thing in the django shell just to get really specific):

I’ve been scratching my head for days and can’t figure it out, if anyone knows what’s going on here I’d be really appreciative!

That looks like an error in an f string in the imported code. Try a vanilla import accounts.views to check if you get the same error.

I’d also question why you’re trying to do dynamic imports. It’s best to import everything at start-up time, to detect bugs early. You can then use a simple if statement or dict to find the relevant view function.

ohmy… thank you. I did get the same error. I looked everywhere for it… I thought it was an issue with importlib.