Arli94
Arli94

Reputation: 720

Cannot import xgboost in Jupyter notebook

I'm on a MAac. I can import xgboost from python2.7 or python3.6 with my Terminal but I can not import it on my Jupyter notebook.

import xgboost as xgb
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-7-745aa3a2d734> in <module>()
----> 1 import xgboost as xgb

ModuleNotFoundError: No module named 'xgboost'

Although I write :

!pip3 install xgboost

It prints that :

Requirement already satisfied: xgboost in     /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xgboost-0.6-py3.6.egg
Requirement already satisfied: numpy in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from xgboost)
Requirement already satisfied: scipy in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from xgboost)

How to resolve this?

Upvotes: 23

Views: 43618

Answers (4)

Beltino Goncalves
Beltino Goncalves

Reputation: 607

Within Jupyter Notebook cell, try running:

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

This allows the package to be installed with right on Jupyter notebook.

Upvotes: 18

Amir Charkhi
Amir Charkhi

Reputation: 846

Since you are using macOS, you can use Homebrew to install xgboost:

In your terminal, run the following command:

brew install xgboost

See this for details.

Upvotes: -2

aydin abedinia
aydin abedinia

Reputation: 311

if you are using anaconda, you can install XGBoost with a command that mentioned below :

conda install -c conda-forge xgboost

Upvotes: 2

AChampion
AChampion

Reputation: 30288

Running a shell escape !pip3 doesn't guarantee that it will install in the kernel you are running. Try:

import sys
print(sys.base_prefix)

and see if this matches either of your terminal pythons. You should be able to run <base_prefix>/bin/pip install <package> to ensure it is in the right site-packages.

You can also look at which python your kernel is running by looking in kernel.json most likely in ~/Library/Jupyter/kernels/<kernel>/kernel.json.

Note: you can also programmatically install packages with:

import pip
pip.main(['install', '<package>'])

which will force it to be in the right site-packages for your kernel.

Upvotes: 15

Related Questions