Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 712
Luke Davis Avatar asked Jan 10 '17 19:01

Luke Davis


People also ask

How do you get all magic commands?

As a first example, you can use the magic command %lsmagic to list the available magic commands (Figure 3.11). To get the output you have to execute the cell as with any other code cell. The %load_ext magic command can be used for loading IPython extension which can add new magic commands.

What is used to run system commands from IPython?

The %run magic command allows you to run any python script and load all of its data directly into the interactive namespace.

What is Run_line_magic?

Third, we use the run_line_magic() method on the ip object to run our line magic. This method takes two arguments: the name of the magic function and the remaining of the arguments for that magic. If you were in an ipython shell, this is the equivalent of running %{magic name} {magic arguments} .


1 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 ✨🔮✨

like image 75
Wayne Werner Avatar answered Sep 20 '22 01:09

Wayne Werner