Cannot import modules in jupyter notebook; wrong sys.path

前端 未结 3 1292
囚心锁ツ
囚心锁ツ 2021-01-04 02:29

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/Jupyte

3条回答
  •  长情又很酷
    2021-01-04 02:56

    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.

提交回复
热议问题