Beautiful Soup not working in Pycharm - python

I am trying to import beautiful soup in my script, but get a module not found error.
Error Message
But the problem is that when i try to install it, i get the message below:
Ajays-MacBook-Pro:/ aj$ pip install bs4
Requirement already satisfied: bs4 in /anaconda/lib/python2.7/site-packages
Requirement already satisfied: beautifulsoup4 in /anaconda/lib/python2.7/site-packages (from bs4)
What am I doing wrong here?
Note: I tried importing bs4 in Jupyter notebook and it works without any issues.

You use different version of Python in the PyCharm, you should add the Python in the /anaconda folder in the PyCharm settings.

You just need to install BeautifulSoup4 from the terminal in pycharm.
Then when you try to import it in your code you need to change this statement
from BeautifulSoup import BeautifulSoup
to this
from bs4 import BeautifulSoup
That error will be solved.

Related

Bs4 error possible from wrong pip installation location, i kind solved but not in the pythonic way

Every time when i try to import the bs4 module i get this error
ModuleNotFoundError: No module named 'bs4'
When i try to install the bs4 module i get this message on the console:
C:\Users\gabri>pip install bs4
Requirement already satisfied: bs4 in c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages (0.0.1)
Requirement already satisfied: beautifulsoup4 in c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages (from bs4) (4.9.3)
Requirement already satisfied: soupsieve>1.2 in c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages (from beautifulsoup4->bs4) (2.2.1)
When i try to import the requests module that's working i get this message:
C:\Users\gabri>pip install bs4
Requirement already satisfied: bs4 in c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages (0.0.1)
Requirement already satisfied: beautifulsoup4 in c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages (from bs4) (4.9.3)
Requirement already satisfied: soupsieve>1.2 in c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages (from beautifulsoup4->bs4) (2.2.1)
My python version is:
Python 3.9.5
My pip version is:
pip 21.1.2 from c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages\pip (python 3.9)
I used to have anaconda installed and then i installed python again. I read in some post that maybe the problem was happening because of two versions of python installed than i uninstall anaconda and jupyter. However nothing change. Some people said to me that my pip was install in another python besides the one i am trying to run, but i dont know how to change that. The code i'm trying to run in this
import requests as r
from bs4 import BeautifulSoup
try:
result = r.get('https://www.google.com.br/search?q=Python')
except Exception as err:
print("Something went wrong: ",err)
else:
response = result.text
soup = BeautifulSoup(response,'html.parser')
print(soup.title)
print(soup.title.string)
And the error is this:
>>> %Run teste.py
Traceback (most recent call last):
File "C:\Users\gabri\teste.py", line 2, in <module>
from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'
I think that my pip is install in this path:
pip 21.1.2 from c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages\pip (python 3.9)
and my python is install in this path:
C:\Users\gabri\AppData\Local\Programs\Python\Python39\python39.zip
C:\Users\gabri\AppData\Local\Programs\Python\Python39\DLLs
C:\Users\gabri\AppData\Local\Programs\Python\Python39\lib
C:\Users\gabri\AppData\Local\Programs\Python\Python39
C:\Users\gabri\AppData\Local\Programs\Python\Python39\lib\site-packages
Thats a picture of my paths on windows
enter image description here
I try put the file with the code that i'm trying to run inside the path of bs4 and it worked. But i would like to know if there is an easy method besides that
So with the helping of #He3lixx i found the answer to the question. My IDE, Thonny was using the wrong sys path to import the bs4 module. The module that pip install bs4 was in the python installation folders. So i change the paths imports in the IDE settings. To figure out what paths the IDE was using and compare to the path the bs4 was installed i use this code in the IDE shell:
import sys
for sys in sys.path:
print (sys)

BeautifulSoup not working in google colab

