Some Python shells, like iPython, provide autocompletion functionality while typing in code. If you’re like me and use the default shell in your terminal (the one that starts up when you just execute python
) this feature isn’t automatically available. Alas, I discovered a way to make this possible! Click the read more to find out how.
I recently came across this blog post that describes a very simple way to activate tab-completion for the default Python shell.
First, create a file called .pythonrc
in your home directory. In that file, type or paste in the following:
import rlcompleter, readline readline.parse_and_bind('tab:complete')
Second, this file will need to be executed whenever the default Python shell is opened so that these packages can be imported and the tab-complete function can be loaded. In order to do this, we need to add the following to our .profile
or .bashrc
file (or whichever your system uses):
echo "export PYTHONSTARTUP=~/.pythonrc" >> ~/.profile
If you reopen your terminal and start up the default Python shell interpreter, you should be able to use tab-completion on your functions and variable names.
Enjoy!
-Daniel