

Ask HN: Any tips for using Python Interactively? - breck

I'm SSH'ing into a Ubuntu box using Putty.<p>I need to run Python in interactive mode and am finding it pretty frustrating.<p>How do I get the last command? (like using the up arrow at the command line, or in IDLE on Windows Alt-P)<p>Any other tips?<p>I'm trying to get IPython going right now but am having some difficulties.<p>Thanks!
======
marcher
Up should work. If you want it to persist across sessions you'll have to
enable readline history.

You might find my pythonrc useful:
<http://bitbucket.org/brodie/dotfiles/src/tip/.pythonrc.py> \- 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.

~~~
breck
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)

------
artificer
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.

------
utnick
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?

~~~
breck
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)

~~~
tedshroyer
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

