How to use my PyCharm Python Interpreter in Jupyter Notebook? - python

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)

Related

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.

Jupyter Notebook in VS Code is not working

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

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.

Jupyter notebook, how to execute system shell commands in the right conda environnment?

I'm currently experiencing some troubles with jupyter notebook and system shell commands. I use nb_conda_kernels to be able to access all of my conda environment from a jupyter notebook launched in base environment, and this works perfectly in most of my use cases. For simplicity sake, let's assume I have 2 environments, the base one, and one named work_env. I launch jupyter notebook in the base environment, and select the work_env kernel upon opening the notebook I'm working on.
Today I came across this line:
! pip install kaggle --upgrade
upon execution of the cell (with the work_env kernel correctly activated), pip installed the kaggle package in my base environment. The intended result was to install this package in my work_env. Any ideas on how to make shell commands execute in the "right" environment from jupyter notebook?
Try specifying the current python interpreter.
import sys
!$sys.executable -m pip install kaggle --upgrade
sys.executable returns the path to the python interpreter you are currently running. $ passes that variable to your terminal (! runs the command on the terminal).
Aliases expand Python variables just like system calls using ! or !! do: all expressions prefixed with ‘$’ get expanded. For details of the semantic rules, see PEP-215
from https://ipython.org/ipython-doc/3/interactive/magics.html
-m is used to run a library module (pip in this case) as a script (check python -h). Running pip as a script guarantees that you are using the pip linked to the current python interpreter rather than the one specified by your system variables.
So, in this way you are sure that pip is installing dependencies on the very same python interpreter you are working on (which is installed in your current environment), this does the trick.

Python modules (Jupyter) using wrong anaconda environment on Windows

My problem is that I am running multiple python environments with different dependencies on a Windows 10 box. The python themselves are working fine, however when I run Jupyter it does not use the correct python environment.
I have several python environments setup there were installed using
conda env create -n python=3.5
I installed Jupyter using pip (because the conda install seemed broken at the moment)
pip install jupyter
I am able to switch between them using activate in the windows cmd
>activate env1
/c/Anaconda3/envs/env1/python
>which python
>activate env2
>which python
/c/Anaconda3/envs/env2/python
When I run each python binary and check the sys.path it pertains to the correct install.
import sys
sys.path
['', 'C:\\Anaconda3\\envs\\env1', 'C:\\Anaconda3\\envs\\env1\\python35.zip', 'C:\\Anaconda3\\envs\\env1\\DLLs', 'C:\\Anaconda3\\envs\\env1\\lib', 'C:\\Anaconda3\\envs\\env1\\lib\\site-packages', 'C:\\Anaconda3\\envs\\env1\\lib\\site-packages\\cycler-0.10.0-py3.5.egg']
I do not have PYTHONPATH set and the only python entries in my path (other than whatever activate changes) are to the root Anaconda.
>echo %PATH%
... C:\Anaconda3\Library\bin; C:\Anaconda3\Scripts; C:\Anaconda3 ...
Things seem to work well if I am just using python.
However whenever I try to run jupyter it seems to be using env2 regardless. In my Jupyter notebook I run the import sys; sys.path and it always seems to point to env2.
I have verified that I am using the correct Jupyter, and I have attempted running Jupyter by calling python.
>activate env1
>which jupyter
/c/Anaconda3/envs/env1/Scripts/jupyter
>activate env2
>which jupyter
/c/Anaconda3/envs/env2/Scripts/jupyter
jupyter notebook # This shows env2 in sys.path
python -m jupyter notebook # This shows env2 in sys.path extended by the below entries
'c:\\anaconda3\\envs\\env2\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\username\\.ipython'
I have also tried manually installing the correct python in Jupyter and it does not change anything.
ipython kernel install --user
I have a bunch of dependencies installed in my env1 that my Jupyter notebooks require and I don't want to mess with my env2.
Does anyone have any idea why Jupyter is not using the correct python environment? Is there some python configuration I am not aware of? Is this some odd Windows specific issue?

Categories

Resources