1

I wrote a simple python program that I learned from Mosh Hamedani course.

  • Operating System: Windows10, 64bit
  • Editor: VSCode
  • Python: 3.9.0

Installed Pythons

1- I created a folder called "PyCrawler".

2- Then in my project directory, using terminal, run these commands one by one:

  pipenv install beautifulsoup4

  pipenv install requests

3- Then it give me an error to install autopep8, so I installed that.

4- created a file named "app.py"

5- selected correspond venv.

6- write these code inside app.py

import requests
from bs4 import BeautifulSoup

response = requests.get("http://stackoverflow.com/questions")
soup = BeautifulSoup(response.text, "html.parser")

questions = soup.select(".question-summary")
print(questions[0].select_one(".question-hyperlink").getText())

7- when I run this program using "python app.py" command it gives me "ModuleNotFoundError: No module named 'bs4' " error.

a screenshot from the error that I got

It's strange. I installed beautifulsoup4, why I got this error?!!

Solutions in other questions that have been asked didn't help me.

I got same issue for selenium.

I think if I install any package I get this issue and I don't know why.

Thanks for your helps in advance.

z.ghane
  • 151
  • 1
  • 15
  • it's quite likely that you have more than one python intallation, try printing the actual path using VSCode and [somthing like this](https://stackoverflow.com/a/60950452/11261546) – Ivan Apr 19 '21 at 11:22
  • Try `pip3env` to install the dependencies. Because you using python version 3 – Dilux Apr 19 '21 at 11:23
  • did the path of your intallation matrch with the path you printed with `sys.path` ? – Ivan Apr 19 '21 at 17:35
  • @Ivan when I'm in my project folder in VSCode, I commented out all code and import sys and print(sys.path), it show me ... (next comment) – z.ghane Apr 20 '21 at 06:05
  • ['path to my project', 'C:\\Users\\Acer\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip', 'C:\\Users\\Acer\\AppData\\Local\\Programs\\Python\\Python39\\DLLs', 'C:\\Users\\Acer\\AppData\\Local\\Programs\\Python\\Python39\\lib', 'C:\\Users\\Acer\\AppData\\Local\\Programs\\Python\\Python39', 'C:\\Users\\Acer\\AppData\\Roaming\\Python\\Python39\\site-packages', 'C:\\Users\\Acer\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages'] I think they are paths to original python that I've installed on my machine, not python in venv. – z.ghane Apr 20 '21 at 06:08
  • maybe try install `pipenv install bs4` – George Imerlishvili Apr 20 '21 at 07:47
  • @Zighol check with below link to sove your issue. https://stackoverflow.com/questions/11783875/importerror-no-module-named-bs4-beautifulsoup – Dilux Apr 20 '21 at 10:19
  • pipenv install bs4 did not solve the issue. now I'm sure it's not specific to a package, so please consider this. installing requests package and then import that throw the same issue too. – z.ghane Apr 20 '21 at 10:30

1 Answers1

2

You are probably installing the packages to a different version of Python than the one you are using to run your program. Before you run your program, enter the command

$ pipenv shell

into your terminal to activate the enviornment. Then

$ python app.py

should work. For more information, see the documentation for pipenv.

Erik André
  • 528
  • 4
  • 13