Cannot start docker VM: ImportError: cannot import name 'format_lazy' from 'django.utils.text' (Python Error)

huangapple go评论120阅读模式
英文:

Cannot start docker VM: ImportError: cannot import name 'format_lazy' from 'django.utils.text' (Python Error)

问题

无法启动Docker虚拟机,因为抛出了一个Python错误。

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

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

使用以下命令构建的docker虚拟机:


docker-compose build

docker-compose up

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

完整跟踪如下:

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)

Manage.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
英文:

I cannot start a docker VM, due to a Python error that is being thrown.

I have tried switching Python versions from 3.5 to 3.6 to 3.7.

I have connected to the VM directly, and attempted to run commands directly from the command line (eg. ./manage.py makemigrations) and received the same error. The contents of manage.py are at the bottom.

The docker virtual machine that is built using:

docker-compose pull

docker-compose build

docker-compose up

When the VM starts, the below error is shown:

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

Full trace below:


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)

Manage.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 content:

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

答案1

得分: 0

django.utils.text.format_lazy 函数是在 Django 2.0 版本中添加的。

你应该要么升级到 2.0 版本的 Django,要么将扩展降级到不依赖于这个函数的版本。

英文:

django.utils.text.format_lazy function was added in Django 2.0.

You should either upgrade to a version of Django from 2.0 or downgrade the extension to a version that doesn't rely on this function.

huangapple
  • 本文由 发表于 2020年1月6日 20:46:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定