25

I am having a problem importing modules in my iPython/Jupyter notebook. The problem fundamentally lies in where the sys.path is pointing to.

From the iPython/Jupyter notebook, sys.executable returns:

'/usr/bin/python'

However, from the command line, it returns:

'//anaconda/bin/python'

I have tried un-installing and re-installing anacondas, but the problem still remains.

I have also tried augmenting $PYTHONPATH in my bash_profile to include //anaconda/bin/python, but this doesn't resolve it.

Is there anyway to change the sys.path in my jupyter notebook permanently, without simply using sys.path.append(...)?

runawaykid
  • 1,391
  • 1
  • 15
  • 20

6 Answers6

14

I had the same issue. After going through many (like way too many) solutions to this issue found elsewhere, I manage to figure out a solution that at least works in my case.

Step1: check the correct executable path of the anaconda environment.

Go on command line, activate the conda environment that is problematic, and check the correct executable path for the environment.

conda activate {envronment name};
then on python console, (>>>)import sys;sys.executable

For instance on Linux it will be /media/{username}/{path-to}/anaconda3/envs/{environment name}/bin/python

Step2: correct the executable path for jupyter sessions.

From command line, check the path where kernel.json of your problematic conda environment is located.

jupyter kernelspec list

For instance on Linux it will be: /home/{username}/.local/share/jupyter/kernels/{environment name}

Open the kernel.json located in that folder and replace the incorrect executable path, as shown below.

{
 "argv": [
  "REPLACE-THIS-WITH-THE-CORRECT-EXECUTABLE-PATH",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "environment name",
 "language": "python"
}
 

Hope this works in your case too.

  • Thanks for a very clear answer. I am running with anaconda-navigator. Any idea how to change the kernel picked up by jupyter when invoked by anaconda-navigator? I found the kernel used through ```import sys print(sys.executable)``` – emeralddove Aug 22 '19 at 09:20
  • Unfortunately I am not familiar with anaconda-navigator (heard it for the first time here). May be it would be good idea to ask this as a separate question. I am pretty sure somebody must have encountered the same issue. –  Aug 22 '19 at 14:18
  • 1
    Thank you again. I had to install kernel within the required environment using ```python3 -m ipykernel install --user```. And installed required libraries within this environment (pillow, in my case). This worked. – emeralddove Aug 25 '19 at 08:43
  • 1
    This is correct answer. Just make sure ipykernel is installed in the virtualenv. – Shivam Sinha Nov 22 '19 at 05:40
  • On windows I get only one value `jupyter kernelspec list`. Should't this `kernel.json` be different for each environment. – quest Apr 29 '21 at 20:33
9
  1. Open a new terminal window and see if this helps. If not, proceed with 2.

  2. Start a standard Python session from the terminal and type this:

    >>> import sys
    >>> sys.executable
    
  3. Do the same in the notebook:

    In [1]: import sys
            sys.executable
    
  4. Compare the results. Hopefully, this gives you a clue what is going on.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
4

My system is Mac. I came across the same problem.

I use my anaconda python installed the packages but my jupyter notebook is not using it and cannot import the modules. I solved the problem by following steps:

Step1: check the actual python path you used to install the packages

I run which python and it shows the default python I use to install packages:

➜  ~ which python
/Users/my_name/opt/anaconda3/bin/python

Step2: change the jupyter kernel's python path to the above path

I run jupyter kernelspec list, it shows 2 kernels my jupyter notebook can use:

Available kernels:
  python3    /Users/my_name/Library/Jupyter/kernels/python3
  python2    /usr/local/share/jupyter/kernels/python2

Since I usually use python3 in my jupyter, then I choose to edit the first kernel's configuration, run: vi /Users/my_name/Library/Jupyter/kernels/python3/kernel.json

replace the first path as "/Users/my_name/opt/anaconda3/bin/python"(as the which python command shows.)

{
 "argv": [
  "/Users/my_name/opt/anaconda3/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "heterodimers",
 "language": "python"
}

save and quit the kernel.json file.

After that, my jupyter notebook can import the packages I installed in the terminal.

And thanks to Mike's answers because I basically followed his solution to find mine. The difference is that I did not use the conda environment.

Seraph
  • 61
  • 4
1

None of the above answers solved my issue. In my windows system I tried this and it worked.

conda create --name {envname}
conda install ipykernel --name {envname}
python -m ipykernel install --prefix=C:/anaconda/envs/{envname} --name {envname}
activate envname
pip install jupyter

Check sys.executable in notebook afterwords to ensure that your prefix is mentioned in the output.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

I just came across this problem. did a

find ~ -name kernal.json

and got

/home/mee/dev/my-venv1/__venv__/share/jupyter/kernels/python3/kernel.json
/home/mee/dev/my-venv2/__venv__/share/jupyter/kernels/python3/kernel.json
/home/mee/.local/share/jupyter/kernels/python3/kernel.json

I looked inside ~/.local/share/jupyter/kernels/python3/kernel.json and found it was pointing to /home/alex/dev/my-venv2/__venv__/bin/python3.

I deleted ~/.local/share/jupyter/kernels/python3/kernel.json and now the one in my-venv2 is pointing to the correct one and all works as expected.

CpILL
  • 6,169
  • 5
  • 38
  • 37
0

win11,miniconda3,virtualenv name “sitk”

just run:

conda activate sitk
conda install ipykernel
python -m ipykernel install --user --name sitk

solves the problem for SimpleITK import error in jupyter lab