Modulenotfounderror: No module named 'bs4' and cannot access module bs4

Asked

Viewed 87 times

-1

I am trying to import the bs4 module into a code and every time I try the following error is displayed

ModuleNotFoundError: No module named 'bs4'

Theoretically the bs4 module has already been installed and when I run the command in the terminal pip install bs4 the following message appears:

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)

The requests module is working and when I try to install it gives the following message. I was trying to compare with module bs4 to see if they were installed in different locations but not following find error

C:\Users\gabri>pip install requests
Requirement already satisfied: requests in c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages (2.25.1)
Requirement already satisfied: idna<3,>=2.5 in c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages (from requests) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages (from requests) (2020.12.5)
Requirement already satisfied: chardet<5,>=3.0.2 in c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages (from requests) (4.0.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages (from requests) (1.26.5)

my current version of python is this:

Python 3.9.5

my current version of Pip is this:

pip 21.1.2 from c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages\pip (python 3.9)

And that’s the code I’m trying to use the remote and it’s gone wrong:

import requests as r
from bs4 import BeautifulSoup

try:
    result = r.get('https://www.google.com/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)

If i Try to install bs4 using the Commanda pip3 install bs4 i get this messages:

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)

I don’t know if that helps in any way anymore python 3.9 seems to be on its way here

C:\Users\gabri\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.9

Here is a picture of how the environment variables are configured

inserir a descrição da imagem aqui

  • Could you clarify which IDE you are using to program?

  • I’m using Thonny and I also have pycharm installed

  • Regarding the pycharm this may be the cause of the problem, because the pycharm use a virtualenv for the project (it does not share the env with the operating system), so as installed in the S.O. may be missing from the IDE’s "env".

  • But I was only using Thonny to run the codes. I installed pycharm and didn’t even use it anymore. Is the idea to uninstall pychamr? I was able to solve the problem by placing the file that wanted to run the code inside the folder that bs4 is in, but this did not seem the best way to solve the problem

  • I don’t know Thonny, I don’t know how it works. I’m sorry.

  • which python versions are installed on your pc (there is version 2.7 or 3.6) plus 3.9?

  • Every ide, whether in Setting or in another specification, has the choice of which interpreter will be used, it may have happened to be using a standard version and not the one you installed the libs to, assigning the error return, the ide may be using a different version, if that is just change which interpreter will be using.

  • I was pretty close to that. The ide was using sys.path from the wrong folders, so it tried to import modules from different folders than the ones they were installed. Since I’m quite new to programming, I had no idea about it, so thank you very much for your personal help.

Show 3 more comments

1 answer

1

With the help of @stack.Rdoso I was able to find an answer. Basically my IDE was searching for the bs4 module in a different path than the one it was installed in. To resolve this I changed the path my IDE was searching for imports in the IDE’s own settings. Now she is always searching in the same folders as Pip installs the modules. To find out which path the IDE was using I typed in the Ide console:

import sys
for sys in sys.path:
    print(sys)    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.