0

Python version: 3.9.5

pip version: 21.1.1

BeautifulSoup4 version: 4.9.3

from bs4 import BeautifulSoup

with open('home.html', 'r') as html_file:
    content = html_file.read()
    print(content)

I have been trying to use the BeautifulSoup4 library but it just won't work. In vscode, it shows the indication that bs4 is there when I do CTRL+click on bs4 written in Code. But it still gives ModuleNotFoundError: No module named 'bs4'

I have installed it properly with pip3 as shown in the below screenshot

enter image description here

The version of python I am using in VS Code is shown in below Image enter image description here

Just a side note. I also used some other packages such as Camelcase and that one worked fine. Idk why this one is not working properly. I couldn't find any proper related solution for it in the existing solutions for it.

  • 1
    Have you tried to run python from cmd and import bs4? That will help you figure out where exactly the problem is. Also - any chance you are using a venv on vscode? – Y.R. Jun 24 '21 at 07:55
  • I think no I am not using any virtual enviroment .. I have Anaconda installed by as I have shown u in Screen shot from Vs Code . I am currently using default installed python3.9.5 – Muhammad Umar Khan Jun 24 '21 at 07:59
  • @Y.R. I just did run python3 from cmd and then types `from bs4 import BeautifulSoup4` and it gave me `ModuleNotFoundError: No module named 'bs4'` – Muhammad Umar Khan Jun 24 '21 at 08:02
  • I did Ctrl+F5 on VSCode and it runs code but when I do `python3 main.py` it doesn't work and gives the error. Idk what is going on. – Muhammad Umar Khan Jun 24 '21 at 08:11
  • I would suggest debugging using python on cmd. Do you have a few versions of python3 installed? Maybe packages are being downloaded to the wrong python dir. Check out https://stackoverflow.com/questions/6767283/find-where-python-is-installed-if-it-isnt-default-dir – Y.R. Jun 24 '21 at 08:25
  • hey. the issue got resolved. thanks allot. ur first hint was really helpful cause I figured it out for what might be the cause of it. it was because of `python` and `python3` paths and the python among installed I was using. – Muhammad Umar Khan Jun 24 '21 at 08:33
  • Great :) happy that helped. – Y.R. Jun 24 '21 at 08:37

1 Answers1

0

I had python3 installed 2 times.. 1 from the Windows store and the other from python exe file. python3 command referred to windows installed python3 and python referred to exe file installed python3. BeautifulSOup installed via pip3 was linked to the python command instead of python3 so that was the reason it wasn't working.

after checking for paths of python and python3 everything got clarified and the problem solved thanks to the hint given by @Y.R in the comments section of the main post.