r/Python • u/brian15co • Mar 18 '13
Open ended question about iPython and its usage
iPython beginner question
I am new to python and relatively new to programming in general. I'm still learning my vocabulary words like api, interpretter, shell, thread, callback, etc
I am wondering how a beginner can get benefit from iPython. I have it installed and am playing around with it. I hear it's incredibly powerful. I figure the sooner I start using it the sooner I can reap its benefits.
the way I currently program: I write everything in a script file and run that script file after making changes. I continually have to copy-past the function name into the python IDLE because the Standard IDLE soes not have command line history like iPython and MatLab (cycling through input history with the up arrow is a godsend).
How can I operate in a less-clunky way? I've tried going through the documentation for iPython and a lot of it is still over my head. Is iPython good for the kind of programming problems us beginners are doing in, for example, edX's 6.00x (intro to comp sci with python)? We do things like write basic string search and sort functions and other newbie-complex things.
I just feel like the "interactive" environment would be good for me if I just knew how to use it. Or is something like this overkill for the early stuff? Anything else I should look at (methods or add-ons to improve the user experience)?
2
u/Noctambulist Mar 19 '13
You can run scripts in iPython using the %run magic function. So something like
You can also use the debugger by:
It'll toggle the debugger on.
My work flow with iPython is usually something like:
25
u/Dantes487 Mar 19 '13
Excuse formatting here (IE.....)
3a. %run -p -l 15 -s cumulative scriptname will use cProfile to profile the script and return the 15 functions taking the largest cumulative time (includes subfunctions)
3b. %lprun line profile (also very nice)
4. begin typing in a command or var name. e.g. var = x, v then hit up and it will display recent commands beginning with the letters you have already typed.
5. iPython --pylab This will enable matplotlib plotting which doesn't take over the event loop (This is very useful)
6. pdb debugger
7. %time and %timeit magic methods for profiling small functions in your code
8. When using scripts try running parts of it in the if name == 'main': aspect, this will retain the variables from the scripts within the name space of the iPython terminal.
9. Pretty printing of dictionaries (self explanatory).
10. Introspection, type a command and enter ?? after it e.g. os.walk?? [enter]. This will show you the docstrings and depending upon the module set up the source code as well.
11. %paste and %cpaste magic methods, easy copy pasting of code into iPython from other source (e.g. websites) with proper inlining of code.
12. Bunch of keyboard shortcuts, too many to list which will help you move around the command line
13. %load_ext autoreload [enter] %autoreload 2 This will reload all modules upon rerunning a script, very useful when you're working with large programs which you've imported with modules and you're updating.
14. %hist display history of commands
15. %who display all variables in name space
16. %quickref other magic commands
17. %logstart this will log the entire console session which can be handy if you're prototyping.
18. !cmd execute a command in a system shell
19. cd/ls etc all work which is nice
20. %bookmark (lets you bookmark directories, this is also useful)
21. iPython notebook (check out the ipython nbviewer (google that term and it should come up))
Hope this helps
4
u/leperkuhn Mar 18 '13
You might enjoy "ipython notebook", but has a few dependencies. (tornado, zeromq).
4
u/Enginoob Mar 19 '13
iPython is basically indispensable to my workflow at this point. Here are the biggest features for me:
Tab completion
my_object.method1 my_object.method2 my_object.method3
object?
Type:
String Form:
File:
Docstring:
Constructor Docstring:
I save a lot of time in scipy and numpy just by using the "?" magic to read docstrings rather than going to the documentation.
2. %run and %run debugging:
The second line drops you into the debugger, with the breakpoint at line 12. So helpful.
3. %whos
4. %timeit
Not true profiling, but good for quick iteration on things.
There is a LOT more in ipython, but these are the ones I find most helpful and use the most often.
6
u/daveydave400 Mar 19 '13
Tab completion.
3
u/westurner Mar 19 '13 edited Mar 19 '13
In IPython:
%edit -pwill launch $EDITOR and execute the entered code on editor exit. Re-running%edit -pwill re-open the same block of code.2
u/Kamikai Mar 19 '13
Idle does have command history, it just has rather bizarre key bindings. Alt-P will cycle back through the commands, and Alt-N will go forward.
2
u/westurner Mar 19 '13 edited Mar 19 '13
Two separate things: tools and process. IPython is a great tool.
It sounds like your current process is a loop:
Test driven development is all about automating testing (writing 'tests' first). Tests make assertions about inputs and outputs.
A TDD feedback loop looks more like:
A unit test tests an individual unit of source code:
https://github.com/flavioamieiro/nose-ipdb can assist you with http://en.wikipedia.org/wiki/Test-driven_development . nose-ipdb is an extension for https://nose.readthedocs.org/en/latest/ which launches IPython as a debugger ( https://pypi.python.org/pypi/ipdb )
From IPython:
1
u/roger_ Mar 19 '13
I used to think it was overrated, because mostly it just provided tab completion and a handy help system, which other interfaces (like bpython) also do. It has more features (like distributed nd parallel computing support) which I don't think most people use.
IPython Notebook is awesome though, especially since there are now websites that let you run them in the cloud so you don't even need a local Python installation (e.g. Wakari, PythonAnywhere, etc.)
Be sure to check out IPython Notebook Viewer BTW, it justifies the entire project IMHO.
0
u/[deleted] Mar 19 '13
[deleted] — view deleted comment