Cannot import modules in jupyter notebook; wrong sys.path

落花浮王杯 提交于 2019-12-05 00:28:34
  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.

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": "heterodimers",
 "language": "python"
}

Hope this works in your case too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!