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)
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.
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.
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.
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
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’
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 -
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
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")
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 #-----------------------------------------------------------------------------------------------------