无法在 jupyter notebook 中导入模块;错误的系统路径 [英] Cannot import modules in jupyter notebook; wrong sys.path

查看:49
本文介绍了无法在 jupyter notebook 中导入模块;错误的系统路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 iPython/Jupyter 笔记本中导入模块时遇到问题.问题从根本上在于 sys.path 指向的位置.

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

从 iPython/Jupyter notebook 中,sys.executable 返回:

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

'/usr/bin/python'

但是,从命令行返回:

'//anaconda/bin/python'

我已经尝试卸载并重新安装 anacondas,但问题仍然存在.

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

我也尝试在我的 bash_profile 中增加 $PYTHONPATH 以包含//anaconda/bin/python,但这并没有解决它.

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

无论如何要永久更改我的 jupyter 笔记本中的 sys.path,而无需简单地使用 sys.path.append(...)?

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

推荐答案

我遇到了同样的问题.在经历了在其他地方找到的针对此问题的许多(例如太多)解决方案后,我设法找到了一个至少适用于我的情况的解决方案.

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.

进入命令行,激活有问题的conda环境,并检查该环境的正确可执行路径.

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

conda activate {环境名称};
然后在python控制台上,(>>>)import sys;sys.executable

例如在 Linux 上它将是/media/{username}/{path-to}/anaconda3/envs/{environment name}/bin/python

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

从命令行,检查有问题的 conda 环境的 kernel.json 所在的路径.

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

jupyter kernelspec list

例如在 Linux 上,它将是:/home/{username}/.local/share/jupyter/kernels/{environment name}

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

打开位于该文件夹中的kernel.json并替换不正确的可执行路径,如下所示.

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.

这篇关于无法在 jupyter notebook 中导入模块;错误的系统路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