Beautifulsoup4 won't import

I’m learning Python3 and am trying to run a Python script that Dr. Serverence has in a file called urllinks,py. It wants me to import it, but it keeps erroring out. I checked and the large text below shows that it is installed in Python. I cannot find an email or a link to contact him, the only one I found is on Twitter, and it won’t take a message. I’ve tried his
install proceedue and it hasn’t helped. I’m pretty computer literate, but I’m out of ideas… Please help

The file:
File “C:\py\freecodecamp\urllinks.py”, line 6, in

The part of the script making the request:
import urllib.request, urllib.parse, urllib.error
import BeautifulSoup # ( I even tried lower case beautifulsoup and adding a 4 at the end)

The error code I get:
Traceback (most recent call last):
import urllib.request, urllib.parse, urllib.error
File “C:\py\freecodecamp\urllinks.py”, line 6, in
import BeautifulSoup
ModuleNotFoundError: No module named ‘BeautifulSoup’

Feedback from the command line telling me that beautifulsoup4 is already installed:

:\Users\tom>pip install beautifulsoup4
Requirement already satisfied: beautifulsoup4 in c:\users\tom\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (4.9.3)
Requirement already satisfied: soupsieve>1.2 in c:\users\tom\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (from beautifulsoup4) (2.1)

1 Like

Beautiful Soup is imported using bs4 as a module name. BeautifulSoup is class which can be imported from that library.

1 Like

Thank you for the reply… however it explains the concept… I need help when that concept is not working. It calls the import from bs4, but that folder after unzipping has no file by that name, so it does not get called.

1 Like

If Beautiful Soup is installed with pip it should be available in python globally, regardless of from which folder script is run. As long as python interpreter used is the one for which pip installed it. This can be checked by running python in command line (just with python) command and then trying to import module. Successful importing will not show any information, rather it will just not show error.

1 Like

This was at the bottom of my original post… it shows that beautifulsoup4 is already installed via pip… just can’t call it from the script???

Feedback from the command line telling me that beautifulsoup4 is already installed:

:\Users\tom>pip install beautifulsoup4
Requirement already satisfied: beautifulsoup4 in

c:\users\tom\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (4.9.3)

1 Like

@alfonsi4
From your example it looks as if you are trying to import
BeautifulSoup and not BeautifulSoup4.

1 Like

Brandon, thanks for your help. That is what was in the script. I did try adding the 4, using lower case since my install query responded that a lower case version was installed, and other spellings to see if they would work. Unfortunately they did not. I am a Systems Engineer … which only means I’ve tried to hack a lot things… this one is getting the bettor of me.

1 Like

You must have a problem with the path. Your program is not able to locate the BeautifulSoup4 executable. Check your path or folder structure.

1 Like

Once again, just to confirm, with what line are you trying to import it?

1 Like

I am seeing different paths in the example you posted.
“C:\py\freecodecamp\urllinks.py”

But you are installing beautifulsoup4 in
"C:\Users\tom pip install beautifulsoup4"

You should create a directory for each application, create a virtual environment in the directory, then install your packages in that same directory.

# Make a directory.
$ mkdir my_project/

# Go into the directory.
$ cd my_project/

# Create the virtual environment.
$ pipenv shell

# Install the package.
$ pipenv install beautifulsoup4
1 Like

II did paths to these 3 locations that python said they were installed in.
This one:
c:\users\tom\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages
and these 2 that are sub directories of the one above
bs4
beautifulsoup4-4.9.3.dist-info

Here is the error code when I run the script
File “C:/py/freecodecamp/urllinks.py”, line 6, in
import beautifulsoup4
File “C:\Program Files\JetBrains\PyCharm Edu 2020.3\plugins\python-ce\helpers\pydev_pydev_bundle\pydev_import_hook.py”, line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named ‘beautifulsoup4’

1 Like

It’s imported with literally just bs4, not a expanded full name.

1 Like

it is a script from Dr. Serverence… that was in his script

1 Like

Try to run it on the command line without using Pycharm.

1 Like

I did the bs4 import instead of the beautifulsoup4 like you said… looks improved, but won’t let me input a url. So we are improving… it asks for the url, but goes to the next line with a >?

Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
runfile(‘C:/py/freecodecamp/urllinks.py’, wdir=‘C:/py/freecodecamp’)
Enter -

?

1 Like

I get the same error in both PyCharm, and the command line. I am running an event log, which shows it does get the info from the url, but when it runs that next line to get beautifulsoup it crashes. I tried multiple versions of the name : bs4, beautifulsoup, beautifulsoup4, BeautifulSoup4, Beautifulsoup. the last one is in his script. It now accepts the import bs4, but it is greyed out in the script, which makes me wonder if it is ignoring it. So it gets the file but doesn’t parse it

Traceback (most recent call last):
File “c:\py\freecodecamp\urllinks.py”, line 17, in
soup = BeautifulSoup(html, ‘html.parser’)
NameError: name ‘BeautifulSoup’ is not defined

1 Like

This means that beautifulsoup is not being imported. At the top of your file you should see BeautifulSoup being imported. Can you paste all of the code you wrote?

Where you do the imports you should have something like
from bs4 import BeautifulSoup

>>> from bs4 import BeautifulSoup

>>> soup = BeautifulSoup("<p>Some<b>bad<i>HTML")
1 Like

Here is the original file from Dr. Severence called urllinks.py
changing the import from the bs4 folder to just ‘import BeautifulSoup’
doesn’t work either.
Like I mentioned before I tried spelling it many different ways since my
install search came back
‘beautifulsoup4’, lowercase with the number as my installed version name
#-----------------------------------------------------------------------------------------------------

To run this, download the BeautifulSoup zip file

http://www.py4e.com/code3/bs4.zip

and unzip it in the same directory as this file

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl

Ignore SSL certificate errors

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = input('Enter - ')
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, ‘html.parser’)

Retrieve all of the anchor tags http://www.alfonsi.com

tags = soup(‘a’)
for tag in tags:
print(tag.get(‘href’, None))

1 Like

Why change anything in it at all? Script is written in a way that line:

from bs4 import BeautifulSoup

Should be like that. Changing import line possibly will require more changes in script.

1 Like

The only reason I was trying changes… as I said in my original post, “it’s not working”…

1 Like