If the title of the post sounds cryptic, then you’re not a pythonista, in which case you can happily ignore the next 3 minutes of geek talk! For anyone else met with an error when executing “import BeautifulSoup from BeautifulSoup” (especially running Anaconda’s version of Python), here’s a handy tip.
Running 32-bit Ubuntu 12.04, I asked for “python” in terminal and was met with
Python 2.7.6 |Anaconda 1.8.0 (32-bit)| (default, Nov 11 2013, 10:47:19)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
ImportError: No module named BeautifulSoup
Okay, maybe BeautifulSoup isn’t a standard package? Back to terminal, I searched for Anaconda’s linked packages:
cecilialiao@ubuntu:~$ conda list
# packages in environment at /home/x/anaconda:
# Warning: subprocess call to pip failed
_license 1.1 py27_0
anaconda 1.8.0 np17py27_0
apptools 4.2.0 py27_0
astropy 0.2.5 np17py27_0
atom 0.3.4 py27_0
beautiful-soup 4.3.1 py27_0
binstar 0.3.1 py27_1
BeautifulSoup is blatantly there, so why the import error?
Turns out that the latest version of BeautifulSoup (as of writing) is no longer called BeautifulSoup, but instead “bs4″ (all lower cap). So, to use the library, you run the following…
from bs4 import BeautifulSoup
… and you’re all set to go.
All this is pretty obvious if I knew to go to BeautifulSoup’s documentation in the first place, or even Wikipedia’s entry on BeautifulSoup, for the simple command. As it happened, though, trying to learn linux and python at the same time can lead one to over an hour of fruitless Google searches. I hope this helps beginners out there scratching their heads when trying to follow an older webscraping tutorial on using BeautifulSoup!
(Image courtesy of Xavi Villalvilla.)