Activar tab-completion en la Shell de Python

nov. 19, 2011·
Julio Batista Silva
Julio Batista Silva
· 2 min de lectura
  1. Crea el archivo ~/.pyrc con el siguiente contenido:

    $ cat .pyrc
    # ~/.pyrc
    # enable syntax completion
    try:
        import readline
    except ImportError:
        print "Module readline not available."
    else:
        import rlcompleter
        readline.parse_and_bind("tab: complete")
    

Si vas a usarlo solo en Linux, este try no es realmente necesario; lo puse alrededor de import readline, ya que utiliza la biblioteca GNU readline, que puede tener algunos problemas fuera de Linux.

  1. Exporta la variable para que se lea cada vez que se abre una shell de Python (usa la ruta completa con /home/usuario):

    julio@julio-acer ~> export PYTHONSTARTUP="/home/julio/.pyrc"
    
  2. Inserta esta misma línea en los archivos .bashrc y .zshrc, para que la variable siempre se inicialice. En csh, la asignación de variables es diferente. Añade la siguiente línea a ~/.cshrc:

    setenv PYTHONSTARTUP ~/.pythonrc"
    
  3. ¡Listo! Pulsa la tecla TAB para mostrar la lista de posibles completados:

    $ python
    Python 3.2.2 (default, Sep  5 2011, 04:52:19)
    [GCC 4.6.1 20110819 (prerelease)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import re
    >>> re.
    re.A                  re.__eq__(            re._compile(
    re.ASCII              re.__file__           re._compile_repl(
    re.DEBUG              re.__format__(        re._compile_typed(
    re.DOTALL             re.__ge__(            re._expand(
    re.I                  re.__getattribute__(  re._pattern_type(
    re.IGNORECASE         re.__gt__(            re._pickle(
    re.L                  re.__hash__(          re._subx(
    re.LOCALE             re.__init__(          re.compile(
    re.M                  re.__le__(            re.copyreg
    re.MULTILINE          re.__lt__(            re.error(
    re.S                  re.__name__           re.escape(
    re.Scanner(           re.__ne__(            re.findall(
    re.T                  re.__new__(           re.finditer(
    re.TEMPLATE           re.__package__        re.functools
    re.U                  re.__reduce__(        re.match(
    re.UNICODE            re.__reduce_ex__(     re.purge(
    re.VERBOSE            re.__repr__(          re.search(
    re.X                  re.__setattr__(       re.split(
    re.__all__            re.__sizeof__(        re.sre_compile
    re.__cached__         re.__str__(           re.sre_parse
    re.__class__(         re.__subclasshook__(  re.sub(
    re.__delattr__(       re.__version__        re.subn(
    re.__dict__           re._alphanum_bytes    re.sys
    re.__doc__            re._alphanum_str      re.template(
    

Alternativas

Existen shells de Python mejoradas, como iPython. Es bastante simple, interactiva y contiene varias funciones interesantes, incluido el autocompletado.

La wiki oficial sugiere otras.

Julio Batista Silva
Autores
Senior Cloud Developer
comments powered by Disqus