Skip to content
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

[python] improve error message for required packages #4304

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion examples/python-guide/plot_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down
8 changes: 4 additions & 4 deletions python-package/lightgbm/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down Expand Up @@ -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_
Expand Down Expand Up @@ -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_)
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion python-package/lightgbm/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down