I have tried the below code in google colab but it gives an error which i have also shown below
!pip install beautifulsoup4
Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.7/dist-packages (4.6.3)
from beautifulsoup4 import BeautifulSoup
ModuleNotFoundError Traceback (most recent call last) in ()
----> 1 from beautifulsoup4 import BeautifulSoup
ModuleNotFoundError: No module named 'beautifulsoup4'
--------------------------------------------------------------------------- NOTE: If your import is failing due to a missing package, you can manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the "Open Examples" button below.
Than i also tried
!apt install BeautifulSoup4
E: Unable to locate package BeautifulSoup4
!apt install BeautifulSoup
E: Unable to locate package BeautifulSoup
I have also tried the above commands without exclamation mark (!). other packages were installed but beautifulsoup4 is not installing neither it is allowing to import.
I appreciate if someone can guide me regarding this.
for importing BeautifulSoup use that one:
from bs4 import BeautifulSoup
there's usual case a different package naming while installing and the step of importing/using
Instead of
from beautifulsoup4 import BeautifulSoup
Try out
from bs4 import BeautifulSoup
! pip install bs4
It worked very well in Google colaboratory.

ImportError: No module named bs4 - despite bs4 and BeautifulSoup being installed

I downloaded Python 3.7 and am running a script with "from bs4 import BeautifulSoup" and am receiving the following error on execution;
"File "myscript.py", line 3, in
from bs4 import BeautifulSoup ImportError: No module named bs4"
When I type "pip3 install bs4" or "pip3 install BeautifulSoup4" in the terminal I get the following;
"Requirement already satisfied: bs4 in
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
(0.0.1) Requirement already satisfied: beautifulsoup4 in
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
(from bs4) (4.6.3)"
Executing "import bs4" and "from bs4 import BeautifulSoup" in IDLE don't error out.
Any idea about what's going on?
Just pip install bs4.
Probably, you are maintaining different versions of Python.
check if you have more than one version of python, if so add the path of python 3.7 in the system setting and try removing the older python if possible
and then pip install BeautifulSoup
I had a similar problem with importing bs4. There is a similar question with many answers. I tried many things and nothing worked but when I installed poetry [https://python-poetry.org/ ]
$ poetry add pendulum
It sorted dependencies for me so I was able to import bs4.

html5lib installed but BeautifulSoup cannot find it

I have installed the html5lib package. I'm sure because when i try to install it, i get a message that it is already installed.
pip install html5lib
Requirement already satisfied: html5lib in ./anaconda/lib/python3.5/site-packages
Also i am able to successfully import that package.
import html5lib
Yet when I try to use the parser as a part of the BeautifulSoup constructor
soup = BeautifulSoup(response.data, 'html5lib')
I'm unsuccessful
FeatureNotFound: Couldn't find a tree builder with the features you requested: html5lib. Do you need to install a parser library?
Is it possible to force bs4 to lookup the installed parsers and successfully find html5lib?
Took Megalng's suggestion and restarted the IDE. That did the trick!

How can I from bs4 import BeautifulSoup?

This code:
from bs4 import BeautifulSoup
Doesn't work, and gives this error :
raise AttributeError, "'%s' object has no attribute '%s'" % (self.__class__.__name__,attr)
^
SyntaxError: invalid syntax
What should i do ?
You should be using pip to install, so you can simply do
pip install beautifulsoup4
That will install the latest BS4, which is 4.3.1 as of 2013-08-15. It supports Python 3.
Also, if you are using python3, you should use:
pip3 install beautifulsoup4
For Windows...
Go to start menu type cmd right click on cmd icon click run as administrator
then type pip install beautifulsoup4.
It likely will fail to install correctly if you fail to do the above step as even though your windows user is an admin account it does not run all apps as administrator.
Notice the difference if you simply just open cmd without the run as admin.
Remember also when using it like so...
from bs4 import beautifulsoup4
Will not work as it is not correctly formatted.
from bs4 import BeautifulSoup4
Will work correctly as it is CaseSensitive.
In your terminal run below code:
pip install beautifulsoup4
If you are using Jupyter notebook run below code in your python file not in terminal.
!pip install beautifulsoup4
if it successfully install you get below output:
Requirement already satisfied: beautifulsoup4 in
c:\users\anaconda3\lib\site-packages (4.11.1)
Then run your code:
from bs4 import BeautifulSoup

Categories