Python: Protip - Enable Tab Completion in Python Shell

written in python, shell, tab complete

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.



shell
1
2
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.



shell
1
#> touch ~/.pyrc


Step 2:


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



~/.pyrc
1
2
3
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).



shell
1
2
3
4
5
#> 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:



shell
1
#> 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.



shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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.