From 633d54c6b7b64ce71bd83cf6f73ed2077813a938 Mon Sep 17 00:00:00 2001 From: Nikita Titov Date: Thu, 20 May 2021 16:24:08 +0300 Subject: [PATCH] improve error message for required packages --- 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 d30a11a04b77..a68ec7cf396a 100644 --- a/examples/python-guide/plot_example.py +++ b/examples/python-guide/plot_example.py @@ -6,7 +6,7 @@ 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 31b696ef565d..c62f7b822bf2 100644 --- a/python-package/lightgbm/basic.py +++ b/python-package/lightgbm/basic.py @@ -2085,7 +2085,8 @@ def add_features_from(self, other): 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 @@ def trees_to_dataframe(self): 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 be1647b81fb0..0ac9198d7acd 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 2b2261736067..e0444897901d 100644 --- a/python-package/lightgbm/sklearn.py +++ b/python-package/lightgbm/sklearn.py @@ -461,7 +461,8 @@ def __init__(self, boosting_type='gbdt', num_leaves=31, max_depth=-1, 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