Reputation: 41
I'm doing a project using python 3.7 and beautifulsoup4-4.8.0. I found a might-not-be-correct solution to fix the ImportError: cannot import name 'HTMLParseError' from 'html.parser'. If you could help me verify it, it would be greatly appreciated!
******************* Issue ********************
I'm outputting the original error I got here to help illustrate what I did and why I did this.
Traceback (most recent call last): File "./Youtube.py", line 1, in from bs4 import BeautifulSoup File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bs4/init.py", line 29, in from .builder import builder_registry File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bs4/builder/init.py", line 294, in from . import _htmlparser File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bs4/builder/_htmlparser.py", line 7, in from html.parser import ( ImportError: cannot import name 'HTMLParseError' from 'html.parser' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/html/parser.py)
*************** My solution *****************
In the error output, it shows that we cannot import 'HTMLParseError' from the file 'html.parser', where it was supposed to be implemented. so I did my research(https://docs.python.org/3.4/library/html.parser.html) and found out that HTMLParseError was deprecated since version 3.3, and was removed in version 3.5: It turned out that this exception was never raised by the parser using default non-strict mode (which I believe is what I'm using).
SOOOOO basically what I did was just to remove that line of code that imports the 'HTMLParseError' from 'html.parser', located in the file (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bs4/builder/_htmlparser.py)
and voilà! It erased the error. I don't know if it is technically correct. If someone can help me verify this solution, it would be really really appreciated!
Upvotes: 1
Views: 8613
Reputation: 7069
$ pip install beautifulsoup4 --force-reinstall
$ python3 -m pip install beautifulsoup4 --force-reinstall
Upvotes: 4
Reputation: 41
I was able to solve my problem by running the following command.
pip3 install —ignore-installed beautifulsoup4
My issue was that I was not using the py3 compatible version of bs4. because bs4 is distutils Installed, I had to use the —ignore-installed flag to resolve my ERROR: Cannot uninstall 'beautifulsoup4'. It is a distutils installed project...
Thanks to user2357112's comment!
Upvotes: 3