Since everyone is doing the Advent of Code, I want to do something else - the #IPython Advent Calendar.
IPython is great to interactively solve problems like this, and I want to show you some tips to make the most of it.
So 25 tips for 25 days 👇
1. You can display useful information for any function, module, or variable by adding "?" at the end (or at the beginning). No need to google for the documentation. Import "re" modul...
2. If you want to see the whole source code of a function (or a class/module), add two question marks instead of one:
your_function?? Type "import pandas&qu...This will display the whole...
3. If you want to write a longer function, use "%edit" command.
IPython will open a temporary file in your favorite editor (you need to set it with the EDITOR environment variable), and when you save and close it, it will execute that code.
4. Speaking of the %edit command, you can run "%edit -p" to reopen the same file that you edited last time. This is great to incrementally build some bigger code block or to quickly fix a mistake you made last time.
5. Forgot how some function is called? You can use * for wildcard search in a module or class. For example, os.*dir*? returns all functions from the os module that contain "dir" in their names. Image
6. Post-mortem debugging is 🚀!
When an exception happens, run %debug, and it will start a debugger for that past exception.
Great way to debug unexpected exceptions (unexpected == you didn't think it's going to fail, so you didn't enable the debugger before running your code). Image
7. Run %pdb to automatically start a debugger on every exception (run %pdb again to disable it). Image
8. Run shell commands directly from IPython by prefixing them with "!".
Wanna run vim from IPython? Why not ("!vim")?!
Node REPL inside IPython REPL? Sure ("!node")!
Drop your file system ("!rm -rf /") - well, maybe not 😅 Image
6. Post-mortem debugging is 🚀!
When an exception happens, run %debug, and it will start a debugger for that past exception.
Great way to debug unexpected exceptions (unexpected == you didn't think it's going to fail, so you didn't enable the debugger before running your code). Image
7. Run %pdb to automatically start a debugger on every exception (run %pdb again to disable it). Image
8. Run shell commands directly from IPython by prefixing them with "!".
Wanna run vim from IPython? Why not ("!vim")?!
Node REPL inside IPython REPL? Sure ("!node")!
Drop your file system ("!rm -rf /") - well, maybe not 😅 Image
9. You can move around the filesystem with %cd without leaving IPython. If you started IPython in a "wrong" folder, you can move with "%cd some/other/folder". You also get autocompletion for folder names when pressing TAB. Image
10. Use "%autoreload" to automatically reload any imported function/module before running them.
It's great when you want to edit code in one window and run it in IPython without reimporting it every time (more info here: switowski.com/blog/ipython-a…).
11. Use %xmode to change how verbose the errors are. Each time you run it, it changes between Minimal (almost no information)->Plain->Context (default value)->Verbose (LOTS of information)->Minimal.

The verbose mode prints values of the local variables in the stack trace. Image
Use "%rerun ~1/" to automatically rerun all the commands from the previous IPython session.

Unfortunately, it won't work if you had exceptions (rerun stops after every exception) or if the last session also contains "%rerun ~1/" (it will start a weird recursion and fail). Image
13. Want to execute some code each time you start IPython? Add it in the "startup" folder. It's a great way to automatically import some modules that you use all the time.
14. You can create a new profile with different settings. For example, you can create a profile just for debugging with verbose exceptions and plenty of debugging/profiling libraries automatically imported.
15. Output of the last command is stored in variable _
If you forgot to assign an expression to a variable, use var = _. Outputs from older commands are stored in variables constructed by adding a cell number to _. So _1 is the output of the first command in the current session. Image
16. You can edit ANY function or module (even those installed with pip) - just run "%edit function_name". IPython will find the source code of that function and open it for editing.

Below, I'm breaking the built-in randint() function by making it always return 42.
17. Want to share your code with someone? Run %pastebin <line numbers> (e.g. %pastebin 1-5) - this will take the first 5 lines from IPython, create an online pastebin (it's like GitHub gist), and return a link that you can send to someone. Image
18. Want to use IPython as a debugger?
Install "ipdb" and either use "ipdb.set_trace()" or run "export PYTHONBREAKPOINT=ipdb.set_trace" and use the built-in "breakpoint()" statement (requires Python >= 3.7). Thanks to @mborus for this idea!
@mborus 19. Want to execute code from another language? Type %%ruby, write some ruby code, press Enter twice, and IPython will run it without problems. It works for other languages, too (including Python 2). Image
18,5. And if you just want to start an IPython session in a specific place in your code (not as a debugger), drop the "from IPython import embed; embed()" line there.
@mborus 20. Use %store to save variables in IPython's database. That way, you can restore it with "%store -r variable" in another IPython session. Or set 'c.StoreMagics.autorestore = True' in configuration to automatically restore all variables each time you start IPython. Image
21. If you want to save your IPython session to a file and turn it into a proper program, use "%save filename.py first_cell-last_cell".
Then open https://t.co/vL2FCpvS8m in your editor and remove lines that you don't need. Et voilà! You have a Python script! Image
22. If you need to clean up code with an incorrect indentation or trailing ">" characters (for example, when copying from an email), copy that code and run %paste.

IPython will try to automatically fix the indentation, remove > characters and execute the code from the clipboard. Image
23. To see all the variables from the current session with their types and values, run %whos. Image
24. Do you want to speed up your code with asynchronous functions? IPython has an event loop built-in, so you can write and run async functions just like regular functions (it's called %autoawait and can be turned off if you want to start your own event loop). Image
25. You can run scripts that contain IPython-specific syntax (shell commands or magic commands). Just save them with the .ipy extension and run them with the "ipython filename.ipy" command. Image

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Sebastian Witowski

Sebastian Witowski Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!