Python Forum
Import error even though module is installed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Import error even though module is installed
#1
Cant post my code because I'm new and it has links?

from bs4 import BeautifulSoup
from urllib.request import urlopen

url = "http://www.pythonforbeginners.com"

content = urlopen(url).read()

soup = BeautifulSoup(content)

print( soup.prettify())

#print(title) 
#>> 'title'? Python For Beginners

#print soup.title.string
#>> ? Python For Beginners

print(soup.p) 
Reply
#2
remove the links.
You'll be able to use them after you make a few posts
Reply
#3
Error:
Traceback (most recent call last): File "/home/ubuntu/workspace/ex50/bin/firstsoup.py", line 1, in <module> from bs4 import BeautifulSoup ImportError: No module named 'bs4' Process exited with code: 1

Sorry for the multiple reply chunks- bs4 is definitely installed, so I don't know what I'm doing wrong. Apologize in advance for my newbie/idiot status!
Reply
#4
You may have it installed, but apparently not for the version of Python you are running
please read: https://python-forum.io/Thread-Basic-Par...er-Windows

type (from command line):
python --version
Reply
#5
(Jun-26-2017, 06:40 PM)datafix Wrote: Sorry for the multiple reply chunks- bs4 is definitely installed,
It's not installed when you get that message.
pip3 install beautifulsoup4
You execute with python3 firstsoup.py
Look at my Linux Python 3 environment,
and your script is wrong in newer BeautifulSoup has to call a parser and don't use urllib look at Web-Scraping part-1
Reply
#6
I thought I was running v3 but in fact i was running v2. So I fixed that, reran it with v3 and got the same import error. Running on Cloud9- that shouldn't make a difference- do you think?
Reply
#7
run this: https://python-forum.io/Thread-Show-Inst...age-detail
Reply
#8
(Jun-27-2017, 06:34 PM)datafix Wrote: Running on Cloud9- that shouldn't make a difference- do you think?
I use Cloud9 sometime myself.
Cloud9 has Python 2 and 3 installed,you have to pip3 install module,
and run code with python3 myscript.py
Here a run at Cloud9.
# install modules
snippsat:~/workspace/bs4_test $ sudo pip3 install beautifulsoup4 requests lxml                                                                                             
Downloading/unpacking beautifulsoup4
 Downloading beautifulsoup4-4.6.0-py3-none-any.whl (86kB): 86kB downloaded
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/lib/python3/dist-packages
Downloading/unpacking lxml
 Downloading lxml-3.8.0.tar.gz (3.8MB): 3.8MB downloaded
 Running setup.py (path:/tmp/pip_build_root/lxml/setup.py) egg_info for package lxml
   Building lxml version 3.8.0.
   Building without Cython.
   Using build configuration of libxslt 1.1.28    
Successfully installed beautifulsoup4 lxml
Cleaning up...

# After install can run a interactive test
snippsat:~/workspace/bs4_test $ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> from bs4 import BeautifulSoup
>>> 
>>> url = 'https://www.python.org/'
>>> url_get = requests.get(url)
>>> soup = BeautifulSoup(url_get.content, 'lxml')
>>> print(soup.select('head > title')[0].text)
Welcome to Python.org
>>> 
Reply
#9
Thanks so much for all of the feedback. I was off on another project, but I'm back. I'll try out these suggestions and let you know how it worked.

@snippsat - Thanks! Your last post did the trick. Smile Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask_table module compatibility issue: cannot import name 'Markup' from 'flask' venkateshbalagiri 1 180 Mar-22-2024, 05:07 AM
Last Post: venkateshbalagiri
Bug Selenium installed but not installed? NoNameoN 9 4,656 Feb-19-2021, 11:31 AM
Last Post: NoNameoN
  Getting import error when trying to import Selenium Several 1 2,261 Oct-06-2020, 10:22 PM
Last Post: Larz60+
  Python Import Sybase Module returning non explicit error thebeefes 2 5,851 Mar-14-2019, 07:32 PM
Last Post: Carlos
  Error in Selenium: CRITICAL:root:Selenium module is not installed...Exiting program. AcszE 1 3,587 Nov-03-2017, 08:41 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020