Installing multiple Python 3 kernels in iPython/Jupyter Notebooks - python

I have Python 3.7 and 3.8 on my system.
I have run pip install on both 3.7 and 3.8 for jupyter notebook, however when trying to create a new notebook, the kernel list only shows one value for "Python 3"
How do I get both Python 3.7 and 3.8 to show up here?

You have to install the kernel on jupyter.
You can do this in command prompt/bash terminal:
jupyter kernelspec list
This will return all the currently installed kernels for jupyter notebooks.
It's best to run a virtual environment for each project you have. Activate your venv, and then you can install the Python kernel for use within a jupyter notebook with this bash command:
ipython kernel install --user --name=projectname
Note this installed kernel will also be available to jupyter when from outside the venv.
You can uninstall kernels with the following:
jupyter kernelspec uninstall <name of kernel>
See here for more details: https://ipython.readthedocs.io/en/stable/install/kernel_install.html
There are also instructions in there for installing multiple python kernels without using a virtual environment.

Related

Why does jupyter notebook on vscode always say not Python is installed?

Python is installed in Linux, as shown in terminal, but why does jupyter notebook on vscode always say not Python is installed?
Thanks a lot.
Have you selected a python environment which has jupyter notebook installed.
Install jupyter notebook in environment:
pip install jupyter notebook
Select environment in VSCode:
https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment

Why do I see multiple interpreters in my Jupyter Notebook?

When I open the Jupyter Notebook web app, I see multiple interpreters:
Ideally, I should just see the Python 3* I have installed via HomeBrew:
/usr/local/bin/python3 --> 3.9.9
and the 2.* one that comes preinstalled on macOS
/usr/bin/python --> 2.7.18
non of which are detected by the Jupyter Notebook!
I would appreciate it if you could help me know what is the problem and how I can clean it up.
To delete Interperters:
Run jupyter kernelspec list to get the paths of all your kernels.
Then simply uninstall your unwanted-kernel
jupyter kernelspec uninstall unwanted-kernel
you can also just run
# List all kernels and grap the name of the kernel you want to remove
jupyter kernelspec list
# Remove it
jupyter kernelspec remove <kernel_name>
The docs has a list of the common paths for kernels to be stored in: common paths
to add your interpreters:
terminate jupyter, activate the environment you want to add a kernel for, and then run this command (requires conda install ipykernel):
python -m ipykernel install --user --name <kernel_name> --display-name "<Name_to_display>"
Make sure to replace <kernel_name> and <Name_to_display> to the name of your environment.
Once you installed the kernel, you can change to it through the above menu and even through this code snippet from a Jupyter cell:
%%javascript
Jupyter.notebook.session.restart({kernel_name: '<kernel_name>'})
you can read more here

How do I get Jupyter Notebook to use the latest version of Python installed on my PC?

I have python 3.10 installed on my PC but every time I open a new Jupyter Notebook and check the version it still says that I am using the previous version that I had. Is there a way to make it start using the latest version every time I open a new notebook?
You should try to work with virtual environments. They are very commonly used in Python. Then, on your Jupyter Notebook, you will be able to choose the particular environment (and Python version) you want to run your notebook.
First, create a virtual environment using Python 3.10:
pip install virtualenv
virtualenv nameofthevenv --python python3.10
source nameofthevenv/bin/activate
Then, inside the virtualenv:
(nameofthevenv) $ pip install jupyter ipykernel
(nameofthevenv) $ python -m ipykernel install --user --name nameofthevenv
(nameofthevenv) $ jupyter notebook
Finally, go to the page in your browser the last command opened, and choose the running kernel at the top right: Kernel >> Change Kernel >> <list of kernels>. You will see nameofthevenv. Select it and you're good to go: the python version will be Python 3.10.

How to check where Jupyter is looking for packages

I'm not able to find a package from my notebook when I have installed it via pip / conda in my terminal.
For example, I did pip install trading-calendars and conda install trading-calendars in terminal but from trading_calendars import get_calendar in Jupyter notebook throws a ModuleNotFoundError with the message No module named 'trading_calendars'.
Is it possible that Jupyter takes time to refresh? If this keeps happening despite restarting Jupyter notebook, what should I do?
Duplicates:
How to list imported modules?
Package for listing version of packages used in a Jupyter notebook
Possible solution:
sys.path different in Jupyter and Python - how to import own modules in Jupyter?
This can be pretty confusing with Jupyter. It's very important to realize that your Jupyter client can connect to different "kernels" which equates to various python environments you might have installed. That is, you can start the Jupyter server with one python environment, and be executing your notebook's cells from another.
You need to make sure that you have the libraries installed to the environment that your kernel is using.
You will need to generate a kernelspec for your environment if you haven't already.
You can create a kernelspec using ipykernel. Here's an example of me doing it with conda.
$ conda activate test
$ conda install ipykernel
$ python -m ipykernel install --user --name test \
--display-name "Python (test)"
You can view your kernelspecs with this command
{~/path/to/project} (master *$)$ jupyter kernelspec list
Available kernels:
django_extensions /Users/nicholasbrady/Library/Jupyter/kernels/django_extensions
python3 /Users/nicholasbrady/anaconda3/share/jupyter/kernels/python3
python2 /usr/local/share/jupyter/kernels/python2

How do I add a python3 kernel to my jupyter notebook?

I installed jupyter notebook along with anaconda python as the only python on my PC (Windows 10). However I recently installed python 3.6.2 and I wonder if I can somehow add it to jupyter so as I can use them changeably.
I remember having both on my other machine when I installed python first and then after that I installed the whole anaconda package with the jupyter notebook (so I had python 3 and python(conda) option for kernels).
So how can I add to jupyter?
To install a Jupyter kernel for a different version of Python, run:
conda create -n py36 'python=3.6' ipykernel # Replace `3.6` with desired version
To enable the newly installed Jupyter kernel in all conda environments, run:
source activate py36
python -m ipykernel install --user
Now, when you start Jupyter from your root environment, you will have the option to choose a different Python kernel when you click on 'Kernel -> Change kernel':
source deactivate
jupyter notebook
For more info, see Installing the IPython kernel.
Simple, just find where the script jupyter-notebook resides, for example ~/.local/bin if you installed it locally.
Then just edit the first line to: #!/usr/bin/python3 will be fine.

Categories

Resources