Skip to content

FIX Avoid side effects importing pip/setuptool in show_version #22621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions sklearn/utils/_show_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import platform
import sys
import importlib
from importlib.metadata import version, PackageNotFoundError
from ..utils.fixes import threadpool_info
from .. import __version__


from ._openmp_helpers import _openmp_parallelism_enabled
Expand Down Expand Up @@ -37,6 +38,9 @@ def _get_sys_info():
def _get_deps_info():
"""Overview of the installed version of main dependencies

This function does not import the modules to collect the version numbers
but instead relies on standard Python package metadata.

Returns
-------
deps_info: dict
Expand All @@ -46,7 +50,6 @@ def _get_deps_info():
deps = [
"pip",
"setuptools",
"sklearn",
"numpy",
"scipy",
"Cython",
Expand All @@ -56,20 +59,14 @@ def _get_deps_info():
"threadpoolctl",
]

def get_version(module):
return module.__version__

deps_info = {}
deps_info = {
"sklearn": __version__,
}

for modname in deps:
try:
if modname in sys.modules:
mod = sys.modules[modname]
else:
mod = importlib.import_module(modname)
ver = get_version(mod)
deps_info[modname] = ver
except ImportError:
deps_info[modname] = version(modname)
except PackageNotFoundError:
deps_info[modname] = None

return deps_info
Expand Down