Autocompletion for your python shell|console|interpreter
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.