Autocompletion for your python shell|console|interpreter
July 2010
Sun Mon Tue Wed Thu Fri Sat
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
About
This site is an effort to share some of the base knowledge I have gathered through all this years working with Linux, FreeBSD, OpenBSD, Python or Zope, among others. So, take a look around and I hope you will find the contents useful.
Recent Entries
Recent Comments

collective.solr 1.0rc1 (Release candidate)
2010-07-30 plone.org releases

Products.cron4plone 1.1.5rc1 (Release candidate)
2010-07-30 plone.org releases

vs.dashboardmanager 0.2.6.1
2010-07-30 plone.org releases

Heads up! OpenBSD now supports multi-byte characters!
2010-07-30 OpenBSD Journal (undeadly.org)

Setting the Focus Distance on the Epson V700 Scanner
2010-07-29 betabug

Gnome Census Released (and Red Hat 16% vs Canonical 1% Flame)
2010-07-29 Ramble on

Cómo conectarse a bases de datos SQLite desde NetBeans
2010-07-29 vaites (dmnet)

Diferencias cambiando de Perl a Python
2010-07-28 blackshell

Monos y cacahuetes
2010-07-28 userlinux.net

No Gazoline
2010-07-28 betabug

[c2k10] (Part 5)
2010-07-28 OpenBSD Journal (undeadly.org)

Cómo evitar fbc_channel=1 con Facebook Fan/Like Box
2010-07-28 vaites (dmnet)

Copyright Nonsense
2010-07-28 Ramble on

New Plone Usergroup in Charlottesville, VA kicks off July 29th
2010-07-28 plone.org news

Redimensionar la ventana de Firefox sin extensiones
2010-07-27 vaites (dmnet)

ἀφορισμός XII: Silencio
2010-07-27 emereci

New committer: Baptiste Daroussin (ports)
2010-07-27 FreeBSD latest news

[c2k10] The Hackathon BBQ (Part 4) - June 25 - July 3, 2010, Edmonton, Alberta, Canada
2010-07-26 OpenBSD Journal (undeadly.org)

Setting up Bacula
2010-07-26 Evilcoder

Plone 4 upgrade coming to plone.org
2010-07-25 plone.org news

So I bought a Scanner
2010-07-24 betabug

FreeBSD 8.1 RELEASED
2010-07-24 Evilcoder

FreeBSD 8.1-RELEASE Available
2010-07-23 FreeBSD latest news

Announcing Tornado 1.0
2010-07-23 Ramble on

Sauna Sprint just around the corner
2010-07-22 plone.org news

April-June, 2010 Status Report
2010-07-22 FreeBSD latest news

O culeiro
2010-07-21 emereci

Limitando usuarios ssh en Mercurial
2010-07-21 userlinux.net

The Wire
2010-07-20 emereci

Comienza la mudanza, nos vamos a Reading
2010-07-18 blackshell

Recent Trackbacks
Categories
OpenBSD (8 items)
BSD (0 items)
FreeBSD (12 items)
Linux (2 items)
Security (3 items)
Python (18 items)
Zope (13 items)
Daily (120 items)
e-shell (8 items)
Hacks (7 items)
PostgreSQL (3 items)
OSX (7 items)
Nintendo DS (0 items)
enlightenment (0 items)
Apache (3 items)
Nintendo Wii (0 items)
Django (23 items)
Music (9 items)
Plone (7 items)
Varnish (0 items)
Lugo (1 items)
Sendmail (-1 items)
europython (7 items)
Archives

Syndicate this site (XML)

RSS/RDF 0.91

19 enero
2010

Autocompletion for your python shell|console|interpreter

this is an old one, but I would like to have it here as a reminder

One of the nice features of the django shell (that is, the python shell you get when you call ./manage.py shell inside a django project) is that it has tab autocompletion, that is, you can import a module and use tab-completion to see all the modules within it.

A lot of people are using iPython these days because of the lack of this feature on the default Python shell, but this is only by default, and if you are using iPython only because of the tab completion, forget about installing extra stuff, you only have to do this little trick:

import rlcompleter, readline
readline.parse_and_bind('tab:complete')

and you can use tab completion, for example:

