daemon
daemon

Reputation: 3

No beautifulsoup.py file in bs4 folder?

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

Answers (1)

abarnert
abarnert

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:

  • If Y is a class, function, or other name in X, it imports that value from module X into your globals as Y.
  • If 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:

  1. find the module specified in the from clause, loading and initializing it if necessary;
  2. for each of the identifiers specified in the import clauses:
    1. check if the imported module has an attribute by that name
    2. if not, attempt to import a submodule with that name and then check the imported module again for that attribute
    3. if the attribute is not found, ImportError is raised.
    4. 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

Related Questions