9
$ virtualenv test
$ source test/bin/activate
$ pip3 install beautifulsoup4

Now the script test.py

import urllib.request
import sys
import unittest, time, re

import requests
from bs4 import BeautifulSoup

class Sel(unittest.TestCase):

    def setUp(self):
        self.base_url = "file:///Users/ishandutta2007/Desktop/cars/cars-price-list.html"

    def test_sel(self):
        with urllib.request.urlopen(self.base_url) as url:
            html_source = url.read()
        data = html_source#.encode('utf-8')
        parsed_html = BeautifulSoup(data, 'html.parser')

if __name__ == "__main__":
    unittest.main()

when I run $python3 test.py

File "test.py", line 6, in from bs4 import BeautifulSoup ModuleNotFoundError: No module named 'bs4'

then tried with

from beautifulsoup4 import BeautifulSoup

File "test.py", line 6, in from beautifulsoup4 import BeautifulSoup ModuleNotFoundError: No module named 'beautifulsoup4'

requirements clearly shows both

$ pip3 freeze > requirements.txt
$ cat requirements.txt 
beautifulsoup4==4.6.0
certifi==2018.1.18
chardet==3.0.4
idna==2.6
requests==2.18.4
urllib3==1.22
ishandutta2007
  • 16,676
  • 16
  • 93
  • 129

6 Answers6

13

Only install BeautifulSoup4 instead of bs4 and BeautifulSoup then,

do this:

from bs4 import BeautifulSoup
Abdullah Ahmed Ghaznavi
  • 1,978
  • 3
  • 17
  • 27
1

I would do this for a work around:

All of this under your created virtual environment

$ wget https://pypi.python.org/pypi/beautifulsoup4
$ pip3 install beautifulsoup4-4.6.0-py3-none-any.whl
$ pip3 freeze |grep beautifulsoup4
$ python3
>>> from bs4 import BeautifulSoup
Joseph D.
  • 11,804
  • 3
  • 34
  • 67
0

It looks like you're not in the right place; cd test and try again.

Here's the problem: Python uses the PYTHONPATH environment variable to look for packages, and by default that includes your current directory. However, the BeautifulSoup4 package is installed under the test directory. If you start Python from the test directory, it should be able to find it.

0

Found the issue, virtualenv was installed with python2.7,

pip3 install virtualenv

fixed the issue.

ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
0

What I did was to delete thebeautifulsoup4-4.8.0.dist-info and bs4 folders from C:\Users\User\Anaconda3\Lib\site-packages and again performed pip install beautifulsoup4 on command prompt. It worked for me.

YesThatIsMyName
  • 1,585
  • 3
  • 23
  • 30
Neh
  • 3
  • 3
-1

import bs4 worked for me. Both using an Anaconda env or Pip

from bs4 import BeautifulSoup