How to resolve "ModuleNotFoundError: no module named xgboost"

XGBoost (Extreme Gradient Boosting) is a widely used machine learning library for regression and classification tasks.

Typically, a Python-based compiler will encounter the error "ModuleError: no module named xgboost" when the user tries to import xgboost library.

import xgboost as xgb
print(xgb.__version__)

Causes of error

It is most likely to occur because of the following reasons:

  • The user does not have xgboost installed on their system.

  • They set up the library in a Python virtual environment.

  • There was an error or misspelling when attempting to import the module.

Installing xgb module

We can resolve this error by installing the xgboost library on our Python environment.

pip install xgboost
or
pip3 install xgboost
Installing xgboost library

This command will download and install the library on our system, allowing us to import the particular module.

After installing xgboost, we have to check if the installation was successful. We will do it by importing the module in a Python script and running the code.

Code example

import xgboost as xgb
print(xgb.__version__)
print ("Xgboost has been imported successfully.")

Now, the user can run the above code to verify that the xgboost module has been installed on their system.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved