Posts in tab complete

Python: Protip - Enable Tab Completion in Python Shell

I was recently chatting with someone that made the statement; “The only reason why I use my text editor is because of tab completion, I am a minimalist pythonista.”. I responded with, “If your a minimalist, why don’t you have tab completion enabled in the interpreter?” They were unaware of the ability to enable tab completion within python. Here is a my PSA on how to enable this behavior. :)



By following these instructions you will be able to enable tab-completion within your python shell.



Preperation steps:


Before enabling tab-completion, you may need to install 2 python modules (rlcompleter, readline). While these libraries are mostly included with python2.6+, some versions (OS X, for example), require the updated version to allow readline to function correctly.



```bash shell

pip install readline pip install rlcompleter

```



Step 1:


If you don’t already have a ~/.pyrc file, this command will create one for you, which is required for this trick to work.



```bash shell

> touch ~/.pyrc

```



Step 2:


Now we will create a file within your homedir which will instruct python to bind tab completion at python launch.



```python ~/.pyrc

import rlcompleter import readline readline.parse_and_bind(“tab: complete”)

```



Step 3:


Now to ensure that your newly created ~/.pyrc file is executed each time python starts, add the following to your ~/.bashrc (or equiv. shell rc).



```bash shell

> export PYTHONSTARTUP=“[PATH TO PYRC FILE]/.pyrc”

*OR TO MAKE THE CHANGE PERSIST TERMINAL CLOSURE

> echo export PYTHONSTARTUP=“[PATH TO PYRC FILE].pyrc” >> ~/.bashrc

```



Now to test, execute the following:



```bash shell

> source ~/.bashrc #reloads your ~/.bashrc file (if you added the entry to your ~/.bashrc, else ignore)

```



Now we will open the actual python shell, and view the tab completion goodness.



```python shell

Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type “help”, “copyright”, “credits” or “license” for more information.

import os os. Display all 234 possibilities? (y or n) os.EX_CANTCREAT os.class( os.fchdir( os.putenv( os.EX_CONFIG os.delattr( os.fchmod( os.read( os.EX_DATAERR os.dict os.fchown( os.readlink( os.EX_IOERR os.doc os.fdopen( os.remove( os.EX_NOHOST os.file os.fork( os.removedirs( os.EX_NOINPUT os.format( os.forkpty( os.rename( os.EX_NOPERM os.getattribute( os.fpathconf( os.renames( os.EX_NOUSER os.hash( os.fstat( os.rmdir( os.EX_OK os.init( os.fstatvfs( os.sep os.EX_OSERR os.name os.fsync( os.setegid( os.EX_OSFILE os.new( os.ftruncate( os.seteuid( os.EX_PROTOCOL os.package os.getcwd( os.setgid( os.EX_SOFTWARE os.reduce( os.getcwdu( os.setgroups( os.EX_TEMPFAIL os.reduce_ex( os.getegid( os.setpgid( os.EX_UNAVAILABLE os.repr( os.getenv( os.setpgrp( os.EX_USAGE os.setattr( os.geteuid( os.setregid( os.F_OK os.sizeof( os.getgid( os.setreuid( os.NGROUPS_MAX os.str( os.getgroups( os.setsid( os.O_APPEND os.subclasshook( os.getloadavg( os.setuid( os.O_ASYNC os.copy_reg os.getlogin( os.spawnl( os.O_CREAT os.execvpe( os.getpgid( os.spawnle( os.O_DIRECTORY os.exists( os.getpgrp( os.spawnlp( os.O_DSYNC os.exit( os.getpid( os.spawnlpe( os.O_EXCL os.get_exports_list( os.getppid( os.spawnv( os.O_EXLOCK os.make_stat_result( os.getsid( os.spawnve( os.O_NDELAY os.make_statvfs_result( os.getuid( os.spawnvp( os.O_NOCTTY os.pickle_stat_result( os.initgroups( os.spawnvpe( os.O_NOFOLLOW os.pickle_statvfs_result( os.isatty( os.stat( os.O_NONBLOCK os.spawnvef( os.kill( os.stat_float_times( os.O_RDONLY os.abort( os.killpg( os.stat_result( os.O_RDWR os.access( os.lchflags( os.statvfs( os.O_SHLOCK os.altsep os.lchmod( os.statvfs_result( os.O_SYNC os.chdir( os.lchown( os.strerror( os.O_TRUNC os.chflags( os.linesep os.symlink( os.O_WRONLY os.chmod( os.link( os.sys os.P_NOWAIT os.chown( os.listdir( os.sysconf( os.P_NOWAITO os.chroot( os.lseek( os.sysconf_names os.P_WAIT os.close( os.lstat( os.system( os.R_OK os.closerange( os.major( os.tcgetpgrp( os.SEEK_CUR os.confstr( os.makedev( os.tcsetpgrp( os.SEEK_END os.confstr_names os.makedirs( os.tempnam( os.SEEK_SET os.ctermid( os.minor( os.times( os.TMP_MAX os.curdir os.mkdir( os.tmpfile( os.UserDict os.defpath os.mkfifo( os.tmpnam( os.WCONTINUED os.devnull os.mknod( os.ttyname( os.WCOREDUMP( os.dup( os.name os.umask( os.WEXITSTATUS( os.dup2( os.nice( os.uname( os.WIFCONTINUED( os.environ os.open( os.unlink( os.WIFEXITED( os.errno os.openpty( os.unsetenv( os.WIFSIGNALED( os.error( os.pardir os.urandom( os.WIFSTOPPED( os.execl( os.path os.utime( os.WNOHANG os.execle( os.pathconf( os.wait( os.WSTOPSIG( os.execlp( os.pathconf_names os.wait3( os.WTERMSIG( os.execlpe( os.pathsep os.wait4( os.WUNTRACED os.execv( os.pipe( os.waitpid( os.W_OK os.execve( os.popen( os.walk( os.X_OK os.execvp( os.popen2( os.write( os._Environ os.execvpe( os.popen3(
os.all os.extsep os.popen4(
os.

```



In the above example, I have imported the “os” module, typed “os.” and have pressed . Now all of the possible matched object names available within the module are shown. viola, python tab completion enabled.

written in python, shell, tab complete