4

I am trying to create a script to download the captcha from my website. I think the code works except from that error, when I run it in cmd (I am using windows not Linux) I receive the following:

from bs4 import BeautifulSoup
ImportError: No module named bs4

I tried using pip install BeautifulSoup4 but then I receive a syntax error at install.

Here is the script:

from bs4 import BeautifulSoup
import urllib2
import urllib

url = "https://example.com"
content = urllib2.urlopen(url)
soup = BeautifulSoup(content)
img = soup.find('img',id ='imgCaptcha')
print img
urllib.urlretrieve(urlparse.urljoin(url, img['src']), 'captcha.bmp')

The problem according to this answer must be due to the fact I have not activated the virtualenv, and THEN install BeautifulSoup4.

I don't think this information will be of any help but I saved my python text in a notepad.py and the run it using cmd.

LightCC
  • 9,804
  • 5
  • 52
  • 92
We're All Mad Here
  • 1,544
  • 3
  • 18
  • 46
  • Where are you running the command `pip install BeautifulSoup4`? The fact that you are getting a syntax error suggests to me that you might be running the command in the Python shell, which is incorrect - you need to run it in the windows command line. – gtlambert Oct 25 '15 at 16:52
  • I ran the above code in windows command line, which did not work and showed the above error and the afterwards I added `pip install BeautifulSoup4` on top of `from bs4 import BeautifulSoup` and then a syntax error appeared. – We're All Mad Here Oct 25 '15 at 16:59
  • I agree but I do not know how to solve the installation errors – We're All Mad Here Oct 25 '15 at 17:06
  • The command `pip install BeautifulSoup4` must not be part of your Python code. Open the Windows command line and type `pip install BeautifulSoup4`. Then try running your code. – gtlambert Oct 25 '15 at 17:06
  • I just tried that and it says `pip is not regognized`.. I guess I must install something from here: [pip python](https://pypi.python.org/pypi/pip) – We're All Mad Here Oct 25 '15 at 17:12
  • Correct - install `pip`, then follow my instructions. – gtlambert Oct 25 '15 at 17:13

4 Answers4

6

I had the same problem until a moment ago. Thanks for the post and comments! According to @Martin Vseticka 's suggestion I checked if I have the pip.exe file in my python folders. I run python 2.7 and 3.7 simultaneously. I didn't have it in the python 2.7 folder but in the 3.7. So in the command line I changed the directory to where the pip.exe file was located. Then I ran "pip install BeautifulSoup4" and it worked. See enclosed screen shot. enter image description here

Simone
  • 497
  • 5
  • 19
  • in Windows, you can use the [Python Launcher for Windows](https://docs.python.org/3/using/windows.html?highlight=python%20launcher#python-launcher-for-windows) to ensure you are running python from the correct install (i.e. 2.7, 3.6, 3.7, etc.). You can either do this with each run (`py -3.7 -m pip`) or setup a default version to run (i.e. `PYTHON_PY 3.7` environment variable, then just use `py -m pip`). – LightCC Jul 11 '20 at 02:20
  • Thanks for sharing @LightCC - the post is 6 years old. I am sure there have been plenty of up-dates in the mean time. In general I found the Anaconda environment quite handy. – Simone Jun 08 '21 at 09:07
1

I did a fresh install of Python 3.5 on my Windows 8.1 (64b) machine and then run:

C:\Users\Me\AppData\Local\Programs\Python\Python35-32\Scripts>pip install b
eautifulsoup4
Collecting beautifulsoup4
  Downloading beautifulsoup4-4.4.1-py3-none-any.whl (81kB)
    100% |################################| 81kB 890kB/s
Installing collected packages: beautifulsoup4
Successfully installed beautifulsoup4-4.4.1

and then run:

C:\Users\Me\AppData\Local\Programs\Python\Python35-32>python test.py

test.py contains only:

from bs4 import BeautifulSoup

I received no error.

MartyIX
  • 27,828
  • 29
  • 136
  • 207
0

I have python3.7 32bit and python3.7 64 bit installed on Windows 10. For this app, I am using the 32 bit version. I was getting the following error:

ImportError: No module named bs4

when I was trying to 'import bs4' from my app.py. Someone said: "Make sure your pip and your python are both 32 bits", but omitted to explicitly say how, so here it is:

  1. Delete your virtual environment if it exists.

  2. Create a new venv using python -m venv venv, but explicitly call it from your 32bit python directory like this: C:\Users\me\AppData\Local\Programs\Python\Python37-32\python -m venv my_venv

  3. install the requirements by explicitly calling the 32bit version like this:

C:\Users\me\AppData\Local\Programs\Python\Python37-32\Scripts\pip install -r requirements.txt

That's it. I hope it works.

LightCC
  • 9,804
  • 5
  • 52
  • 92
mrivas
  • 1
  • 1
  • 2
0

I could install bs4, but still got an error message like yours. My operation system is 64 bit, and I only had this issue after I installed the latest Python in "32 bit" version. So I removed the 32 bit one, then I can import the bs4 again, and it still works after I install the latest Python in 64 version. Hope it helps.

Catsitter
  • 3
  • 3