I like the idea of rlcompleter every time you launch python, but so far in various tests have not been able to get it working. So today (to get Boilerplate Python back running) I’m spending some time playing with rlcompleter.

It turns out that with a few tricks, we can have a cross-platform rlcompleter function, that works on Linux/Windows/OSX all in one file (this is new hotness). On windows, you will need to install pyreadline for windows, but we can rock that.

#Code  UUID = '9301d536-860d-11de-81c8-0023dfaa9e40'
import sys
try:
        import readline
except ImportError:
        try:
                import pyreadline as readline
        # throw open a browser if we fail both readline and pyreadline
        except ImportError:
                import webbrowser                
                webbrowser.open("http://ipython.scipy.org/moin/PyReadline/Intro#line-36")
                # throw open a browser
        #pass
else:
        import rlcompleter
        if(sys.platform == 'darwin'):
                readline.parse_and_bind ("bind ^I rl_complete")
        else:
                readline.parse_and_bind("tab: complete")

Run that script on Windows, Linux, or OSX, and you (should) get tab completion for both basic commands, and files. And if you don’t have readline installed on windows, it will launch a web browser right to pyreadline. Readon, there are some details on how to get it running on every launch in python interactive console. Read on!

So, for tab complete to run when you run the interactive console, you have to setup a launch file to run everytime that python starts. In general, this is done by setting up a PYTHONSTARTUP system variable.

On OS X and Linux:

Create an file ‘.pythonrc’ in your home directory, and put that code into it. This is a file to launch each time that python starts (in the command line environment that is).
Then set an environment variable ‘PYTHONSTARTUP to points to that file. You will probably want to create (or append it to) .profile in your home directory.

PYTHONSTARTUP= ~/.pythonrc

not into ‘~/.MacOSX/environment.plist’

On Windows:

You need to create a similar file (here we called it ‘.inputrc’ and put it in your home directory.

you can even try importing your standard bash/command-line into your environment too:

import os.path
# history file
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
    readline.read_history_file(histfile)
except IOError:
    pass