Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Jupyter kernelspec to point to anaconda python

I'm using Anaconda to manage both Python and Jupyter. That is:

>> which python
>> /home/.../software/anaconda3/bin/python

and

>> which jupyter
>> /home/.../software/anaconda3/bin/jupyter

But Jupyter's python kernel seems to be pointing to a system version of Python rather than my local version through Anaconda, since the sys.path is different in a Jupyter Python 3 notebook. Also, jupyter kernelspec list gives the following:

Available kernels:
  ir         /usr/local/share/jupyter/kernels/ir
  matlab     /usr/local/share/jupyter/kernels/matlab
  python3    /usr/local/share/jupyter/kernels/python3

This doesn't seem altogether surprising since the docs say in section 1.5.5:

By default, kernel specs will go in a system-wide location (e.g. /usr/local/share/jupyter/kernels). If doing a --user install, the kernel specs will go in the JUPYTER_DATA_DIR location.

For personal sanity and organization, I want the version of Python that I use in the command line to be the same that is accessed in Jupyter. As a result, I think that what I should do is change my jupyter kernelspec list for python3 so that it points to my desired Anaconda python version, i.e. /home/.../software/anaconda3/bin/python. My questions are: 1) is that indeed the best solution for my stated preferences, and 2) how do I actually change my jupyter kernelspec entry for python3? Not sure if this will come up, but I don't want to be using virtual environments--I want the default to be same version of Python across both the command line and Jupyter.

like image 860
itf Avatar asked Oct 03 '17 18:10

itf


People also ask

How do I change the Python environment in Jupyter notebook?

You can also create new environments from within Jupyter Notebook (home screen, Conda tab, and then click the plus sign). And you can create a notebook in any environment you want. Select the "Files" tab on the home screen and click the "New" dropdown menu, and in that menu select a Python environment from the list.

Is Anaconda same as Jupyter notebook?

Anaconda Navigator is a GUI tool that is included in the Anaconda distribution and makes it easy to configure, install, and launch tools such as Jupyter Notebook. A Conda Python environment is an isolated environment. It allows you to install packages without modifying your system's Python installation.

What is the relation between Jupyter notebook and Anaconda?

What is Jupyter Notebook? It is a web-based program under Anaconda distribution and it let you code Python. You can also call it a web application under Anaconda.


2 Answers

I ended up reposting this to the Jupyter Github issues page, and was recommended to delete /usr/local/share/jupyter/kernels/python3. This allows Jupyter to find a default Python kernel using the same Python running Jupyter itself (i.e. Anaconda), and this worked for me.

You can find my post on Jupyter's Github page as well as an explanation for why the above solution works here.

like image 176
itf Avatar answered Oct 23 '22 14:10

itf


1) Jupyter kernels in /usr/local/ are indeed a global install. But I do not see why it couldn't be linked to your anaconda python3 interpreter.

2) To explicitly link your anaconda interpreter to your jupyter install you can run :

pip install ipykernel
python -m ipykernel install --prefix=/usr/local/ --name "anaconda_kernel"

for a global install, or change /usr/local/ if you want a per user install. A doc is specially set for anaconda here

If you combine it with jupyter kernelspec remove python3 beforehand, you can then reset your anaconda kernel as default to be sure.

like image 32
MCMZL Avatar answered Oct 23 '22 13:10

MCMZL