From f076ca58b382fa2fb0a6d8d7367983083aed03a6 Mon Sep 17 00:00:00 2001 From: Nikita Titov Date: Thu, 20 May 2021 22:33:20 +0300 Subject: [PATCH] improve error message for required packages (#4304) --- examples/python-guide/plot_example.py | 2 +- python-package/lightgbm/basic.py | 6 ++++-- python-package/lightgbm/plotting.py | 8 ++++---- python-package/lightgbm/sklearn.py | 3 ++- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/examples/python-guide/plot_example.py b/examples/python-guide/plot_example.py index d30a11a04..a68ec7cf3 100644 --- a/examples/python-guide/plot_example.py +++ b/examples/python-guide/plot_example.py @@ -6,7 +6,7 @@ import lightgbm as lgb if lgb.compat.MATPLOTLIB_INSTALLED: import matplotlib.pyplot as plt else: - raise ImportError('You need to install matplotlib for plot_example.py.') + raise ImportError('You need to install matplotlib and restart your session for plot_example.py.') print('Loading data...') # load or create your dataset diff --git a/python-package/lightgbm/basic.py b/python-package/lightgbm/basic.py index 31b696ef5..c62f7b822 100644 --- a/python-package/lightgbm/basic.py +++ b/python-package/lightgbm/basic.py @@ -2085,7 +2085,8 @@ class Dataset: elif isinstance(self.data, pd_DataFrame): if not PANDAS_INSTALLED: raise LightGBMError("Cannot add features to DataFrame type of raw data " - "without pandas installed") + "without pandas installed. " + "Install pandas and restart your session.") if isinstance(other.data, np.ndarray): self.data = concat((self.data, pd_DataFrame(other.data)), axis=1, ignore_index=True) @@ -2402,7 +2403,8 @@ class Booster: Returns a pandas DataFrame of the parsed model. """ if not PANDAS_INSTALLED: - raise LightGBMError('This method cannot be run without pandas installed') + raise LightGBMError('This method cannot be run without pandas installed. ' + 'You must install pandas and restart your session to use this method.') if self.num_trees() == 0: raise LightGBMError('There are no trees in this Booster and thus nothing to parse') diff --git a/python-package/lightgbm/plotting.py b/python-package/lightgbm/plotting.py index be1647b81..0ac9198d7 100644 --- a/python-package/lightgbm/plotting.py +++ b/python-package/lightgbm/plotting.py @@ -80,7 +80,7 @@ def plot_importance(booster, ax=None, height=0.2, if MATPLOTLIB_INSTALLED: import matplotlib.pyplot as plt else: - raise ImportError('You must install matplotlib to plot importance.') + raise ImportError('You must install matplotlib and restart your session to plot importance.') if isinstance(booster, LGBMModel): booster = booster.booster_ @@ -197,7 +197,7 @@ def plot_split_value_histogram(booster, feature, bins=None, ax=None, width_coef= import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator else: - raise ImportError('You must install matplotlib to plot split value histogram.') + raise ImportError('You must install matplotlib and restart your session to plot split value histogram.') if isinstance(booster, LGBMModel): booster = booster.booster_ @@ -294,7 +294,7 @@ def plot_metric(booster, metric=None, dataset_names=None, if MATPLOTLIB_INSTALLED: import matplotlib.pyplot as plt else: - raise ImportError('You must install matplotlib to plot metric.') + raise ImportError('You must install matplotlib and restart your session to plot metric.') if isinstance(booster, LGBMModel): eval_results = deepcopy(booster.evals_result_) @@ -602,7 +602,7 @@ def plot_tree(booster, ax=None, tree_index=0, figsize=None, dpi=None, import matplotlib.image as image import matplotlib.pyplot as plt else: - raise ImportError('You must install matplotlib to plot tree.') + raise ImportError('You must install matplotlib and restart your session to plot tree.') if ax is None: if figsize is not None: diff --git a/python-package/lightgbm/sklearn.py b/python-package/lightgbm/sklearn.py index 2b2261736..e04448979 100644 --- a/python-package/lightgbm/sklearn.py +++ b/python-package/lightgbm/sklearn.py @@ -461,7 +461,8 @@ class LGBMModel(_LGBMModelBase): and you should group grad and hess in this way as well. """ if not SKLEARN_INSTALLED: - raise LightGBMError('scikit-learn is required for lightgbm.sklearn') + raise LightGBMError('scikit-learn is required for lightgbm.sklearn. ' + 'You must install scikit-learn and restart your session to use this module.') self.boosting_type = boosting_type self.objective = objective