在 Azure 上部署 echo-bot.py 时出错。 ModuleNotFoundError:没有名为“aiohttp”的模块

问题描述 投票:0回答:1

我正在遵循 Azure 的教程,了解如何创建和部署简单的 echo 机器人。

但是,我在部署时遇到了问题。错误如下:

2024-02-15T14:28:44.906911470Z    _____                               
2024-02-15T14:28:44.906996971Z   /  _  \ __________ _________   ____  
2024-02-15T14:28:44.907002071Z  /  /_\  \\___   /  |  \_  __ \_/ __ \ 
2024-02-15T14:28:44.907005571Z /    |    \/    /|  |  /|  | \/\  ___/ 
2024-02-15T14:28:44.907008971Z \____|__  /_____ \____/ |__|    \___  >
2024-02-15T14:28:44.907012571Z         \/      \/                  \/ 
2024-02-15T14:28:44.907015771Z A P P   S E R V I C E   O N   L I N U X
2024-02-15T14:28:44.907019271Z 
2024-02-15T14:28:44.907022471Z Documentation: http://aka.ms/webapp-linux
2024-02-15T14:28:44.907025671Z Python 3.7.17
2024-02-15T14:28:44.907028771Z Note: Any data outside '/home' is not persisted
2024-02-15T14:28:47.919246257Z Starting OpenBSD Secure Shell server: sshd.
2024-02-15T14:28:48.126441484Z Site's appCommandLine: gunicorn --bind 0.0.0.0 --worker-class aiohttp.worker.GunicornWebWorker --timeout 600 app:APP
2024-02-15T14:28:48.126477484Z Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite -userStartupCommand 'gunicorn --bind 0.0.0.0 --worker-class aiohttp.worker.GunicornWebWorker --timeout 600 app:APP'
2024-02-15T14:28:48.187623605Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2024-02-15T14:28:48.187668106Z Build Operation ID: 1014dbc4d99e9853
2024-02-15T14:28:48.197276572Z Output is compressed. Extracting it...
2024-02-15T14:28:48.205194026Z Extracting '/home/site/wwwroot/output.tar.gz' to directory '/tmp/8dc2e31979c41aa'...
2024-02-15T14:28:48.197713275Z Oryx Version: 0.2.20230707.1, Commit: 0bd28e69919b5e8beba451e8677e3345f0be8361, ReleaseTagName: 20230707.1
2024-02-15T14:28:50.351728409Z App path is set to '/tmp/8dc2e31979c41aa'
2024-02-15T14:28:51.165217213Z Writing output script to '/opt/startup/startup.sh'
2024-02-15T14:28:51.425979509Z Using packages from virtual environment antenv located at /tmp/8dc2e31979c41aa/antenv.
2024-02-15T14:28:51.449039868Z Updated PYTHONPATH to '/opt/startup/app_logs:/tmp/8dc2e31979c41aa/antenv/lib/python3.7/site-packages'
2024-02-15T14:28:55.296754676Z 
2024-02-15T14:28:55.296796276Z Error: class uri 'aiohttp.worker.GunicornWebWorker' invalid or not found: 
2024-02-15T14:28:55.296803276Z 
2024-02-15T14:28:55.296807276Z [Traceback (most recent call last):
2024-02-15T14:28:55.296811176Z   File "/opt/python/3.7.17/lib/python3.7/site-packages/gunicorn/util.py", line 99, in load_class
2024-02-15T14:28:55.296815276Z     mod = importlib.import_module('.'.join(components))
2024-02-15T14:28:55.296819076Z   File "/opt/python/3.7.17/lib/python3.7/importlib/__init__.py", line 127, in import_module
2024-02-15T14:28:55.296822976Z     return _bootstrap._gcd_import(name[level:], package, level)
2024-02-15T14:28:55.296826776Z   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
2024-02-15T14:28:55.296840176Z   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
2024-02-15T14:28:55.296844176Z   File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
2024-02-15T14:28:55.296847976Z   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2024-02-15T14:28:55.296851776Z   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
2024-02-15T14:28:55.296859076Z   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
2024-02-15T14:28:55.296862676Z   File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
2024-02-15T14:28:55.296866477Z ModuleNotFoundError: No module named 'aiohttp'
2024-02-15T14:28:55.296869977Z ]

我找到了这个SO,但它没有解决我的问题。

这是我的

requirements.txt

botbuilder_schema==4.13.0
gunicorn==20.1.0
aiohttp==3.6.3
botbuilder-integration-aiohttp==4.13.0
botbuilder-ai==4.13.0
botbuilder==0.0.1
botbuilder-core==4.13.0
botbuilder-dialogs==4.13.0

这是我的

.deployment

[config]
SCM_DO_BUILD_DURING_DEPLOYMENT = true

这是我的根文件夹中的文件:

  • 自述文件.md
  • 初始化.py
  • 应用程序.py
  • bot.py
  • 配置.py
  • 部署模板
  • 需求.txt
  • .部署
  • .env

这是我用来部署机器人的命令:

az webapp deployment source config-zip --resource-group "resource_name" --name "service_name" --src "echo-bot.zip"

请帮忙!

azure azure-web-app-service botframework azure-bot-service azure-webapps
1个回答
0
投票
  • 由于版本不兼容,我在本地遇到了相同的 no module named
    aiohttp
    found
    错误。

按照以下步骤部署 echo bot 应用程序。

我已经使用Python 3.9版本创建了一个python echo bot项目,并且能够在本地运行。

  • 创建并激活虚拟环境
py -m venv env
.\env\Scripts\activate

在requirements.txt中添加了以下包

botbuilder-integration-aiohttp>=4.14.0
aiohttp
botbuilder-core
botbuilder-schema
  • 安装所需的依赖项
pip install -r requirements.txt

本地响应:

enter image description here

  • 要将应用程序部署到 Azure,请将
    init_func()
    添加到 app.py 并修改代码如下:
import sys
import traceback
from datetime import datetime
from aiohttp import web
from aiohttp.web import Request, Response, json_response
from botbuilder.core import (BotFrameworkAdapter, BotFrameworkAdapterSettings,
                             TurnContext)
from botbuilder.core.integration import aiohttp_error_middleware
from botbuilder.schema import Activity, ActivityTypes

from bot import MyBot
from config import DefaultConfig

CONFIG = DefaultConfig()

SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD)
ADAPTER = BotFrameworkAdapter(SETTINGS)

async def on_error(context: TurnContext, error: Exception):

    print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)
    traceback.print_exc()

    await context.send_activity("The bot encountered an error or bug.")
    await context.send_activity(
        "To continue to run this bot, please fix the bot source code."
    )

    if context.activity.channel_id == "emulator":

        trace_activity = Activity(
            label="TurnError",
            name="on_turn_error Trace",
            timestamp=datetime.utcnow(),
            type=ActivityTypes.trace,
            value=f"{error}",
            value_type="https://www.botframework.com/schemas/error",
        )
        await context.send_activity(trace_activity)

ADAPTER.on_turn_error = on_error

# Create the Bot
BOT = MyBot()

# Listen for incoming requests on /api/messages
async def messages(req: Request) -> Response:
    # Main bot message handler.
    if "application/json" in req.headers["Content-Type"]:
        body = await req.json()
    else:
        return Response(status=415)

    activity = Activity().deserialize(body)
    auth_header = req.headers["Authorization"] if "Authorization" in req.headers else ""

    response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn)
    if response:
        return json_response(data=response.body, status=response.status)
    return Response(status=201)
    
# enclosing the below code with init_func()

def init_func(argv):
    APP = web.Application(middlewares=[aiohttp_error_middleware])
    APP.router.add_post("/api/messages", messages)
    return APP
if __name__ == "__main__":
    APP = init_func(None)

    try:
        web.run_app(APP, host="0.0.0.0", port=CONFIG.PORT)
    except Exception as error:
        raise error
  • 使用多租户Python Azure 应用服务创建Azure 机器人。
  • 打开 Azure 机器人并在
    /api/messages i.e.,(https://<webappname>.azurewebsites.net)
    中添加带有
    Azure Bot=>COnfiguration=>Message Endpoint
    的 Web 应用程序 URL。
  • 获取 Microsoft 应用程序 ID 并创建一个密钥,以使用其值作为来自
    Azure Bot=>Configuration
    的 Microsoft 应用程序密码。
  • 更新
    Microsoft App ID and Microsoft App Password 
    中的
    Config.py
    值:
class DefaultConfig:
    """ Bot Configuration """
    PORT = 3978
    APP_ID = os.environ.get("MicrosoftAppId", "a573434f-3b7b-4e52-919a-c574d56965b5")
    APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "mWk8Q~.6uGbHAvNzBlg3T6kikKfPUCaC7HV~sdpW")
  • 将应用程序部署到 Azure。

enter image description here

  • 部署应用程序后,在
    Azure App Service=>Configuration=>General settings
    中添加启动命令:
python3 -m aiohttp.web -H 0.0.0.0 -P 8000 app:init_func

enter image description here

测试机器人:

  • 转到您的
    Azure Bot=>Settings=>Test in Web Chat
    并测试聊天机器人,如下所示:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.