Srikanta Patra
Srikanta Patra

Reputation: 41

Import BeautifulSoup

To my understanding "import package.module" is same as "from package import module". But this is not behaving as expected in case of BeautifulSoup.

from bs4 import BeautifulSoup: This command works fine.

But,

import bs4.BeautifulSoup throws the following error

ModuleNotFoundError: No module named 'bs4.BeautifulSoup'

Any thoughts/help on this?

Upvotes: 4

Views: 8860

Answers (2)

Rakesh Shinde
Rakesh Shinde

Reputation: 58

import bs4.BeautifulSoup will work when we have another file like thing in your bs4 package however BeautifulSoup is a class from that package so it cannot be called the way you are calling it.

Example I have a package name Twitter and there is a sub package called twitter-api then I can call it as import Twitter.twitter.api Similarly, if I have package name Twitter and there is a class inside that package with name twitter-api then I have to call it from Twitter import twitter-api

Upvotes: 0

Daniel
Daniel

Reputation: 607

As colorspeed pointed out above, BeautifulSoup is a class therefore import bs4.BeautifulSoup will result in error. Instead use the syntax from bs4 import BeautifulSoup.

Upvotes: 4

Related Questions