[Tutor] Good Text Editor/IDE for Python

Steven D'Aprano steve at pearwood.info
Mon Sep 1 15:59:32 CEST 2014


On Mon, Sep 01, 2014 at 11:57:08AM +1000, Cameron Simpson wrote:
> On 01Sep2014 11:13, Steven D'Aprano <steve at pearwood.info> wrote:
> >Just recently, I've customised my interactive Python with a powerful set
> >of tab completion commands, similar to that provided by IPython. While
> >typing, if I hit tab, it will try to complete the current variable,
> >function, module or file name. I don't know how I programmed without it
> >all these years :-)
> 
> I must try that sometime.

Please do :-)

You can get the module from here:

http://code.google.com/p/tabhistory/

Or directly using Mercurial. Run this command at the shell:

hg clone https://code.google.com/p/tabhistory/

For experts: you can work out what to do next :-) It's a single .py file 
and it has lots of documentation.

For beginners: once you have run the hg comand above, copy the file 
tabhistory/tabhistory.py somewhere where Python will see it. Then, start 
the Python interactive interpreter, and run:

import tabhistory

If that succeeds, it is now running! If it does not, please let me know 
what errors you get. I am especially interested in the experience of Mac 
and Windows users.

(Windows users: you will need the third-party pyreadline module.)

Try it out: at the interactive interpreter, type:


imp[TAB]

where [TAB] means "press the TAB key". Python should complete the 
command line and give you "import". Continue to type:

import co[TAB][TAB]

and Python will print the names of all modules beginning with "co" that 
it can see. On my system, that is:

code         codeop       colorsys     compileall   contextlib   copy
codecs       collections  commands     compiler     cookielib    copy_reg

Keep typing:

import coll[TAB]

and Python will complete the command to:

import collections


Additional features:

- If you press TAB at the beginning of a line, it will insert an 
  actual tab character. That can be configured if you prefer spaces.

- Inside quoted strings, it will complete on file names.

- After something like "from module import " pressing TAB will 
  complete on the attributes of module, but only if module has 
  already been imported and is in the sys.modules cache. This is
  a security feature.

- As the name hints at, tabhistory also enables command history.
  Press the up-arrow repeatedly to call up previous commands, and
  down-arrow to move forward through the history again.


tabhistory also creates two objects for customizing the completion and 
history functions:

from tabhistory import history, completer

will let you conveniently work with them. Both are extensively 
documented. One particularly useful feature: calling the history object 
history() will display the last 10 (by default) lines in the history.

(Question: anyone know how to tell readline to jump straight to a 
particular history line? M-< goes to the oldest line in the history, and 
M-> goes to the most recent, but how would I go straight to line 87?)


Feedback, suggestions and bug reports will be gratefully accepted.


-- 
Steven


More information about the Tutor mailing list