Easy fix to Python ModuleNotFoundError: No module named xgboost

The error ModuleNotFoundError: No module named xgboost occurs when Python can’t find the xgboost module from your current environment.

This error usually happens because the xgboost module is not installed, or it was installed in a different environment.

This article shows simple steps you can take to fix this error.

Reproducing the error.

The error usually appears when you import the xgboost module into your code like this:

import xgboost

When Python can’t find the module, it will show the following error message:

Traceback (most recent call last):
  File ...
    import xgboost
ModuleNotFoundError: No module named 'xgboost'

To fix this error, try to install the module using pip.

Installing the xgboost module

To install the module, you need to run one of the following commands in your terminal:

# Use pip to install xgboost
pip install xgboost

# Or pip3
pip3 install xgboost

# If pip isn't available in PATH
python -m pip install xgboost

# Or with python3
python3 -m pip install xgboost

# For Windows without pip in PATH
py -m pip install xgboost

# for Anaconda
conda install -c base xgboost

# for Jupyter Notebook
!pip install xgboost

Once the installation is complete, you can try to run your code again and see if the error is already fixed.

If you still see the error, that means the Python interpreter you used to run the script can’t find the module you installed.

One of the following scenarios may happen in your case:

  1. You have multiple versions of Python installed on your system, and you are using a different version of Python than the one where xgboost is installed.
  2. You might have xgboost installed in a virtual environment, and you are not activating the virtual environment before running your code.
  3. Your IDE uses a different version of Python from the one that has xgboost installed

Let’s see how to fix these errors.

Case#1 - You have multiple versions of Python

If you have multiple versions of Python installed on your system, you need to make sure that you are using the specific version where the xgboost package is installed.

You can test this by running the which -a python or which -a python3 command from the terminal:

$ which -a python

In the example above, there are two versions of Python installed on /opt/homebrew/bin/python and /usr/bin/python.

Suppose you run the following steps in your project:

  1. Install xgboost with pip using /usr/bin/ Python version
  2. Install Python using Homebrew, now you have Python in /opt/homebrew/
  3. Then add import xgboost in your code

The steps above will cause the error because xgboost is installed in /usr/bin/, and your code is probably executed using Python from /opt/homebrew/ path.

To solve this error, you need to run pip install xgboost command again so that the package is installed and accessible by the new Python version.

Finally, keep in mind that you can also have pip and pip3 available on your computer.

These days, the pip command is used to install modules for Python 2, while pip3 installs for Python 3. Make sure that you are using the right command for your situation.

Next, you can also have the package installed in a virtual environment.

Case#2 - You are using Python virtual environment

Python venv package allows you to create a virtual environment where you can install different versions of packages required by your project.

If you are installing xgboost inside a virtual environment, then the module won’t be accessible outside of that environment.

Even when you never run the venv package, Python IDE like Anaconda and PyCharm usually create their own virtual environment when you create a Python project with them.

You can see if a virtual environment is activated or not by looking at your command prompt.

When a virtual environment is activated, the name of that environment will be shown inside parentheses as shown below:

In the picture above, the name of the virtual environment (base) appears when the Conda virtual environment is activated.

To solve this, you can either:

  1. Turn off the virtual environment so that pip installs to your computer
  2. Install the xgboost in the virtual environment with pip

You can choose the solution that works for your project.

When your virtual environment is created by Conda, run the conda deactivate command. Otherwise, running the deactivate command should work.

To activate your virtual environment, use one of the following commands:

# For Conda:
conda activate <env_name>

# For venv:
source <env_name>/bin/activate

For Pycharm, you need to follow the Pycharm guide to virtual environment.

Case#3 - IDE using a different Python version

Finally, the IDE from where you run your Python code may use a different Python version when you have multiple versions installed.

If you use VSCode, run the steps below to check available Python interpreters:

  1. Open the command palette (CTRL + Shift + P for Windows and ⌘ + Shift + P for Mac)
  2. Run the Python: Select Interpreter command.

You should see all available Python versions listed as follows:

You need to use the same version where you installed xgboost so that the module can be found when you run the code from VSCode.

If you use Pycharm, follow the Pycharm configuring Python interpreter guide.

Once done, the import xgboost statement shouldn’t cause any error.


To conclude, the ModuleNotFoundError: No module named 'xgboost' error occurs when the xgboost package is not available in your Python environment.

To fix this error, you need to install xgboost using pip.

If you already have the module installed, make sure you are using the correct version of Python, activate the virtual environment if you have one, and check for the Python version used by your IDE.

By following these steps, you should be able to import xgboost modules. Good work! 👍

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.