gpt4 book ai didi

python-3.x - AWS Mythical Mysfits - 没有名为 flask 的模块 - ImportError

转载 作者:行者123 更新时间:2023-12-02 18:45:47 25 4
gpt4 key购买 nike

我对 aws 非常陌生,尤其是 docker
所以我一直在关注此链接以了解有关开发 Web 应用程序的内容-
https://aws.amazon.com/getting-started/hands-on/build-modern-app-fargate-lambda-dynamodb-python/module-two/

我被困在它要求我们使用“docker run”命令在本地测试服务的地方(在模块中 - 2)
当我运行命令时,我收到“ImportError”。

这是我的代码片段....请帮帮我!
神话MysfitsService.py -

from flask import Flask, jsonify, json, Response, request
from flask_cors import CORS

# A very basic API created using Flask that has two possible routes for requests.

app = application = Flask(__name__)
CORS(application)

# The service basepath has a short response just to ensure that healthchecks
# sent to the service root will receive a healthy response.
@app.route("/")
def healthCheckResponse():
return jsonify({"message" : "Nothing here, used for health check. Try /mysfits instead."})

# The main API resource that the next version of the Mythical Mysfits website
# will utilize. It returns the data for all of the Mysfits to be displayed on
# the website. Because we do not yet have any persistent storage available for
# our application, the mysfits are simply stored in a static JSON file. Which is
# read from the the filesystem, and directly used as the service response.
@app.route("/mysfits")
def getMysfits():

# read the mysfits JSON from the listed file.
response = Response(open("mysfits-response.json").read())

# set the Content-Type header so that the browser is aware that the response
# is formatted as JSON and our frontend JavaScript code is able to
# appropriately parse the response.
response.headers["Content-Type"]= "application/json"

return response

# Run the service on the local server it has been deployed to,
# listening on port 8080.
if __name__ == "__main__":
application.run(host="0.0.0.0", port=8080)

Dockerfile -
FROM ubuntu:latest
RUN echo Updating existing packages, installing and upgrading python and pip.
RUN apt-get update -y
RUN apt-get install -y python3-pip python-dev build-essential
RUN pip3 install --upgrade pip
RUN echo Copying the Mythical Mysfits Flask service into a service directory.
COPY ./service /MythicalMysfitsService
WORKDIR /MythicalMysfitsService
RUN echo Installing Python packages listed in requirements.txt
RUN pip3 install -r ./requirements.txt
RUN echo Starting python and starting the Flask service...
ENTRYPOINT ["python"]
CMD ["mythicalMysfitsService.py"]

docker build -
ec2-user:~/environment/aws-modern-application-workshop/module-2/app (python) $ docker build . -t 054166015944.dkr.ecr.us-east-2.amazonaws.com/mythicalmysfits/service:latest

输出 -
Sending build context to Docker daemon  14.85kB
Step 1/14 : FROM ubuntu:latest
---> 1d622ef86b13
Step 2/14 : RUN echo Updating existing packages, installing and upgrading python and pip.
---> Using cache
---> 7cdaa818c9c2
Step 3/14 : RUN apt-get update -y
---> Using cache
---> a1fd46891d1e
Step 4/14 : RUN apt-get install -y python3-pip python-dev build-essential
---> Using cache
---> f81dc75dfc3f
Step 5/14 : RUN pip3 install --upgrade pip
---> Using cache
---> 0cee88c88f43
Step 6/14 : RUN echo Copying the Mythical Mysfits Flask service into a service directory.
---> Using cache
---> 709b50aac794
Step 7/14 : COPY ./service /MythicalMysfitsService
---> Using cache
---> f70454638e83
Step 8/14 : WORKDIR /MythicalMysfitsService
---> Using cache
---> 418a08d8ac32
Step 9/14 : RUN echo Installing Python packages listed in requirements.txt
---> Using cache
---> 03659d0cb00e
Step 10/14 : RUN pip3 install -r ./requirements.txt
---> Using cache
---> cb65480b6104
Step 11/14 : RUN echo Starting python and starting the Flask service...
---> Using cache
---> 89738a63f437
Step 12/14 : ENTRYPOINT ["python"]
---> Using cache
---> 97b4f200a571
Step 13/14 : CMD ["mythicalMysfitsService.py"]
---> Using cache
---> a22fe5fb5bdb
Step 14/14 : RUN unset -v PYTHONPATH
---> Using cache
---> f43930d6410a
Successfully built f43930d6410a
Successfully tagged 054166015944.dkr.ecr.us-east-2.amazonaws.com/mythicalmysfits/service:latest

docker run 在本地测试服务 -
$ docker run -p 8080:8080 054166015944.dkr.ecr.us-east-2.amazonaws.com/mythicalmysfits/service:latest

错误 -
Traceback (most recent call last):
File "mythicalMysfitsService.py", line 1, in <module>
from flask import Flask, jsonify, json, Response, request
ImportError: No module named flask

使用 flask --version 检查 flask
输出 -
Flask 1.0.4
Werkzeug 1.0.0

所以我安装了“ flask ”,但它仍然说没有名为 flask 的模块。

最佳答案

  • 使用 ubuntu:latest作为基本图像是一个坏主意。如果您在几个月前退出,您将获得 Ubuntu 18.04。如果你今天拉,你会得到 Ubuntu 20.04。所以有可能得到不一致的构建。最好将特定版本作为基础镜像,例如ubuntu:20.04 .更好的是,使用 python:3.8-slim-buster作为基础镜像,您将在 Debian 上预装最新的 Python 3.8,并且可以在它发布后轻松切换到 3.9。
  • 我的猜测是您在 Python 3 ( pip3 install ) 上安装了库,但您正在使用 Python 2 运行代码。尝试将 ENTRYPOINT 切换为 python3而不是 python .

  • 这篇文章更多地讨论了为你的基础镜像选择一个好的标签: https://pythonspeed.com/articles/reproducible-docker-builds-python/

    关于python-3.x - AWS Mythical Mysfits - 没有名为 flask 的模块 - ImportError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61659543/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com