Skip to main content
 首页 » 编程设计

python之如何在 django 中使用 HTMLParser 的另一个模块位置

2025年02月15日25java哥

我出现了 HTMLParser 错误, 在终端中显示:-

from django.utils.html import format_html, format_html_join, escape
File "/usr/local/lib/python3.5/dist-packages/django/utils/html.py", line 16, in
from .html_parser import HTMLParser, HTMLParseError
File "/usr/local/lib/python3.5/dist-packages/django/utils/html_parser.py", line 12, in
HTMLParseError = _html_parser.HTMLParseError
AttributeError: module 'html.parser' has no attribute 'HTMLParseError'

python 3.5 Django 1.7.7 使用 pip3

根据社区的说法,他们说 HTML 解析器在我这个旧应用程序的 Python 3.5 版本中被删除了。

我该怎么办:-

  • 将 HTMLParser 替换为另一个类似的 how???或者

  • 降级 python 版本我已经尝试过了,但它默认使用 lib ->/usr/lib/python3.5 如何更改此路径

请帮忙解决这个问题。

我的需求文件requirements/common.txt是这样的

# Core Stuff 
# ------------------------------------- 
django>=1.7.7,<1.8 
whitenoise==2.0.6 
 
# Authentication 
# ------------------------------------- 
django-facebook==6.0.3 
PyJWT==1.4.0 
 
# Notifications 
# ------------------------------------- 
django-twilio-sms-2==1.0.2 
hashids==1.1.0 
apns==2.0.1 
 
# Extensions 
# ------------------------------------- 
django-extensions==1.6.1 
Werkzeug==0.11.3 
pytz==2015.7 
django-sampledatahelper==0.3 
arrow==0.7.0 
requests==2.9.1 
django-flat-theme==1.1.3 
 
# Configuration 
# ------------------------------------- 
django-sites==0.9 
django-secure==1.0.1 
python-dotenv==0.3.0 
django-environ==0.4.0 
 
# Models 
# ------------------------------------- 
django-model-utils==2.4 
django-uuid-upload-path==1.0.0 
django-versatileimagefield==1.3 
django-jsonfield==0.9.15 
 
# Images 
# ------------------------------------- 
Pillow==3.1.1 
 
# For the persistance stores 
# ------------------------------------- 
psycopg2==2.6.1 
 
# Celery 
# ------------------------------------- 
celery==3.1.20 
 
# Unicode slugification 
# ------------------------------------- 
unicode-slugify==0.1.3 
django-autoslug==1.9.3 
 
# Django Rest Framework 
# ------------------------------------- 
djangorestframework==3.3.2 
django_filter<=0.11.0  # Do not upgrade until django 1.8 
Markdown>=2.3.1,<2.5   # to fix conflict with mkdocs

和requirements/development.txt

-r common.txt 
 
# Documentation 
# ------------------------------------- 
mkdocs==0.15.2 
# pygraphviz==1.3rc2 
isort==4.2.2 
 
# Debugging 
# ------------------------------------- 
django-debug-toolbar==1.4 
ipython==4.1.1 
ipdb==0.8.1 
 
# Testing 
# ------------------------------------- 
mock==1.3.0 
factory_boy==2.6.0 
flake8==2.5.2 
pytest==2.8.7 
pytest-django==2.9.1 
pytest-flakes==1.0.1 
pytest-mock==0.10.1 
pytest-pythonpath==0.7 
coverage==4.0.3 
freezegun==0.3.6 
pytest-capturelog==0.7 
 
# Versioning 
# ------------------------------------- 
bumpversion==0.5.3

和requirements/production.txt

# Pro-tip: Try not to put anything here. There should be no dependency in 
#   production that isn't in development. 
-r common.txt 
 
# WSGI Handler 
# ------------------------------------- 
gunicorn==19.4.5 
 
# Caching 
# ------------------------------------- 
django-redis==4.3.0 
hiredis==0.2.0 
 
# S3 storgage 
# ------------------------------------- 
# boto==2.39.0 
# django-storages-redux==1.3.2 
 
# Logging 
# ------------------------------------- 
newrelic==2.60.0.46

当我运行 $ python3 manage.py makemigrations 时,安装需求后,出现这个错误。

Traceback (most recent call last): 
  File "manage.py", line 22, in <module> 
    execute_from_command_line(sys.argv) 
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line 
    utility.execute() 
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 354, in execute 
    django.setup() 
  File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 18, in setup 
    from django.utils.log import configure_logging 
  File "/usr/local/lib/python3.5/dist-packages/django/utils/log.py", line 13, in <module> 
    from django.views.debug import ExceptionReporter, get_exception_reporter_filter 
  File "/usr/local/lib/python3.5/dist-packages/django/views/debug.py", line 10, in <module> 
    from django.http import (HttpResponse, HttpResponseServerError, 
  File "/usr/local/lib/python3.5/dist-packages/django/http/__init__.py", line 4, in <module> 
    from django.http.response import ( 
  File "/usr/local/lib/python3.5/dist-packages/django/http/response.py", line 13, in <module> 
    from django.core.serializers.json import DjangoJSONEncoder 
  File "/usr/local/lib/python3.5/dist-packages/django/core/serializers/__init__.py", line 23, in <module> 
    from django.core.serializers.base import SerializerDoesNotExist 
  File "/usr/local/lib/python3.5/dist-packages/django/core/serializers/base.py", line 6, in <module> 
    from django.db import models 
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/__init__.py", line 6, in <module> 
    from django.db.models.query import Q, QuerySet, Prefetch  # NOQA 
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 13, in <module> 
    from django.db.models.fields import AutoField, Empty, FieldDoesNotExist 
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 18, in <module> 
    from django import forms 
  File "/usr/local/lib/python3.5/dist-packages/django/forms/__init__.py", line 6, in <module> 
    from django.forms.fields import *  # NOQA 
  File "/usr/local/lib/python3.5/dist-packages/django/forms/fields.py", line 18, in <module> 
    from django.forms.utils import from_current_timezone, to_current_timezone 
  File "/usr/local/lib/python3.5/dist-packages/django/forms/utils.py", line 15, in <module> 
    from django.utils.html import format_html, format_html_join, escape 
  File "/usr/local/lib/python3.5/dist-packages/django/utils/html.py", line 16, in <module> 
    from .html_parser import HTMLParser, HTMLParseError 
  File "/usr/local/lib/python3.5/dist-packages/django/utils/html_parser.py", line 12, in <module> 
    HTMLParseError = _html_parser.HTMLParseError 
AttributeError: module 'html.parser' has no attribute 'HTMLParseError'

尝试引用:
AttributeError: module 'html.parser' has no attribute 'HTMLParseError' https://code.djangoproject.com/ticket/23763

请您参考如下方法:

您有 2 个机会:

  1. 使用python 3.4
  2. 将您的项目更新到 Django 1.8

使用python 3.4

如果您使用的是 Ubuntu,则可以使用此指令安装 python 3.4 - https://askubuntu.com/questions/802279/how-to-install-python-3-4-5-from-apt

然后你可以用python 3.4创建virtualenv

$ virtualenv .env -p /usr/bin/python3.4 
$ source .env/bin/activate 

将您的项目更新到 Django 1.8

这是更困难但更好的方法,因为你会得到更少的错误,获得安全补丁并且你可以使用新功能。

Here您可以阅读有关新功能以及如何升级项目的更多信息。