Importing bs4 in Python 3.5 - python

I have installed both Python 3.5 and Beautifulsoup4. When I try to import bs4, I get the error below. Is there any fix for that? Or should I just install Python 3.4 instead?
Please be very explicit - I am new to programming. Many thanks!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python 3.5\lib\sit-packages\bs4\__init__.py", line 30, in <module>
from .builder import builder_registry, ParserRejectionMarkup
File "C:\Python 3.5\lib\sit-packages\bs4\__init__.py", line 308, in <module>
from . import _htmlparser
File "C:\Python 3.5\lib\sit-packages\bs4\_htmlparser.py", line 7, in <module>
from html.parser import (
ImportError: cannot import name 'HTMLParseError'

Update: Starting with 4.4.0, BeautifulSoup is compatible with Python 3.5. Upgrade:
pip install --upgrade beautifulsoup4
Old answer:
Because of the changes made for Deprecate strict mode of HTMLParser issue:
Issue #15114: the strict mode and argument of HTMLParser,
HTMLParser.error, and the HTMLParserError exception have been removed.
I'm afraid beautifulSoup4 is not compatible with Python 3.5 at the moment. Use Python 3.4.

Update: BeautifulSoup 4.4.0 has been updated to be python3.5 compatible, so a pip install --upgrade beautifulsoup4 should do the trick if you are still hitting this issue.

I've sent the author a followup about this bug. If you want to install BeautifulSoup on Python 3.5a, I've uploaded a working patch of the source code to github.
https://github.com/jjangsangy/BeautifulSoup4
You can install it using setup.py or just copy & paste this code into terminal.
git clone https://github.com/jjangsangy/BeautifulSoup4 \
&& cd BeautifulSoup4 \
&& python3.5 setup.py install
I'm assuming here that since you're trying out 3.5a your python interpreter is installed with proper user permissions for your site-packages directory so no sudo invocation is necessary.

Related

Pycharm: Fails to make venv based on python 3.9.01+ [duplicate]

I setup a new Debian 10 (Buster) instance on AWS EC2, and was able to install a pip3 package that depended on netifaces, but when I came back to it the next day the package is breaking reporting an error in netifaces. If I try to run pip3 install netifaces I get the same error:
~$ pip3 install netifaces
Collecting netifaces
Using cached https://files.pythonhosted.org/packages/0d/18/fd6e9c71a35b67a73160ec80a49da63d1eed2d2055054cc2995714949132/netifaces-0.10.9.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 20, in <module>
from setuptools.dist import Distribution, Feature
File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 35, 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 55, in <module>
unescape = getattr(html, 'unescape', html_parser.HTMLParser().unescape)
AttributeError: 'HTMLParser' object has no attribute 'unescape'
HTMLParser().unescape was removed in Python 3.9. Compare the code in Python 3.8 vs Python 3.9.
The error seems to be a bug in setuptools. Try to upgrade setuptools. Or use Python 3.8.
I was facing this issue in PyCharm 2018. Apart from upgrading setuptools as mentioned above, I also had to upgrade to PyCharm 2020.3.4 to solve this issue. Related bug on PyCharm issue tracker: https://youtrack.jetbrains.com/issue/PY-39579
Hope this helps someone avoid spending hours trying to debug this.
I had python3.6 and related packages through deb management.
Needed python3.9 for side project and the solution to fix pip and AttributeError: 'HTMLParser' object has no attribute 'unescape'
was to update pip for python3.9 locally for one user:
python3.9 -m pip install --upgrade pip
now installing python3.9 version of the pip-packages work:
python3.9 -m pip install --target=~/.local/lib/python3.9/site-packages numpy
Downgrading to any older python3 version is not the solution and most of the time upgrading setuptools won't fix the issue. The proper solution that worked for me to work with pip using python3.9 is the following on Ubuntu18:
locate /usr/lib/python3/dist-packages/setuptools/py33compact.py33
and change
# unescape = getattr(html, 'unescape', html_parser.HTMLParser().unescape) # comment out this line
unescape = getattr(html, 'unescape', None)
if unescape is None:
# HTMLParser.unescape is deprecated since Python 3.4, and will be removed
# from 3.9.
unescape = html_parser.HTMLParser().unescape

Python3.7 on Windows Ubuntu Bash shell ImportError: cannot import name 'HTTPSHandler' from 'urllib.request' (/usr/lib/python3.7/urllib/request.py)

I've recently updated Python to version 3.7 from version 3.5, and made it the default in my bash shell. I've also updated pip and made sure it uses Python3.7.
Now, whenever I try to install any module using pip I get this error"
$pip3 install google
Traceback (most recent call last): File "/home/AnnaK/.local/bin/pip3", line 7, in <module>
from pip._internal.cli.main import main File "/usr/lib/python3/dist-packages/pip/__init__.py", line 16, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa File "/usr/lib/python3/dist-packages/pip/vcs/subversion.py", line 9, in <module>
from pip.index import Link File "/usr/lib/python3/dist-packages/pip/index.py", line 30, in <module>
from pip.wheel import Wheel, wheel_ext File "/usr/lib/python3/dist-packages/pip/wheel.py", line 39, in <module>
from pip._vendor.distlib.scripts import ScriptMaker File "/usr/share/python-wheels/distlib-0.2.2-py2.py3-none-any.whl/distlib/scripts.py", line 14, in <module> File "/usr/share/python-wheels/distlib-0.2.2-py2.py3-none-any.whl/distlib/compat.py", line 66, in <module> ImportError: cannot import name 'HTTPSHandler' from 'urllib.request' (/usr/lib/python3.7/urllib/request.py)
I get the same error if I try to simply run pip. This didn't happen when I was using python3.5. I did have this problem when briefly using python2.7, but I fixed it using https://askubuntu.com/questions/581999/pip-raises-importerror-cannot-import-name-httpshandler. That solution doesn't seem to work for python3.7.
I've tried to reinstall pip, reinstall python3, update my Windows Ubuntu subsystem, and reinstall libssl-dev. I'm stumped. Any help is appreciated!
You must have inadvertently upgraded your system pip (probably through something like sudo pip install pip --upgrade)
pip x.x adjusts where its internals are situated. The pip3 command you're seeing is one provided by your package maintainer and is not a file managed by pip.
You can read more about this on pip's issue tracker
You'll probably want to not upgrade your system pip and instead use a virtualenv.
To recover the pip3 binary you'll need to sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall.
It should work fine if you use the first approach python3 -m pip ... instead of pip3 ....

python telegram bot - bot run error

When I try to run a bot, I get this error message in the terminal:
zass#zass-system-product-name:~/python-telegram-bot/examples$python
echobot.py
/usr/local/lib/python2.7/dist-packages/python_telegram_bot-8.0-
py2.7.egg/telegram/utils/request.py:38: UserWarning: python-telegram-
bot wasn't properly installed. Please refer to README.rst on how to
properly install.
Traceback (most recent call last):
File "echobot.py", line 8, in <module>
import telegram
File "build/bdist.linux-x86_64/egg/telegram/__init__.py", line 94, in
<module>
File "build/bdist.linux-x86_64/egg/telegram/bot.py", line 34, in
<module>
File "build/bdist.linux-x86_64/egg/telegram/utils/request.py", line
33, in <module>
ImportError: No module named ptb_urllib3.urllib3
I'm running Python 2.7 on ubuntu, git and telegram bot are installed. - How can I fix it?
First of all make sure you have urllib3 installed. Sometimes urllib3 installation breaks. If for some reason your install of urllib3 is failing to include the util submodule, you could simply download the archive from the pypi page and copy the util folder from there to your urllib3 install location.
You may also issue the command below to get an up-to-date version:
sudo pip install urllib3 --upgrade
Then, see where your module is by starting a python interpreter and check where the urllib3 module is being loaded from:
python -c "import urllib3; print urllib3.__file__"
If all the above didn't work for you, make sure you're importing it right! and make sure there is no conflict.
If you clone the python-telegram-bot repo you need to run the following command before installing the code:
git submodule update --init --recursive
Then install the code with python setup.py install
Maybe you have 2 version of python and default version is 3.x
Try to check version of python
python -V
if default version is 3 then run your code with python version 2.
python2 echobot.py
it can be python2 or python2.7
Also it can be cause of versions of libs

How to install PyRTF with Python3?

I tried to install PyRTF from PyPi but this doesn't work.
The package itself can be found:
$ pip3 search PyRTF
PyRTF (0.45) - PyRTF - Rich Text Format Document Generation
But installing it fails:
$ sudo -H pip3 install PyRTF
Collecting PyRTF
Could not find a version that satisfies the requirement PyRTF (from versions: )
No matching distribution found for PyRTF
I am confused.
There is a Python 3 version of PyRTF called PyRTF3. You can find it Here.
PS - Use pip install PyRTF3 or pip install rtfw for Python 2, pip install PyRTF doesn't work.
You could use the raw url from the sourceforge page in order to get it:
pip install https://sourceforge.net/projects/pyrtf/files/pyrtf/0.45/PyRTF-0.45.tar.gz/download
But, to be honest, I don't think this library is maintained on the project website the last update was from 2005, 11 years ago.. Additionally, it uses a line of the form:
from types import StringType
which only exists in Python 2.x not 3.x. Importing it raises that specific error too:
>>> import PyRTF
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/jim/anaconda3/lib/python3.5/site-packages/PyRTF/__init__.py", line 1, in <module>
from PropertySets import *
File "/home/jim/anaconda3/lib/python3.5/site-packages/PyRTF/PropertySets.py", line 12, in <module>
from types import StringType
ImportError: cannot import name 'StringType'
So maybe it was designed for Python 2.x? I can't remember when StringType was removed from the 3.x series, maybe it targeted an old version of it.

python 2.7.10 issues about import bs4

I installed bs4 successfully but when I import it, the command line told me that
Traceback (most recent call last):
Python Shell, prompt 3, line 1
File "C:\Python27\Lib\site-packages\bs4\__init__.py", line 303, in <module>
from . import _htmlparser
File "C:\Python27\Lib\site-packages\bs4\_htmlparser.py", line 36, in <module>
from bs4.builder import (
ImportError: No module named builder
I have searched google but I didn't find a solution..
Could our experts help me on this issue ?
thanks a lot !
my system info:
PC OS : windows 7 64bit
Python version: 2.7.10
You must first pip install beautifulsoup4, then try import bs4. If this doesn't work odds are you have a messed up pip configuration. In order to remedy this either reinstall pip, use easy_install or build from source.
In order to use easy_install just run easy_install beautifulsoup4. In order to build from source run download and extract this zip (unzip /path/to/beautifulsoup4.zip if you're at the terminal). Next cd into the now unzipped folder by doing cd /path/to/beautifulsoup and run python setup.py. The package will now be installed and ready to import!
I uninstalled bs4 package and re-installed it. and now it works...
It is quite weird because I tried to uninstall and re-install, but only this time, it worked...
Thanks for your kind help :)

Categories

Resources