Jupyter Notebook in VS Code is not working - python

I'm using Ubuntu, installed Vs Code and anaconda. I've also installed python extension in VS Code, which brings Jupyter Notebook extension. Now, whenever I'm trying to run code from the Jupyter extension, its throwing error as if it requires ipykernel.
Now, my conda 'base' environment has already ipykernel. Why cant Jupyter extension look for ipykernel from the current activated environment?
Is there any explicit settings to be made, to make VS Code Jupyter extension use ipykernel from the current active environment? Please provide it.

Click here to switch the environment of Jupyter Notebook.

Possible solution python -m ipykernel install --user --name=conda_env_name

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

How to use my PyCharm Python Interpreter in Jupyter Notebook?

Is it possible to use the Python interpreter that I have been using in PyCharm in Jupyter Notebook? I'm not looking to create a brand new virtual environment as I have tried that and encountered "Solving Environment" issues when trying to install packages. How do I get Jupyter Notebook to use my current interpreter ~/opt/anaconda3/envs/PopulationDensity/bin/python as the interpreter?
I have also tried python -m ipykernel install --user --name <kernel_name> --display-name "<Name_to_display>" as suggested in another thread and substituted <kernel_name> with my interpreter path, but that resulted in a syntax error.
The simplest solution is to install Jupyter notebook in the interpreter that Pycharm happens to use and invoke Jupyter notebook from there.
As Pycharm creates new virtual environments for your projects(by default settings), you need to first activate that interpreter.
The Jupyter notebook invoked like above has access to all packages that Pycharm has access to.
Open up a terminal, active the venv and then:
pip install notebook
jupyter notebook
In every running Python you can check the interpreter path with:
import sys
print(sys.executable)

Jupyter is not using my virtual environment in VSCode

I'm currently having problems getting Jupyter to connect to my kernel that I made using my virtualenv.
Here's what I did:
I first created my virtual environment by doing:
python3 -m venv venv
Then I activated the venv in my terminal, and installed the packages that I needed for my project:
pip install numpy pandas matplotlib ipykernel jupyter jupyterlab
Then I created a kernel from my venv so that Jupyter can use by doing:
python3 -m ipykernel install --user --name=python_data_analytics
Afterwards, I tried to get VSCode to use my venv and I got it to use the interpreter at bin/ipython3 of my venv. As for Jupyter, I got it to use the kernel I made from a dropdown menu.
Then when I tried to execute the first cell of my notebook to import my libraries, Python says that it can't find my libraries.
Here's a screenshot of the end result:
It seems that Jupyter is not using the virtual environment that I made and it's falling back to the system's interpreter. I'm not sure how should I go about fixing this problem. Any help is much appreciated. Thank you.
I'm not sure, but in the left bottom option, where there is a label called "Python 3.9.5..." you can select your desired environment.

Installing a ipykernel and running jupyter notebook inside a virtual env - not using conda

I've hit a dead end trying to solve/debug this issue which doesn't seem like it should be that difficult.
I'm working in Pycharm IDE (not the professional) and I'm working inside a virtual environment let's call it pythonProject and I want to be able to run launch a jupyter notebook in this environment so that it can pick up all the python packages i've installed and configured for this environment.
As I understand it from the documentation, these are the steps I need to take.
My terminal prompt statement:
(pythonProject) oliver#oliver-u20:~/pythonProject$
commands:
python3 -m pip install ipykernel
python3 -m pip install notebook
python3 -m ipykernel install --user --name pythonProject --display-name "Python (pythonProject)"
jupyter notebook
But when I load jupyter notebook, it only shows python3 under kernels.
I tried outputting
jupyter kernelspec list
And get only the base kernel which suggests from this that it's not finding my kernelspec, but I can't seem to figure from the documentation what i'm supposed to do.
Am I missing something?
Ok so I've solved this.
I think there was an install issue with jupyter.
I tried reproducing this in a completely new project and venv and could get the kernel showing.
In the project and venv where I still couldn't, I noticed a discrepancy in the output of my jupyter --paths
In the working venv I could see under data
/home/oliver/.local/share/jupyter
Which is where the kernels I installed are located.
However in the project that wasn't working there was instead:
/home/oliver/snap/jupyter/6/.local/share/jupyter
I'm guessing this snap path is from how I originally installed jupyter on my Ubuntu via the app store - seemed sensible at the time.
So I uninstalled jupyter, restarted my venv and the jupyter --paths has magically changed so that
/home/oliver/.local/share/jupyter
is present and when I start a jupyter notebook at the command line with
jupyter notebook
I can see all my kernels showing!
I found this quite difficult to debug with the documentation and command help outputs so hope someone else finds this useful.

ModuleNotFoundError in Jupyter while pip freeze shows its installed

I created a virtual environment, installed pandas and some other libraries, changed the ipython kernel and then opened jupyter inside my virtual environment. Pandas and other libraries worked fine.
Then i installed fastai in my virtualenv, but it shows ModuleNotFoundError in Jupyter only. It works fine in terminal, when i run !pip freeze inside Jupyter it lists 'fastai', when i try to install it in jupyter with '!pip install fastai' it shows 'Requirement already satisfied' but importing it still gives me 'ModuleNotFoundError'. Check this image for example
All answers on SO to this question are for people who haven't changed their jupyter kernel to their environment or who have had other issues, but i couldn't find my issue.
You have to add the virtualenv to the kernel. Nice discussion is here (Execute Python script within Jupyter notebook using a specific virtualenv).
Assuming virtualenv is working fine (jupyter-notebook and fastai are working), these are the additional steps, I might have tried. In the second line (below) change the "--name=NameOfVirtualEnv" appropriately with the name of your virtualenv.
pip install --user ipykernel
python -m ipykernel install --user --name=NameOfVirtualEnv
After that once you start the Jupyter notebook, you will see the "New" dropdown to the right side .. there you will have your virtual environment with the fastai.
Please let me know the outcome. Curious if it worked for you.

Categories

Resources