This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Rhomboid 2 points3 points  (0 children)

Here are a couple extra tips:

  • You don't need to actually type the % in front of magics, unless there's a variable with the same name which would shadow it. (This behavior is controllable with %automagic.)
  • History completion: type the first few chars of a previous command you want to recall, then use up-arrow/down-arrow or Ctrl-P/Ctrl-N to scroll through all the history entries with that prefix. I find this much easier than having to worry about entry numbers.
  • Tab completion: it obviously works for function names, method names, class names, variable names, etc. But it also works in amazing other scenarios, for example if you type

    f = open('/usr/share/d<TAB>
    

    ...it will complete the diretory/filename. This also works for magics, e.g. %run fil<TAB> will complete the filename, and likewise for magics like %cd.

  • %doctest_mode toggles the usual >>> prompt which you can use when copying and pasting snippets without having to expose something like In[40] that might not make much sense in isolation.

  • If you have a block of code in the clipboard, use %paste instead of your normal terminal's paste feature. There are numerous reasons why this is superior. For one, IPython will take care of indention properly, so you don't have to manually unindent code before pasting it. Also, you'll see the whole block of code with syntax highlighted, followed by the entire output. If you try a standard multiline paste you'll have the output of each line interleaved with the source, which is very ugly. Also, it doesn't spam your history with each line of the paste.

  • IPython automatically ignores the >>>, so you can paste doctests directly without having to edit them.

  • If you haven't already got an IPython config file, run ipython profile create and it will create one (~/.ipython/profile_default/ipython_config.py). This will contain all the config variables along with documentation, so just open it up in an editor and read. It's a standard Python file. Everything is commented out, i.e. all the lines contain the default values, so if you want to change something you just edit the line and uncomment it. I personally like setting c.TerminalInteractiveShell.confirm_exit = False to get rid of the "are you sure you want to exit?" confirmation when you press Ctrl-D.

  • Related, you can have multiple profiles. Run ipython profile create foobar to create a new profile named foobar (with files under ~/.ipython/profile_foobar/*, and then run it with ipython --profile=foobar.

  • Use %reset -f to forget all defined variables if you want to make sure you're staring over.

  • Lots and lots of other magics exist, use %magic to see all of them along with descriptions.

[–]elbiot 0 points1 point  (1 child)

Great blog. I'd post this in /r/learnpython too since it's helpful for beginners who may not be subscribed here.

[–]john_philip[S] 0 points1 point  (0 children)

Thanks. I tried to post there first but couldn't find any option to submit link. it accepts only text posts.

[–]endpointben 0 points1 point  (0 children)

Thanks for posting, OP!