如果我输入一个函数定义,请使用ipython 0.11
def f(s): print s
Run Code Online (Sandbox Code Playgroud)
然后我可以在ipython会话中使用该函数,但我不知道如何在ipython_config.py文件中定义它.如果我只是在文件中键入函数定义并尝试使用它未定义的函数.
任何的想法?
这里有两个答案:
首先,对于像上面这样的超简单函数,您可以在其中定义它们exec_lines,例如:
c.InteractiveShellApp.exec_lines = [ "def f(s): print s" ]
Run Code Online (Sandbox Code Playgroud)
(你可以用这种方式定义任意复杂的函数,但它会超出几行而烦人)
对于更复杂的代码,您可以编写要在启动时运行的脚本,并将其添加到exec_files列表中:
c.InteractiveShellApp.exec_files = [ "/path/to/myscript.py" ]
# if you put the script in the profile dir, just the filename will suffice
Run Code Online (Sandbox Code Playgroud)
我们意识到这有点烦人,所以在0.12中,配置文件目录中会有一个启动文件夹,你放在那里的任何东西都会自动运行.这实际上是glob.glob('*.py')为exec_files 添加一个额外的(当然,你可以在0.11中自己做).