>>> import smtplib
>>> smtplib.
smtplib.CRLF                      smtplib.SSLFakeFile               smtplib.__setattr__(
smtplib.LMTP                      smtplib.__all__                   smtplib.__sizeof__(
smtplib.LMTP_PORT                 smtplib.__class__(                smtplib.__str__(
smtplib.OLDSTYLE_AUTH             smtplib.__delattr__(              smtplib.__subclasshook__(
smtplib.SMTP                      smtplib.__dict__                  smtplib._have_ssl
smtplib.SMTPAuthenticationError(  smtplib.__doc__                   smtplib.base64
smtplib.SMTPConnectError(         smtplib.__file__                  smtplib.email
smtplib.SMTPDataError(            smtplib.__format__(               smtplib.encode_base64(
smtplib.SMTPException(            smtplib.__getattribute__(         smtplib.hmac
smtplib.SMTPHeloError(            smtplib.__hash__(                 smtplib.quoteaddr(
smtplib.SMTPRecipientsRefused(    smtplib.__init__(                 smtplib.quotedata(
smtplib.SMTPResponseException(    smtplib.__name__                  smtplib.re
smtplib.SMTPSenderRefused(        smtplib.__new__(                  smtplib.socket
smtplib.SMTPServerDisconnected(   smtplib.__package__               smtplib.ssl
smtplib.SMTP_PORT                 smtplib.__reduce__(               smtplib.stderr
smtplib.SMTP_SSL                  smtplib.__reduce_ex__(
smtplib.SMTP_SSL_PORT             smtplib.__repr__(

In this example, after importing smtplib I just wrote smtplib. in the shell and hit tab twice, to get all the available options in that module.

Nice, isn't it?

Ok, now it would be even nicer if we do not have to set tab completion each time we run the python shell, but this is an easy one.

Each time you run the python shell|console|interpreter, it will search for a variable in your environment called PYTHONSTARTUP, where you can set a file that will be executed after starting the shell (the perfect place to put some init-lines).

So, all you have to do is, first, add this two lines to a file, for example, called .pythonrc in your home:

$ cat ~/.pythonrc
import rlcompleter, readline
readline.parse_and_bind('tab:complete')
$

Second, add the PYTHONSTARTUP variable to your shell profile file. If you are using csh, tcsh or similar shells, this will do it:

echo "setenv PYTHONSTARTUP ~/.pythonrc" >> ~/.cshrc

If your shell is sh, bash, ksh or similars, this will do it:

echo "export PYTHONSTARTUP=~/.pythonrc" >> ~/.profile

(check your OS info, perhaps instead of .profile it could be .kshrc, .bashrc or something like that).

After that, you will be able to use tab completion each time in your default python shell, with no additional dependencies!

NOTE: Of course, you should take a look at iPython anyway, as it has a lot more features than tab-completion, it is a powerful tool you may find very very useful.

Posted by wu at 10:04 | Comments (2) | Trackbacks (0)
<< A parabola do carballo e mais o eucalipto | Main | Hidden emails for comments in this COREBlog >>
Comments
Re: Autocompletion for your python shell|console|interpreter

IPython allows non-blocking interaction with Tkinter, GTK, Qt and WX. The standard Python shell only allows interaction with Tkinter. IPython can interactively manage parallel computing clusters using asynchronous status callbacks and/or MPI. IPython can be used as a system shell replacement, especially on Windows, which has a minimally capable shell. Its default behavior is largely familiar from Unix shells, but it allows customization and the flexibility of executing in a live Python environment

Posted by: double boiler at julio 27,2010 15:03
Re: Autocompletion for your python shell|console|interpreter

Hey "double boiler", thnx for the comment. I know IPython has a lot of features (you only have to check http://ipython.scipy.org to get more info about it.

Anyway, there are times when people don't need all those features, but just need some things like completion or history saving to the default python shell, those are the perfect audience for my post.

This last week, while attending the Europython 2010 conference, I found bpython, another really interesting alternative to the default python interpreter/shell/console. You should take a look at it.

Posted by: Wu at julio 27,2010 17:06
Trackbacks
Please send trackback to:http://blog.e-shell.org/221/tbping
There are no trackbacks.
Post a comment