Ask HN: Any tips for using Python Interactively?

4 points by breck ↗ HN
I'm SSH'ing into a Ubuntu box using Putty.

I need to run Python in interactive mode and am finding it pretty frustrating.

How do I get the last command? (like using the up arrow at the command line, or in IDLE on Windows Alt-P)

Any other tips?

I'm trying to get IPython going right now but am having some difficulties.

Thanks!

6 comments

[ 2120 ms ] story [ 246 ms ] thread
Up should work. If you want it to persist across sessions you'll have to enable readline history.

You might find my pythonrc useful: [link redacted] - be sure to run export PYTHONSTARTUP=~/.pythonrc.py.

My script does the following:

1. Enables readline tab completion. 2. Makes tab insert four spaces. 3. Persists command history (~/.pyhistory). You can disable it by running NOHIST= python. 4. Changes the print hook on functions to include their signature, as well as their documentation. 5. Changes the print hook to use pprint for everything else, also taking into account the terminal width. 6. Colorizes exceptions if Pygments is available. 7. Adds a source() function that shows you the source code of a function, class, or module in your pager. Code is syntax highlighted if Pygments is available. This function works on zipped eggs as well.

Also, enabling readline means all your ~/.inputrc bindings will work. So for example you can hit ^R to search back in history for a snippet you wrote.

Oops. As it turns out this box is running FC. I'm going to try things on a different box. Thanks for pythonrc! (doesn't quite work on this box because readline isn't working for some reason)
hmmm... up arrow gets the last command for me on ubuntu with putty and python 2.5.1

what other frustrating things are you finding?

When I hit up I get: ^[[A

Some other frustrations: reload() seems to not work on occasion(haven't figured out the pattern yet)

Tab completion would be nice (I'm going to try the script above)

I think because of the way import works, that a reload wouldn't update anything that the module you're reloading imports.

  $echo -e "def foo():\n\tprint 'bar'" > a.py
  $echo -e "import a\ndef bar():\n\ta.foo()" > b.py
  $python
  Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) 
  >>>import b
  >>>b.bar()
  bar
  >>>f = open('a.py', 'w')
  >>>f.write("def foo():\n\tprint 'changed'")
  >>>f.close()
  >>>reload(b)
  <module 'b' from 'b.pyc'>
  >>> # Notice that it imported from 'b.pyc' not 'b.py'
  ... b.bar()
  bar
  >>>reload(a)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  NameError: name 'a' is not defined
  >>>#But I need to update a
  ...reload(b.a)
  <module 'a' from 'a.py'>
  >>>#That loaded from 'a.py'
  ...b.bar()
  changed
  >>>exit()
edit

  #You could have done it like this too
  >>>import a
  >>>import b
  >>>b.bar()
  bar
  >>>###Something changes a.py 
  >>>reload(a)
  >>>b.bar()
  changed
If you are going to use it interactively, I strongly suggest you insist in using IPython. Try to read the nice manual first in order to get yourself familiar. It offers autocompletion via the tab key, you can easily execute shell commands, change directories etc.