Tuesday, July 2, 2013

Add History and Tab Completion to the Default Python Shell

So I've in the python shell quite a bit lately and figured it would be nice to be able to have readline history between sessions as well as tab completion. Yes I know things like iPython exist as well as others but wanted to keep it simple. It turns out its not that hard to enable and here is how you do it.

First you'll need to add a line to your .bashrc or .profile setting your PYTHONSTARTUP environment variable

# ~/.bashrc
export PYTHONSTARTUP=$HOME/.pythonstartup.py

Then copy this scripts contents to file your new environment variable points to

# ~/.pythonstartup.py
    import readline
    import rlcompleter
    import atexit
    import os
except ImportError:
    print "Python shell enhancement modules not available."
    histfile = os.path.join(os.environ["HOME"], ".pythonhistory")
    import rlcompleter
    readline.parse_and_bind("tab: complete")
    if os.path.isfile(histfile):
    atexit.register(readline.write_history_file, histfile)
    del os, histfile, readline, rlcompleter, atexit
    print "Python shell history and tab completion are enabled."

That's it. Now anytime you pop into the python shell you'll have history from previous sessions as well as tab completion. Enjoy!

Edit: If you're not running the latest version of OSX you may need to change the readline.pasre_and_bind line to the following

#readline.parse_and_bind("tab: complete")
readline.parse_and_bind("bind ^I rl_complete")


  1. Thanks, really useful. I usually use bpython, it has syntax highlighting in addition. In the source you can remove "import rlcompleter", you don't seem to use it. Remove in the "del" line too.

    1. Sorry, I just realized that rlcompleter is necessary. However, you import it twice.

    2. Ha, nice catch! I'll fix it! The del was so they didn't pollute the global namespace but it really isn't that big of a deal. Someone else had it in their example and thought it was a best practice.

      I think I'll be using bpython as well. Thanks for your feedback!

    3. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

  2. There's something wrong with this idea in a virtualenv:

    Python 2.7.4 (v2.7.4:026ee0057e2d, Apr 6 2013, 11:43:10)
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    Traceback (most recent call last):
    File "/Users/emptysquare/bin/pythonstartup.py", line 14, in
    IOError: [Errno 2] No such file or directory
    >>> histfile

    The file exists, and I can read it with open(histfile).read(), and it works with no virtualenv active. Haven't investigated much further than this -- readline.c in the Python source doesn't do anything unexpected in its read_history_file() implementation. In any case, this seems to work, both in a virtualenv and not in one:

    if os.path.isfile(histfile):
    for line in open(histfile):

    1. You need to print out your histfile variable and see what its contents are. My guess for some reason your HOME environment variable isn't defined.

  3. for my case where arrow up/down wasn't working pip install'ing readline then setting up the above worked for me

    Thank you!

  4. I feel satisfied to read your blog, you have been delivering a useful & unique information to our vision even you have explained the concept as deep clean without having any uncertainty, keep blogging. python Online Training Bangalore

  5. Thanks for sharing this in here. You are running a great blog, keep up this good work.
    Online degree courses
    Distance education