Jupyter 和 Python 中的 sys.path 不同 - 如何在 Jupyter 中导入自己的模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34976803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:48:35  来源:igfitidea点击:

sys.path different in Jupyter and Python - how to import own modules in Jupyter?

pythonjupyterpythonpath

提问by ulf

In Jupyter my own little module is not loaded but in python/bpython is everything is fine. When typing

在 Jupyter 中,我自己的小模块没有加载,但在 python/bpython 中一切正常。打字时

import sys
print(sys.path)

the path to my module will not in show in Jupyter but in python/bpython it is still there.

我的模块的路径不会在 Jupyter 中显示,但在 python/bpython 中它仍然存在。

I am using:

我在用:

  1. PYTHONPATH in .bashrc to include my module,
  2. Jupyter and bpython inside a virtualenv.
  1. .bashrc 中的 PYTHONPATH 以包含我的模块,
  2. 虚拟环境中的 Jupyter 和 bpython。

The most similar questions is this Cannot import modules in jupyter notebook; wrong sys.path

最相似的问题是这个 Cannot import modules in jupyter notebook;错误的系统路径

How to configure Jupyter to load my modules automagically?

如何配置 Jupyter 以自动加载我的模块?

回答by Dogan Askan

Here is what I do on my projects in jupyter notebook,

这是我在 jupyter notebook 中对我的项目所做的工作,

import sys
sys.path.append("../") # go to parent dir
from customFunctions import *

Then, to affect changes in customFunctions.py,

然后,为了影响 的变化customFunctions.py

%load_ext autoreload
%autoreload 2

回答by N. P.

Jupyter has its own PATH variable, JUPYTER_PATH.

Jupyter 有自己的 PATH 变量 JUPYTER_PATH。

Adding this line to the .bashrcfile worked for me:

将此行添加到.bashrc文件中对我有用:

export JUPYTER_PATH=<directory_for_your_module>:$JUPYTER_PATH

回答by Mattia Fantoni

Jupyter is base on ipython, a permanent solution could be changing the ipython config options.

Jupyter 基于 ipython,永久解决方案可能是更改 ipython 配置选项。

Create a config file

创建配置文件

$ ipython profile create
$ ipython locate
/Users/username/.ipython

Edit the config file

编辑配置文件

$ cd /Users/username/.ipython
$ vi profile_default/ipython_config.py

The following lines allow you to add your module path to sys.path

以下几行允许您将模块路径添加到 sys.path

c.InteractiveShellApp.exec_lines = [
    'import sys; sys.path.append("/path/to/your/module")'
]

At the jupyter startup the previous line will be executed

在 jupyter 启动时,将执行上一行

Here you can find more details about ipython config https://www.lucypark.kr/blog/2013/02/10/when-python-imports-and-ipython-does-not/

在这里您可以找到有关 ipython 配置的更多详细信息https://www.lucypark.kr/blog/2013/02/10/when-python-imports-and-ipython-does-not/

回答by Kirill

Suppose your project has the following structure and you want to do imports in the notebook.ipynb:

假设您的项目具有以下结构,并且您想在 中进行导入notebook.ipynb

/app
  /mypackage
    mymodule.py
  /notebooks
    notebook.ipynb

If you are running Jupyter inside a docker container without any virtualenv it might be useful to create Jupyter (ipython) config in your project folder:

如果您在没有任何 virtualenv 的 docker 容器中运行 Jupyter,那么在您的项目文件夹中创建 Jupyter (ipython) 配置可能会很有用:

/app
  /profile_default
    ipython_config.py

Content of ipython_config.py:

内容ipython_config.py

c.InteractiveShellApp.exec_lines = [
    'import sys; sys.path.append("/app")'
]

Open the notebook and check it out:

打开笔记本查看:

print(sys.path)

['', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages/IPython/extensions', '/root/.ipython', '/app']

['', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr /local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages/IPython/extensions', '/root/.ipython', '/app']

Now you can do imports in your notebook without any sys.pathappending in the cells:

现在,您可以在笔记本中进行导入,而无需sys.path在单元格中附加任何内容:

from mypackage.mymodule import myfunc

回答by the775

You can use absolute imports:

您可以使用绝对导入:

/root
    /app
      /config
        config.py
      /source
        file.ipynb

# In the file.ipynb importing the config.py file
from root.app.config import config