Get_app('xxx') Error not font

sanic 20.12.6

app.py

from sanic import Sanic
app = Sanic(“a”)

db.py

from sanic import Sanic
app = Sanic.get_app(“a”)

sanic.exceptions.SanicException: Sanic app name “a” not found.

I would guess it’s probably and import ordering issue. You need the application to be declared before you can retrieve it from the registry.

I have also tried and still have this problem:
snaic 23.3.0
python 3.10

# server.py
import os
from sanic import Sanic
from sanic_ext import Extend
from sanic_redis import SanicRedis
from app.views import api, main, admin
from app.util.umongo_connection import connect
from dotenv import find_dotenv, load_dotenv
# 加载.env文件到环境变量
load_dotenv(find_dotenv('.env'), override=True)

app = Sanic('run')
Extend(app)
# 加载配置
app.update_config("app/config/base.py")
app.update_config("app/config/custom.py")

# redis
redis = SanicRedis(config_name="REDIS") # default config_name is "REDIS"
redis.init_app(app)

# 加载蓝图
app.blueprint(main)
app.blueprint(admin)
app.blueprint(api)

@app.listener('before_server_start')
async def db_init(sanic, loop):
    client = connect(app.config.MOTOR_URI, loop)
    app.ctx.db = client.get_database()
# models/__init__.py
from sanic import Sanic
app = Sanic.get_app('run')

error:
sanic.exceptions.SanicException: Sanic app name ‘run’ not found.
App instantiation must occur outside if name == ‘main’ block or by using an AppLoader.
See Dynamic Applications | Sanic Framework for more details.

Please help me find some ideas! thanks

If you use ``` around your code we will be able to read it more easily :sunglasses:

Likely your issue is with import ordering. Make sure that the app instance is created before Sanic.get_app.

I guess: because of the error reported when starting the program, the code is checked first when the program is started, and then the instance is created

If you have Sanic.get_app in the global scope, it is run at import time.

It has been resolved, it is the issue of introducing order. Thank you again!

1 Like