Reputation: 3
I want to see the contents inside the beautifulsoup.py just like in parse.py
I installed bs4, but no beautifulsoup.py
file? I'm expecting to see a "beautifulsoup.py" in "bs4" folder, since importing it is: from bs4 import BeautifulSoup
.
Importing parse
is: from urllib import parse
which I see a parse.py here. how about beautiful soup?
Upvotes: 0
Views: 364
Reputation: 365707
bs4.BeautifulSoup
is not a submodule, it's a class. You can find its definition in bs4/__init__.py
.
You can see that it's a class like this:
>>> from bs4 import BeautifulSoup
>>> type(BeautifulSoup)
type
… and you can see what file it came from using functions from the inspect
module, like:
>>> import inspect
>>> inspect.getfile(BeautifulSoup)
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bs4/__init__.py'
The syntax from X import Y
can do two different things:
Y
is a class, function, or other name in X
, it imports that value from module X
into your globals as Y
.Y
is a submodule under package X
, it imports that submodule into your globals as module Y
.For full details, see The import system in the docs:
The
from
form uses a slightly more complex process:
- find the module specified in the from clause, loading and initializing it if necessary;
- for each of the identifiers specified in the import clauses:
- check if the imported module has an attribute by that name
- if not, attempt to import a submodule with that name and then check the imported module again for that attribute
- if the attribute is not found, ImportError is raised.
- otherwise, a reference to that value is stored in the local namespace, using the name in the as clause if it is present, otherwise using the attribute name
Upvotes: 2