python - 无法启动docker虚拟机: ImportError: cannot import name 'format_lazy' from 'django.utils.text' (Python Error)

标签 python django python-3.x docker

由于抛出 Python 错误,我无法启动 Docker 虚拟机。

我尝试过将 Python 版本从 3.5 切换到 3.6 再到 3.7。

我已直接连接到虚拟机,并尝试直接从命令行运行命令(例如./manage.py makemigrations)并收到相同的错误。 manage.py 的内容位于底部。

使用以下方式构建的 docker 虚拟机:

docker-compose pull

docker-compose build

docker-compose up

当虚拟机启动时,显示以下错误:

web_1  | ImportError: cannot import name 'format_lazy' from 'django.utils.text' (/usr/local/lib/python3.7/site-packages/django/utils/text.py)

完整跟踪如下:


web_1  | Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f9ba32c7b90>
web_1  | Traceback (most recent call last):
web_1  |   File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
web_1  |     autoreload.raise_last_exception()
web_1  |   File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
web_1  |     six.reraise(*_exception)
web_1  |   File "/usr/local/lib/python3.7/site-packages/django/utils/six.py", line 685, in reraise
web_1  |     raise value.with_traceback(tb)
web_1  |   File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
web_1  |     fn(*args, **kwargs)
web_1  |   File "/usr/local/lib/python3.7/site-packages/django/__init__.py", line 27, in setup
web_1  |     apps.populate(settings.INSTALLED_APPS)
web_1  |   File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 108, in populate
web_1  |     app_config.import_models(all_models)
web_1  |   File "/usr/local/lib/python3.7/site-packages/django/apps/config.py", line 199, in import_models
web_1  |     self.models_module = import_module(models_module_name)
web_1  |   File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
web_1  |     return _bootstrap._gcd_import(name[level:], package, level)
web_1  |   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
web_1  |   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
web_1  |   File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
web_1  |   File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
web_1  |   File "<frozen importlib._bootstrap_external>", line 728, in exec_module
web_1  |   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1  |   File "/usr/local/lib/python3.7/site-packages/filer/models/__init__.py", line 5, in <module>
web_1  |     from .imagemodels import *  # noqa
web_1  |   File "/usr/local/lib/python3.7/site-packages/filer/models/imagemodels.py", line 12, in <module>
web_1  |     from .abstract import BaseImage
web_1  |   File "/usr/local/lib/python3.7/site-packages/filer/models/abstract.py", line 12, in <module>
web_1  |     from ..utils.compatibility import PILImage
web_1  |   File "/usr/local/lib/python3.7/site-packages/filer/utils/compatibility.py", line 8, in <module>
web_1  |     from django.utils.text import Truncator, format_lazy
web_1  | ImportError: cannot import name 'format_lazy' from 'django.utils.text' (/usr/local/lib/python3.7/site-packages/django/utils/text.py)

管理.py:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line(sys.argv)

Dockerfile 内容:

FROM python:3.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
ADD requirements.txt /app/
RUN pip install pip==9.0.1
RUN pip install -r requirements.txt
ADD . /app/

docker-compose.yml

version: '2'
services:
  db:
    image: postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    environment:
      DATABASE_URL: postgres://app@postgres:5432/app
    depends_on:
      - db

最佳答案

django.utils.text.format_lazy该功能是在 Django 2.0 中添加的。

您应该从 2.0 升级到 Django 版本,或者将扩展降级到不依赖此功能的版本。

关于python - 无法启动docker虚拟机: ImportError: cannot import name 'format_lazy' from 'django.utils.text' (Python Error),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59612390/

相关文章:

python - 欺骗MAC地址通信行为

python - Pytorch 不能在 PyCharm/IntelliJ 中使用 CUDA

python - 如何为 POST 请求编写 Django View

Django:用户配置文件的动态 URL

python-3.x - 如何使用 graphviz 绘制 TreeMap ?

python - 如何在 Python 3.x 中强制输入整数?

python - django 国家货币代码

python matplotlib pcolor空白空间

python - 重写 save() 方法

python-3.x - 属性错误 : 'NoneType' object has no attribute 'decode'