Package: fancycompleter
fancycompleter: colorful Python TAB completionWhat is is?fancycompleteris a module to improve your experience in Python by
adding TAB completion to the interactive prompt. It is an extension of
the stdlib'srlcompletermodule.Its best feature is that the completions are displayed in different
colors, depending on their type:In the image above, strings are shown in green, functions in blue,
integers and boolean in yellows,Nonein gray, types and classes in
fuchsia. Everything else is plain white.fancycompleteris compatible with Python 3. However, by default colors
don't work on Python 3, see the sectionHow do I get
colors?for details.Other featuresTo save space on screen,fancycompleteronly shows the characters
"after the dot". By contrast, in the example aboverlcompletershows everything prepended by"sys.".If we press<TAB>at the beginning of the line, a real tab
character is inserted, instead of trying to complete. This is useful
when typing function bodies or multi-line statements at the prompt.Unlikerlcompleter,fancycompleterdoescomplete expressions
containing dictionary or list indexing. For example,mydict['foo'].<TAB>works (assuming thatmydictis a dictionary
and that it contains the key'foo', of course :-)).Starting from Python 2.6, if the completed name is a callable,rlcompleterautomatically adds an open parenthesis(. This is
annoying in case we do not want to really call it, sofancycompleterdisable this behaviour.InstallationFirst, install the module withpiporeasy_install:$ pip install fancycompleterThen, at the Python interactive prompt:>>> import fancycompleter
>>> fancycompleter.interact(persist_history=True)
>>>If you want to enablefancycompleterautomatically at startup, you can
add those two lines at the end of yourPYTHONSTARTUPscript.If you donothave aPYTHONSTARTUPscript, the
following command will create one for you in~/python_startup.py:$ python -m fancycompleter installOn Windows,installautomatically sets thePYTHONSTARTUPenvironment
variable. On other systems, you need to add the proper command in~/.bashrcor equivalent.Note: depending on your particular system,interactmight need to
play dirty tricks in order to display colors, although everything should
"just work". In particular, the call tointeractshould be the last
line in the startup file, else the next lines might not be executed. See
sectionWhat is really going on?for
details.How do I get colors?If you are usingPyPy, you can stop reading now, asfancycompleterwill work out of the box.If you are usingCPython on Linux/OSXand you installedfancycompleterwithpiporeasy_install, they automatically
installedpyreplas a requirement, and you should also get colors out
of the box. If for some reason you don't want to usepyrepl, you
should keep on reading.By default, in CPython line input and TAB completion are handled byGNU
readline(at least
on Linux). However,readlineexplicitly strips escape sequences from
the completions, so completions with colors are not displayed correctly.There are two ways to solve it:(suggested) don't usereadlineat all and rely onpyrepluse a patched version ofreadlineto allow colorsBy default,fancycompletertries to usepyreplif it finds it. To
get colors you need a recent version, >= 0.8.2.Starting from version 0.6.1,fancycompleterworks also onWindows,
relying onpyreadline. At the
moment of writing, the latest version ofpyreadlineis 2.1, which doesnotsupport colored completions; here is thepull
requestwhich adds
support for them. To enable colors, you can installpyreadlinefromthis forkusing the following
command:pip install --upgrade https://github.com/antocuni/pyreadline/tarball/masterIf you are usingPython 3,pyrepldoes not work, and thus is not
installed. Your only option to get colors is to use a patchedreadline, as explained below.I really want to use readlineThis method is not really recommended, but if you really want, you can
use use a patched readline: you can find the patches in themisc/directory:forreadline-5.2forreadline-6.0You can also try one of the following precompiled versions, which has
been tested on Ubuntu 10.10: remember to put them in a place where the
linker can find them, e.g. by settingLD_LIBRARY_PATH:readline-6.0 for32-bitreadline-6.0 for64-bitOnce it is installed, you should double-check that you can find it, e.g.
by runninglddon Python'sreadline.somodule:$ ldd /usr/lib/python2.6/lib-dynload/readline.so | grep readline
        libreadline.so.6 => /home/antocuni/local/32/lib/libreadline.so.6 (0x00ee7000)Finally, you need to forcefancycompleterto use colors, since by
default, it uses colors only withpyrepl: you can do it by placing a
custom config file in~/.fancycompleterrc.py. An example config file
ishere(remind that you need to put a dot in front of the filename!).CustomizationTo customize the configuration of fancycompleter, you need to put a file
named.fancycompleterrc.pyin your home directory. The file must
contain a class namedConfiginheriting fromDefaultConfigand
overridding the desired values.What is really going on?The default and preferred way to get colors is to usepyrepl. However,
there is no way to tell CPython to usepyreplinstead of the built-in
readline at the interactive prompt: this means that even if we install
our completer inside pyrepl's readline library, the interactive prompt
won't see it.The issue is simply solved by avoiding to use the built-in prompt:
instead, we use a pure Python replacement based oncode.InteractiveConsole.
This brings us also some niceties, such as the ability to do multi-line
editing of the history.The console is automatically run byfancycompleter.interact(),
followed bysys.exit(): this way, if we execute it from the script inPYTHONSTARTUP, the interpreter exits as soon as we finish the use the
prompt (e.g. by pressing CTRL-D, or by callingquit()). This way, we
avoid to enter the built-in prompt and we get a behaviour which closely
resembles the default one. This is why in this configuration lines afterfancycompleter.interact()might not be run.Note that if we are usingreadlineinstead ofpyrepl, the trick is
not needed and thusinteract()will simply returns, letting the
built-in prompt to show up. The same is true if we are running PyPy, as
its built-in prompt is based on pyrepl anyway.