Skip to content

Commit 87fd0b5

Browse files
authored
CLN: read_html global importing check (pandas-dev#51505)
1 parent 055d008 commit 87fd0b5

File tree

2 files changed

+4
-43
lines changed

2 files changed

+4
-43
lines changed

pandas/io/html.py

+3-38
Original file line numberDiff line numberDiff line change
@@ -53,33 +53,6 @@
5353
if TYPE_CHECKING:
5454
from pandas import DataFrame
5555

56-
_IMPORTS = False
57-
_HAS_BS4 = False
58-
_HAS_LXML = False
59-
_HAS_HTML5LIB = False
60-
61-
62-
def _importers() -> None:
63-
# import things we need
64-
# but make this done on a first use basis
65-
66-
global _IMPORTS
67-
if _IMPORTS:
68-
return
69-
70-
global _HAS_BS4, _HAS_LXML, _HAS_HTML5LIB
71-
bs4 = import_optional_dependency("bs4", errors="ignore")
72-
_HAS_BS4 = bs4 is not None
73-
74-
lxml = import_optional_dependency("lxml.etree", errors="ignore")
75-
_HAS_LXML = lxml is not None
76-
77-
html5lib = import_optional_dependency("html5lib", errors="ignore")
78-
_HAS_HTML5LIB = html5lib is not None
79-
80-
_IMPORTS = True
81-
82-
8356
#############
8457
# READ HTML #
8558
#############
@@ -922,16 +895,10 @@ def _parser_dispatch(flavor: str | None) -> type[_HtmlFrameParser]:
922895
)
923896

924897
if flavor in ("bs4", "html5lib"):
925-
if not _HAS_HTML5LIB:
926-
raise ImportError("html5lib not found, please install it")
927-
if not _HAS_BS4:
928-
raise ImportError("BeautifulSoup4 (bs4) not found, please install it")
929-
# Although we call this above, we want to raise here right before use.
930-
bs4 = import_optional_dependency("bs4") # noqa:F841
931-
898+
import_optional_dependency("html5lib")
899+
import_optional_dependency("bs4")
932900
else:
933-
if not _HAS_LXML:
934-
raise ImportError("lxml not found, please install it")
901+
import_optional_dependency("lxml.etree")
935902
return _valid_parsers[flavor]
936903

937904

@@ -1194,8 +1161,6 @@ def read_html(
11941161
See the :ref:`read_html documentation in the IO section of the docs
11951162
<io.read_html>` for some examples of reading in HTML tables.
11961163
"""
1197-
_importers()
1198-
11991164
# Type check here. We don't want to parse only to fail because of an
12001165
# invalid value of an integer skiprows.
12011166
if isinstance(skiprows, numbers.Integral) and skiprows < 0:

pandas/tests/io/test_html.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from functools import partial
2-
from importlib import reload
32
from io import (
43
BytesIO,
54
StringIO,
@@ -26,6 +25,7 @@
2625
Timestamp,
2726
date_range,
2827
read_csv,
28+
read_html,
2929
to_datetime,
3030
)
3131
import pandas._testing as tm
@@ -36,7 +36,6 @@
3636

3737
from pandas.io.common import file_path_to_url
3838
import pandas.io.html
39-
from pandas.io.html import read_html
4039

4140

4241
@pytest.fixture(
@@ -1350,9 +1349,6 @@ def run(self):
13501349
else:
13511350
self.err = None
13521351

1353-
# force import check by reinitalising global vars in html.py
1354-
reload(pandas.io.html)
1355-
13561352
filename = datapath("io", "data", "html", "valid_markup.html")
13571353
helper_thread1 = ErrorThread(target=self.read_html, args=(filename,))
13581354
helper_thread2 = ErrorThread(target=self.read_html, args=(filename,))

0 commit comments

Comments
 (0)