Gunicorn Error ModuleNotFoundError: No module named 'api' (flask+docker)
Gunicorn Error ModuleNotFoundError: No module named 'api' (flask+docker)
我正在开发一个示例烧瓶应用程序 (运行 没问题),我想 docker 对其进行处理。项目结构是:
环境变量:
导出 FLASK_APP=api/app.py
出口 FLASK_ENV=发展
目录:
Doker文件
api/
- app.py
- __ init __.py(空)
- config.py
- requirements.txy(合剂,gunicorn)
api.py
from flask import Flask
from os import environ as env
app = Flask(__name__, static_folder='../build', static_url_path='/')
# Get config from flask_env
app.config.from_object(f"api.config.{env['FLASK_ENV'].capitalize()}Config")
@app.route("/")
def hello_world():
return "Hello, World!"
Doker文件
FROM python:3.6.9
WORKDIR /api
COPY api ./
RUN pip install -r requirements.txt
ENV FLASK_APP api/app.py
ENV FLASK_ENV development
EXPOSE 5000
CMD ["gunicorn", "-b", ":5000", "api.app:app"]
烧瓶 运行(有效)
gunicorn -b 127.0.0.1:5000 api.app:app(有效)
docker 构建 -f Dockerfile -t flask-app-api 。 (作品)
12:52 $ docker build -f Dockerfile -t flask-app-api .
Sending build context to Docker daemon 69.12kB
Step 1/8 : FROM python:3.9
---> 716d7c7b7a80
Step 2/8 : WORKDIR /api
---> Using cache
---> 39ef3aa33606
Step 3/8 : COPY api ./
---> Using cache
---> 40094c83cd4b
Step 4/8 : RUN pip install -r requirements.txt
---> Using cache
---> faaa3eb08ac3
Step 5/8 : ENV FLASK_APP api/app.py
---> Using cache
---> abef46437fce
Step 6/8 : ENV FLASK_ENV development
---> Using cache
---> 14b5e8d8bc3c
Step 7/8 : EXPOSE 5000
---> Using cache
---> aa21d96a89f4
Step 8/8 : CMD ["gunicorn", "-b", ":5000", "api.app:app"]
---> Using cache
---> 9d5b5990fe93
Successfully built 9d5b5990fe93
docker 运行 --rm -p 5000:5000 flask-app-api(错误)
[2022-03-16 11:54:35 +0000] [1] [INFO] Starting gunicorn 20.1.0
[2022-03-16 11:54:35 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)
[2022-03-16 11:54:35 +0000] [1] [INFO] Using worker: sync
[2022-03-16 11:54:35 +0000] [7] [INFO] Booting worker with pid: 7
[2022-03-16 11:54:35 +0000] [7] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 359, in import_app
mod = importlib.import_module(module)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'api'
[2022-03-16 11:54:35 +0000] [7] [INFO] Worker exiting (pid: 7)
[2022-03-16 11:54:35 +0000] [1] [INFO] Shutting down: Master
[2022-03-16 11:54:35 +0000] [1] [INFO] Reason: Worker failed to boot.
python --版本
Python3.6.9
¿有帮助吗?
可能因为大量错误而浪费了两天时间。似乎在 app 目录上有一个 init.py 会使 Flask 在 FLASK_APP 和路由方面出现问题。删除它,问题就解决了。
我正在开发一个示例烧瓶应用程序 (运行 没问题),我想 docker 对其进行处理。项目结构是:
环境变量:
导出 FLASK_APP=api/app.py
出口 FLASK_ENV=发展
目录:
Doker文件 api/
- app.py
- __ init __.py(空)
- config.py
- requirements.txy(合剂,gunicorn)
api.py
from flask import Flask
from os import environ as env
app = Flask(__name__, static_folder='../build', static_url_path='/')
# Get config from flask_env
app.config.from_object(f"api.config.{env['FLASK_ENV'].capitalize()}Config")
@app.route("/")
def hello_world():
return "Hello, World!"
Doker文件
FROM python:3.6.9
WORKDIR /api
COPY api ./
RUN pip install -r requirements.txt
ENV FLASK_APP api/app.py
ENV FLASK_ENV development
EXPOSE 5000
CMD ["gunicorn", "-b", ":5000", "api.app:app"]
烧瓶 运行(有效)
gunicorn -b 127.0.0.1:5000 api.app:app(有效)
docker 构建 -f Dockerfile -t flask-app-api 。 (作品)
12:52 $ docker build -f Dockerfile -t flask-app-api .
Sending build context to Docker daemon 69.12kB
Step 1/8 : FROM python:3.9
---> 716d7c7b7a80
Step 2/8 : WORKDIR /api
---> Using cache
---> 39ef3aa33606
Step 3/8 : COPY api ./
---> Using cache
---> 40094c83cd4b
Step 4/8 : RUN pip install -r requirements.txt
---> Using cache
---> faaa3eb08ac3
Step 5/8 : ENV FLASK_APP api/app.py
---> Using cache
---> abef46437fce
Step 6/8 : ENV FLASK_ENV development
---> Using cache
---> 14b5e8d8bc3c
Step 7/8 : EXPOSE 5000
---> Using cache
---> aa21d96a89f4
Step 8/8 : CMD ["gunicorn", "-b", ":5000", "api.app:app"]
---> Using cache
---> 9d5b5990fe93
Successfully built 9d5b5990fe93
docker 运行 --rm -p 5000:5000 flask-app-api(错误)
[2022-03-16 11:54:35 +0000] [1] [INFO] Starting gunicorn 20.1.0
[2022-03-16 11:54:35 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)
[2022-03-16 11:54:35 +0000] [1] [INFO] Using worker: sync
[2022-03-16 11:54:35 +0000] [7] [INFO] Booting worker with pid: 7
[2022-03-16 11:54:35 +0000] [7] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 359, in import_app
mod = importlib.import_module(module)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'api'
[2022-03-16 11:54:35 +0000] [7] [INFO] Worker exiting (pid: 7)
[2022-03-16 11:54:35 +0000] [1] [INFO] Shutting down: Master
[2022-03-16 11:54:35 +0000] [1] [INFO] Reason: Worker failed to boot.
python --版本
Python3.6.9
¿有帮助吗?
可能因为大量错误而浪费了两天时间。似乎在 app 目录上有一个 init.py 会使 Flask 在 FLASK_APP 和路由方面出现问题。删除它,问题就解决了。