After installing (ubuntu) python3.9, installing some packages with pip failes on:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 14, in <module>
from setuptools.dist import Distribution, Feature
File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 24, in <module>
from setuptools.depends import Require
File "/usr/lib/python3/dist-packages/setuptools/depends.py", line 7, in <module>
from .py33compat import Bytecode
File "/usr/lib/python3/dist-packages/setuptools/py33compat.py", line 54, in <module>
unescape = getattr(html, 'unescape', html_parser.HTMLParser().unescape)
AttributeError: 'HTMLParser' object has no attribute 'unescape'
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
asked Jan 9, 2021 at 7:49
3
After some trial and error I upgraded, pip, distlib and setuptools and it solved it. Not sure which of those is causing it. (On the last two I found issues 1 2 of other sites)
It is caused by removing unescape from HTMLParser in python3.9, which seems to break setuptools.
pip3 install --upgrade setuptools
If it does not work, try also:
pip3 install --upgrade pip
pip3 install --upgrade distlib
Note from @seb comment: The default pip3 may not be the python you are using. If so, try pip of your specific version used (e.g. pip3.9)
answered Jan 9, 2021 at 7:49
borgrborgr
19k6 gold badges25 silver badges35 bronze badges
6
attributeerror: htmlparser object has no attribute unescape error occurs because of incompatibility in python version 3.9 version. Actually, unescape is removed from htmlparser module in Python 3.9.x version series. This creates incompatibility if we run same code with python 3.9.x series. In this article, we will address the best and easiest way to fix this htmlparser object has no attribute unescape error.
We can fix this issue from the multiple ways. All you need to understand them and find the easiest from your context.
Solution 1: Downgrade the python 3.9 to a lower version ( >3.4 ) –
Since this incompatibility is because of python version 3.9. As you already know unescape module is no more part of htmlparser from this version. Hence we will downgrade our python version. You can directly install the lower version, It will replace the existing python version with the lower version. But keep the current module > 3.4 to avoid other incompatibility issues. You may try to use any virtual environment to keep other versions stable.
I know you are thinking why version >3.4 one. The reason is simple, This function was first introduced in Python 3.4 series. Hence if you downgrade the python version lower to it, you will again face this same incompatibility error.
Solution 2: upgrading setuptools –
IF you change multiple configurations to fix this error. Which sometime misconfigures the setup files. In this situation, once we are landed over the python stable version. We should upgrade setuptools and related files. You may try the below command-
pip3 install --upgrade setuptools
pip3 install --upgrade pip
pip3 install --upgrade distlib
It will definitely fix the error for you. Also do not forget to close the current terminal and restart it once before running your script again.
I hope now you can easily fix this error. Please comment if you are facing any issues in fixing the same error.
Thanks
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
We respect your privacy and take protecting it seriously
Thank you for signup. A Confirmation Email has been sent to your Email Address.
Something went wrong.
🚨Please review the Troubleshooting section
before reporting any issue. Don’t forget to check also the current issues to
avoid duplicates.
AttributeError
Receiving the following error:
AttributeError: ‘HTMLParser’ object has no attribute ‘unescape’
Your environment
- Operating System (name/version): Windows V.2004
- Python version: 3.9
- coursera-dl version: 0.11.5
Steps to reproduce
Method:
coursera-dl regression-models
- Is the problem happening with the latest version of the script?
- Do you have all the recommended versions of the modules? See them in the
filerequirements.txt
. - What is the course that you are trying to access?
- What is the precise command line that you are using (don’t forget to obfuscate
your username and password, but leave all other information untouched). - What are the precise messages that you get? Please, use the
--debug
option before posting the messages as a bug report. Please, copy and paste
them. Don’t reword/paraphrase the messages.
Expected behaviour
Tell us what should happen.
Actual behaviour
C:Usersryan1Documents>coursera-dl regression-models
coursera_dl version 0.11.5
Downloading class: regression-models (1 / 1)
Parsing syllabus of on-demand course . This may take some time, please be patient …
Processing module week-1-least-squares-and-linear-regression
Processing section introduction
Processing lecture welcome-to-regression-models (supplement)
Traceback (most recent call last):
File «c:usersryan1appdatalocalprogramspythonpython39librunpy.py», line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File «c:usersryan1appdatalocalprogramspythonpython39librunpy.py», line 87, in run_code
exec(code, run_globals)
File «C:Usersryan1AppDataLocalProgramsPythonPython39Scriptscoursera-dl.exe_main.py», line 7, in
File «c:usersryan1appdatalocalprogramspythonpython39libsite-packagescourseracoursera_dl.py», line 247, in main
error_occurred, completed = download_class(
File «c:usersryan1appdatalocalprogramspythonpython39libsite-packagescourseracoursera_dl.py», line 214, in download_class
return download_on_demand_class(session, args, class_name)
File «c:usersryan1appdatalocalprogramspythonpython39libsite-packagescourseracoursera_dl.py», line 134, in download_on_demand_class
error_occurred, modules = extractor.get_modules(
File «c:usersryan1appdatalocalprogramspythonpython39libsite-packagescourseraextractors.py», line 53, in get_modules
error_occurred, modules = self._parse_on_demand_syllabus(
File «c:usersryan1appdatalocalprogramspythonpython39libsite-packagescourseraextractors.py», line 161, in _parse_on_demand_syllabus
links = course.extract_links_from_supplement(
File «c:usersryan1appdatalocalprogramspythonpython39libsite-packagescourseraapi.py», line 1268, in extract_links_from_supplement
supplement_content, self._extract_links_from_text(value))
File «c:usersryan1appdatalocalprogramspythonpython39libsite-packagescourseraapi.py», line 1518, in _extract_links_from_text
supplement_links = self._extract_links_from_a_tags_in_text(text)
File «c:usersryan1appdatalocalprogramspythonpython39libsite-packagescourseraapi.py», line 1597, in _extract_links_from_a_tags_in_text
extension = clean_filename(
File «c:usersryan1appdatalocalprogramspythonpython39libsite-packagescourserautils.py», line 118, in clean_filename
s = h.unescape(s)
AttributeError: ‘HTMLParser’ object has no attribute ‘unescape’
This attributeerror: htmlparser object has no attribute unescape error occurs while installing some packages in python with the help of the pip command in the terminal. The main cause is that while writing the command we write
unescape = getattr(html, ‘unescape’, html_parser.HTMLParser().unescape)
Which returns an error stating “attributeerror: htmlparser object has no attribute unescape” because HTMLParser object has no parameter or attribute named unescape.
The error looks like the statements as written below.
File "/usr/lib/python3/dist-packages/setuptools/py33compat.py", line 54, in <module> unescape = getattr(html, 'unescape', html_parser.HTMLParser().unescape) AttributeError: 'HTMLParser' object has no attribute 'unescape'
Solution of error
To solve this error it is best to upgrade not only pip but distlib and setuptools also. The main reason is that as the python version upgraded the attribute named unescape in HTMLparser is no longer in use. By upgrading these modules you are able to overcome this error positively.
Setuptools is a group of improvements to the Python distutils that make it simpler for programmers to create and distribute Python packages, particularly those that depend on other packages. Users perceive setuptools-created and -distributed packages as being typical Python packages based on the distutils library.
pip3 install --upgrade setuptools
If the above code does not work try this also.
The default package manager for Python is called pip. Additional packages that are not a part of the Python standard library are available for installation and management.
A library called Distlib supports low-level operations related to Python program packaging and distribution. It is meant to serve as the foundation for packaging tools made by other companies.
pip3 install --upgrade pip pip3 install --upgrade distlib
Also Read: Error: java: package org.junit.jupiter.api does not exist
The HTMLParser
module in Python is a useful tool for parsing HTML content. However, you may encounter an AttributeError
when using the unescape
method with an HTMLParser
object. In this guide, we’ll show you how to resolve this issue step-by-step and provide some frequently asked questions for further clarification.
Table of Contents
- Understanding the Issue
- Step-by-Step Solution
- FAQ
- Related Links
Understanding the Issue
The AttributeError
occurs when you attempt to use the unescape
method with an HTMLParser
object, as shown in the code below:
from html.parser import HTMLParser
parser = HTMLParser()
text = "This is an example 'string' with HTML entities."
result = parser.unescape(text)
The error message will look like this:
AttributeError: 'HTMLParser' object has no attribute 'unescape'
This issue arises because the unescape
method was removed from the HTMLParser
class in Python 3.4.
Source: Python documentation
Step-by-Step Solution
To resolve the AttributeError
, you’ll need to use the html
module’s unescape
function instead of the HTMLParser
object’s unescape
method. Here’s how you can do it:
- Import the
html
module: Replace thehtml.parser
import statement with thehtml
module.
import html
- Use the
unescape
function: Use theunescape
function from thehtml
module to decode HTML entities in your text.
text = "This is an example 'string' with HTML entities."
result = html.unescape(text)
Your final code should look like this:
import html
text = "This is an example 'string' with HTML entities."
result = html.unescape(text)
print(result)
Output:
This is an example 'string' with HTML entities.
With these changes, you should no longer encounter the AttributeError
.
FAQ
Why was the unescape
method removed from the HTMLParser
class?
The unescape
method was removed because its functionality was moved to the html
module, which provides a more general-purpose solution for handling HTML entities. This change makes the HTMLParser
class more focused on parsing HTML content.
Can I use the html
module’s unescape
function with Python 2.x?
No, the html
module is not available in Python 2.x. Instead, you can use the HTMLParser
class’s unescape
method, which is available in Python 2.x but deprecated in Python 3.x.
What other functions does the html
module provide?
The html
module provides two main functions: escape
and unescape
. The escape
function is used to replace special characters in a string with their corresponding HTML entities, while the unescape
function is used to replace HTML entities with their corresponding characters.
How can I ensure my code works with both Python 2.x and Python 3.x?
You can use a conditional import statement and a wrapper function to ensure your code works with both Python 2.x and Python 3.x:
import sys
if sys.version_info[0] < 3:
from HTMLParser import HTMLParser
unescape = HTMLParser().unescape
else:
import html
unescape = html.unescape
This code snippet checks the Python version and imports the appropriate module and function based on the version.
Can I use the unescape
function to decode other types of entities, such as XML entities?
No, the unescape
function is specifically designed for decoding HTML entities. To decode XML entities, you can use the xml.sax.saxutils
module’s unescape
function.
- Python HTMLParser documentation
- Python html module documentation
- Python 2.x HTMLParser documentation
- Python xml.sax.saxutils documentation