如何在 ipython 笔记本的开头导入 python 文件

How to import a python file at the start of ipython notebook

我想远程使用我的 iPython notebook 内核并让它在启动时自动导入一个 setup.py 文件。我试过了

python notebook --no-browser

输出消息之一是:

Using existing profile dir: u'/path/to/profile_default'

这样的目录包含一个 startup 文件夹,其中包含我的 setup.py 文件。 不幸的是,文件的内容没有按预期导入。

我正在寻找与标准 shell 的 -c 选项等效的 notebook

以防万一,这还在Python 2.7.

创建一个新的 ipython 配置文件,

$ ipython profile create foo

编辑它生成的ipython_config.py文件,您可以通过以下方式找到它:

$ ipython locate profile foo

之后

c = get_confg()

添加

c.InteractiveShellApp.exec_lines = ['import setup']

然后使用 --profile 参数启动笔记本,

$ ipython notebook --no-browser --profile=foo

如果您不想创建配置文件,则可以使用 --ClassName.attribute=...

通过命令行添加它
$ ipython notebook --no-browser --InteractiveShellApp.exec_lines="['import setup']"

有关 ipython 个人资料的更多信息:http://ipython.org/ipython-doc/stable/config/intro.html