How can I configure IPython to issue the same "magic" commands at every startup?

Question:

I’d like to be able to use %cd "default_dir" and %matplotlib whenever I call ipython from my terminal. I tried writing this in a .py file in .ipython/profile_default/startup/file.py but it results in the following error:

[TerminalIPythonApp] WARNING | Unknown error in handling startup files:
  File "/Users/<name>/Dropbox/.ipython/profile_default/startup/startup.py", line 18
    %cd "~/Dropbox/"
    ^
SyntaxError: invalid syntax
Asked By: Luke Davis

||

Answers:

You just need to use the magic in your startup scripts:

get_ipython().magic('cd ~/Dropbox')
get_ipython().magic('matplotlib')

Put that in the contents of your startup script and it should do the magic you need ✨ ✨

Answered By: Wayne Werner

I just wanted to elaborate the Wayne’s answer, but do not have enough reputation to do a comment. You can have the following in the start up script to run the required magic commands

from IPython.core import getipython

getipython.get_ipython().run_line_magic("reload_ext", "autoreload")
getipython.get_ipython().run_line_magic("autoreload", "2")

Module reference is here Ipython module

To run the above start up at terminal, do this

ipython -i startup.py
Answered By: vkt
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.