Fix "No module named xgboost" ImportError in Jupyter Notebook

XGBoost is gaining a lot of traction, and its downloads are increasing. Yet, there's a common issue with the installation, especially in Jupyter Notebook environments where it's typically installed with:

! pip install xgboost
# Or
! pip3 install xgboost
# Or
! conda install -c conda-forge xgboost

Oftentimes, even though this approach works for other modules, this will result in:

ImportError: No module named xgboost

This issue especially seems to be present in Jupyter Notebooks - the most common environment in which you might use XGBoost. This issue may be fixed by using Python's pip module explicitly:

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

! python -m pip install xgboost
# Or
! python3 -m pip install xgboost

But as it may also fail, one of the most common fixes is:

import sys
!{sys.executable} -m pip install xgboost

After which, you can import without issues:

import xgboost
Last Updated: July 1st, 2022
Was this helpful?
David LandupAuthor

Entrepreneur, Software and Machine Learning Engineer, with a deep fascination towards the application of Computation and Deep Learning in Life Sciences (Bioinformatics, Drug Discovery, Genomics), Neuroscience (Computational Neuroscience), robotics and BCIs.

Great passion for accessible education and promotion of reason, science, humanism, and progress.

© 2013-2024 Stack Abuse. All rights reserved.