-1

If we go to: https://pypi.org/project/bs4/ we can read:

This is a dummy package managed by the developer of Beautiful Soup to prevent name squatting. The official name of PyPI’s Beautiful Soup Python package is beautifulsoup4. This package ensures that if you type pip install bs4 by mistake you will end up with Beautiful Soup.

So given the following line in python: from bs4 import BeautifulSoup, Comment How can I replace it with beautifulsoup4? ie I don't want to install bs4 package at all as it's just a dummy package as claimed...

sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • I understood it more like a redirect, in fact i use the package `bs4` and its working fine – mnikley Jan 19 '23 at 08:06
  • @mnikley but I don't want my `requirements.txt` file to include both bs4 and beautifulsoup4... – WeatherMon Jan 19 '23 at 08:07
  • simply `pip uninstall bs4` then – mnikley Jan 19 '23 at 08:11
  • I'm curious as to why you would want to omit it. does it improve performance in some way? – Driftr95 Jan 19 '23 at 08:12
  • @mnikley have you seen my import line?... – WeatherMon Jan 19 '23 at 08:20
  • @Driftr95 why to have both inside requirments.text? beautifulsoup4 and bs4 – WeatherMon Jan 19 '23 at 08:20
  • @WeatherMon it seems like you always have to use `from bs4 import BeautifulSoup` for the import, no matter if you installed via `pip install bs4` (which adds an additional package `bs4` with version `0.0.1`, this is related to pypi to be able to install via `beautifulsoup4` AND `bs4`) or via `pip install beautifulsoup4`. However, if you use `pip install beautifulsoup4`, `bs4` will NOT show up in your requirements.txt file, but your import will still work. – mnikley Jan 19 '23 at 08:38
  • I don't get it, given the above import line `from bs4 ...` which should I add to my `requirements.txt` file? `bs4` or `beautifulsoup4 `? @mnikley – WeatherMon Jan 19 '23 at 08:45
  • @WeatherMon you only add `beautifulsoup4`, it will automatically download the right package, and you can keep using your `from bs4 import BeautifulSoup` in your script. – mnikley Jan 19 '23 at 09:58

1 Answers1

2

A distribution package and the import package it contains do not necessarily have the same name. In other words the thing that you install does not necessarily have the same name as the thing you import. Even though the convention is to give them the same name, there are exceptions. And "Beautiful Soup" is such an exception.

So you pip install beautifulsoup4 but you import bs4.

With that said, you can probably safely uninstall bs4:

python -m pip uninstall bs4

as long as you still have beautifulsoup4 installed:

python -m pip install beautifulsoup4
python -m pip show beautifulsoup4
sinoroc
  • 18,409
  • 2
  • 39
  • 70