Archive

Posts Tagged ‘readline’

Anaconda: problem with the readline module under Manjaro

October 5, 2015 Leave a comment

Problem
I tried the Anaconda Python distribution under Manjaro, but there was a problem with the “readline” module:

>>> import readline
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: libncursesw.so.5: cannot open shared object file: No such file or directory

Solution
As root:

# cd /usr/lib
# ln -s libncursesw.so libncursesw.so.5

It may be an ugly hack, but it solved the issue for me.

Categories: python Tags: , ,

How to make a python, command-line program autocomplete arbitrary things

June 4, 2013 2 comments

This entry is based on this SO post.

Problem
You have an interactive command-line Python script and you want to add autocompletion to it when hitting the TAB key.

Solution
Here is a working example (taken from here):

import readline

addrs = ['angela@domain.com', 'michael@domain.com', 'david@test.com']

def completer(text, state):
    options = [x for x in addrs if x.startswith(text)]
    try:
        return options[state]
    except IndexError:
        return None

readline.set_completer(completer)
readline.parse_and_bind("tab: complete")

while True:
    inp = raw_input("> ")
    print "You entered", inp
Categories: python Tags: , ,

bash-like functionalities in command-line Python script

Problem
You have an interactive Python script that reads input from the command line. You want to add bash-like functionalities to it like moving the cursor with the arrows, jump to the front with Home, jump to the end with End, browse previous commands with the up arrow, etc.

Solution
You won’t believe what is needed for this:

import readline

Yes, that’s it. Just import it and you are good to go.

Minimal example:

import readline

while True:
    inp = raw_input("> ")
    print "You entered", inp

Alternative
Before discovering the readline module, I used to start my scripts with “rlwrap“:

rlwrap my_script.py

It does the trick too. I put the line above in a script called “my_script.sh” and I launched this latter one. However, “import readline” is simpler.

Categories: python Tags: , , ,

rlwrap: adding keyboard editing functionalities to your scripts

January 1, 2013 Leave a comment

Problem
You have an interactive script that waits for some user input, similarly to the (Python) shell. In bash, you can use the arrow keys for editing / browsing, but it’s not available in a Python script right away. If you read the user input with raw_input() for instance, you cannot move the cursor back with the left arrow, or you cannot browse previous commands with the up arrow.

And still… How to add such bash-like functionalities to your script? The method must be absolutely painless, of course.

Solution
Use the command “rlwrap“, which magically adds these functionalities to any script. Awesome. So instead of launching your script with “./something.py“, launch it like this:

$ rlwrap something.py

You might have to install rlwrap with “sudo apt-get install rlwrap“. More info about rlwrap: here.

Categories: python Tags: , , ,