How To Define Functions In Ipython Configuration File?
Solution 1:
Two answers here:
First, for super simple functions like the one above, you can define them in exec_lines
, e.g.:
c.InteractiveShellApp.exec_lines = [ "def f(s): print s" ]
(you can define arbitrarily complex functions this way, but it gets annoying beyond a couple of lines)
For more complicated code, you can write a script that you would like to run at startup, and add that to the exec_files
list:
c.InteractiveShellApp.exec_files = [ "/path/to/myscript.py" ]
# if you put the script in the profile dir, just the filename will suffice
We realized this is slightly annoying, so in 0.12, there will be a startup folder in profile directories, and anything you put in there will be run automatically. This is essentially adding an extra glob.glob('*.py')
to exec_files (which you can do yourself in 0.11, of course).
Solution 2:
You'll need to define a python file to run when ipython starts. You can do this by setting the exec_files
list:
c = get_config()
c.InteractiveShellApp.exec_files = [
'/tmp/mymodule.py'
]
Then my file "/tmp/mymodule.py":
deffoo():
print"bar"
And finally, using this:
$ ipython
In [1]: foo()
bar
More information about the ipython config-file can be found here: http://ipython.org/ipython-doc/dev/config/ipython.html
Solution 3:
Multi-line functions can be defined in *.py
and *.ipy
files under the startup
directory within the profile directory.
(Note that *.ipy
and *.py
files are treated differently.)
The profile directory can be discovered from the command line using
$ ipython locate profile
Post a Comment for "How To Define Functions In Ipython Configuration File?"