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
12
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.
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
12345
#> 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.
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.