Python 3.5 and Django 1.7's HTMLParseError

#Python #Django

Table of Contents

I just updated my Python installation to 3.5 Alpha 1 and started with Django today and I hit a road block already. It all went fine, when I created the virtual environment and installed Django 1.7.4 in it. And then when I did startproject, I got AttributeError: module 'html.parser' has no attribute 'HTMLParseError'.\\

 1(py3.5venv) ➜  myProject git:(master) ✗ django-admin.py startproject myProject
 2Traceback (most recent call last):
 3  File "/py3.5venv/bin/django-admin.py", line 5, in
 4    management.execute_from_command_line()
 5  File "/py3.5venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
 6    utility.execute()
 7  File "/py3.5venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 354, in execute
 8    django.setup()
 9  File "/py3.5venv/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
10    from django.utils.log import configure_logging
11  File "/py3.5venv/lib/python3.5/site-packages/django/utils/log.py", line 13, in
12    from django.views.debug import ExceptionReporter, get_exception_reporter_filter
13  File "/py3.5venv/lib/python3.5/site-packages/django/views/debug.py", line 10, in
14    from django.http import (HttpResponse, HttpResponseServerError,
15  File "/py3.5venv/lib/python3.5/site-packages/django/http/__init__.py", line 4, in
16    from django.http.response import (
17  File "/py3.5venv/lib/python3.5/site-packages/django/http/response.py", line 13, in
18    from django.core.serializers.json import DjangoJSONEncoder
19  File "/py3.5venv/lib/python3.5/site-packages/django/core/serializers/__init__.py", line 23, in
20    from django.core.serializers.base import SerializerDoesNotExist
21  File "/py3.5venv/lib/python3.5/site-packages/django/core/serializers/base.py", line 6, in
22    from django.db import models
23  File "/py3.5venv/lib/python3.5/site-packages/django/db/models/__init__.py", line 6, in
24    from django.db.models.query import Q, QuerySet, Prefetch  # NOQA
25  File "/py3.5venv/lib/python3.5/site-packages/django/db/models/query.py", line 13, in
26    from django.db.models.fields import AutoField, Empty
27  File "/py3.5venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 18, in
28    from django import forms
29  File "/py3.5venv/lib/python3.5/site-packages/django/forms/__init__.py", line 6, in
30    from django.forms.fields import *  # NOQA
31  File "/py3.5venv/lib/python3.5/site-packages/django/forms/fields.py", line 18, in
32    from django.forms.utils import from_current_timezone, to_current_timezone
33  File "/py3.5venv/lib/python3.5/site-packages/django/forms/utils.py", line 15, in
34    from django.utils.html import format_html, format_html_join, escape
35  File "/py3.5venv/lib/python3.5/site-packages/django/utils/html.py", line 16, in
36    from .html_parser import HTMLParser, HTMLParseError
37  File "/py3.5venv/lib/python3.5/site-packages/django/utils/html_parser.py", line 12, in
38    HTMLParseError = _html_parser.HTMLParseError
39AttributeError: module 'html.parser' has no attribute 'HTMLParseError'

Then Antti Haapala (a friend from Stakoverflow's Python Chat room) found out that it is because HTMLParseError is deprecated from Python 3.3 onwards and removed in Python 3.5, as per the official documentation. He continued the investigation and found out that django guys are already aware of this, as they have a bug raised, and the fix has been already delivered to the code repository.
Now, all we can do is, wait for the version of django which officially supports Python 3.5 or manually patch html_parser.py file as per the above shown fix. Even if we patch it manually, there is no guarantee that everything else will work fine.\\