配置 Jupyter 默认导入
Configuring Jupyter default imports
如何让 Jupyter(控制台和笔记本)默认导入一些 Python 包?我想只使用 .jupyter
文件夹
一些背景知识:
- Jupyter 提供了 UI/environments 控制台和笔记本等。它遵循它所谓的 kernels 来执行。
- IPython 为 Jupyter 提供默认 (Python) 内核。
- Jupyter 配置不会直接影响内核,但每个内核可能有自己的配置。
IPython 配置位于您的 .ipython
目录中。在 IPython 启动时向 运行 添加代码的最快方法(影响终端和笔记本中的 IPython 会话)是将 启动文件 添加到您的 IPython 个人资料。
创建默认配置文件,如果它不存在(可能存在):
ipython profile create
创建一个 Python 脚本 ~/.ipython/profile_default/startup/whateveryouwant.py
并在其中添加您希望在启动时准备就绪的任何导入或其他命令 IPython。 IPython 每次启动时都会 运行 此脚本和该目录中的任何其他脚本。
我个人找不到只使用 .jupyter
文件夹的方法。无论如何,您必须在 .ipython
文件夹中指定启动时要执行的命令:
- 如果
~/.ipython/profile_default/ipython_config.py
不存在则创建
添加如下内容:
c = get_config()
c.InteractiveShellApp.exec_lines = [
'import numpy as np\n'
'import scipy as sp\n'
'import matplotlib as plt\n'
]
您还可以在此处指定任何有效的命令,而不仅仅是导入。
如何让 Jupyter(控制台和笔记本)默认导入一些 Python 包?我想只使用 .jupyter
文件夹
一些背景知识:
- Jupyter 提供了 UI/environments 控制台和笔记本等。它遵循它所谓的 kernels 来执行。
- IPython 为 Jupyter 提供默认 (Python) 内核。
- Jupyter 配置不会直接影响内核,但每个内核可能有自己的配置。
IPython 配置位于您的 .ipython
目录中。在 IPython 启动时向 运行 添加代码的最快方法(影响终端和笔记本中的 IPython 会话)是将 启动文件 添加到您的 IPython 个人资料。
创建默认配置文件,如果它不存在(可能存在):
ipython profile create
创建一个 Python 脚本
~/.ipython/profile_default/startup/whateveryouwant.py
并在其中添加您希望在启动时准备就绪的任何导入或其他命令 IPython。 IPython 每次启动时都会 运行 此脚本和该目录中的任何其他脚本。
我个人找不到只使用 .jupyter
文件夹的方法。无论如何,您必须在 .ipython
文件夹中指定启动时要执行的命令:
- 如果
~/.ipython/profile_default/ipython_config.py
不存在则创建 添加如下内容:
c = get_config() c.InteractiveShellApp.exec_lines = [ 'import numpy as np\n' 'import scipy as sp\n' 'import matplotlib as plt\n' ]
您还可以在此处指定任何有效的命令,而不仅仅是导入。