导入错误:无法从“jinja2”导入名称“escape”。运行 docker compose up 时出现错误

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

我分别有以下 Dockerfile 和 docker-compose:

Dockerfile:

FROM python:3.9
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app

CMD python main.py

docker-compose.yml:

version: '3.8'
services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8001:5000
    volumes:
      - .:/app
    depends_on:
      - db

  db:
    platform: linux/x86_64
    image: mysql:5.7.41
    restart: always
    environment:
      MYSQL_DATABASE: main
      MYSQL_USER: root_user
      MYSQL_PASSWORD: root_user
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - .dbdata:/var/lib/mysql
    ports:
      - 33067:3306

requirements.txt

jinja2<3.1.0
Flask==1.1.2
Flask-SQLAlchemy==2.4.4
SQLAlchemy==1.3.20
Flask-Migrate==2.5.3
Flask-Script==2.0.6
Flask-Cors==3.0.9
requests==2.25.0
mysqlclient==2.0.1
pika==1.1.0

我添加了jinja2<3.1.0 after reading that jinja2 removed the escape class in the 3.1.0 version. But I am getting the same error when running the backend service.

main_app-backend-1  | Traceback (most recent call last):

main_app-backend-1  |   File "/app/main.py", line 1, in \<module\>

main_app-backend-1  |     from flask import Flask

main_app-backend-1  |   File "/usr/local/lib/python3.9/site-packages/flask/\__init_\_.py", line 14, in \<module\>

main_app-backend-1  |     from jinja2 import escape

main_app-backend-1  | ImportError: cannot import name 'escape' from 'jinja2' (/usr/local/lib/python3.9/site-packages/jinja2/\__init_\_.py)

main_app-backend-1 exited with code 1

有人遇到过类似的事情吗?

docker flask docker-compose jinja2
1个回答
0
投票

实际安装了哪个版本的

jinja2
?如果我
pip install 'jinja2<3.1.0'
,我会得到版本 3.0.3,并且
jinja2.escape
似乎存在于该版本中:

root@d7f369c74abd:/# pip install 'jinja2<3.1.0'
Collecting jinja2<3.1.0
  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.6/133.6 kB 4.5 MB/s eta 0:00:00
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Installing collected packages: MarkupSafe, jinja2
Successfully installed MarkupSafe-2.1.3 jinja2-3.0.3
root@d7f369c74abd:/# python
Python 3.9.18 (main, Dec 19 2023, 20:58:27)
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import jinja2
>>> jinja2.escape("hello world")
<stdin>:1: DeprecationWarning: 'jinja2.escape' is deprecated and will be removed in Jinja 3.1. Import 'markupsafe.escape' instead.
Markup('hello world')

请注意,最新版本中尚未删除

escape
方法;它只是被重新定位到
jinja2.filters
包中。对于版本 3.1.2(当前版本):

root@479c2978e931:/# pip install jinja2
Collecting jinja2
  Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.1/133.1 kB 3.1 MB/s eta 0:00:00
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Installing collected packages: MarkupSafe, jinja2
Successfully installed MarkupSafe-2.1.3 jinja2-3.1.2
root@479c2978e931:/# python
Python 3.9.18 (main, Dec 19 2023, 20:58:27)
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import jinja2.filters
>>> jinja2.filters.escape("hello world")
Markup('hello world')
© www.soinside.com 2019 - 2024. All rights reserved.