Why is the Python readline module not available on OS X?

The documentation of the Python readline module says "Availability: Unix". However, it doesn't appear to be available on OS X, although other modules marked as Unix are available. Here is what I'm using:

$ uname -a
Darwin greg.local 8.11.1 Darwin Kernel Version 8.11.1: Wed Oct 10 18:23:28 PDT 2007; root:xnu-792.25.20~1/RELEASE_I386 i386 i386
$ which python
/usr/bin/python
$ python
Python 2.3.5 (#1, Nov 26 2007, 09:16:55) 
[GCC 4.0.1 (Apple Computer, Inc. build 5363) (+4864187)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import readline
Traceback (most recent call last):
  File "", line 1, in ?
ImportError: No module named readline
>>> 

I have also installed Python 2.5 through MacPorts but readline is not available there either.

What can I do to provide readline functionality for Python's raw_input() function on OS X?


Asked by: Dainton785 | Posted: 05-10-2021






Answer 1

Have you tried to install the py-readline (or py25-readline for Python 2.5) port?

Also, in the snippet above, you are NOT using the MacPort python, but rather the Apple Python.

The MacPort version should be located in the /opt/local directory structure. You should check your path.

Answered by: Julia417 | Posted: 06-11-2021



Answer 2

Try rlwrap. It should work with any version of python and in general any shell.

Install via brew install rlwrap on Mac OS X

usage as rlwrap python. It stores history as well.

Answered by: Cherry510 | Posted: 06-11-2021



Answer 3

It's not shipped in OS X because of licensing issues (restrictions brought to you by the GPL).

Macports python should be fine if you have the readline package installed.

Answered by: Brad334 | Posted: 06-11-2021



Answer 4

You should be able to get readline support on Mac's native Python. Apparently it's implemented via BSD's editline, and you have to start it up slightly differently. I just tested this on a Mac, and it works fine (OS X 10.5.7). See man python for more details, the section "Interactive Input Editing and History Substitution" - mirrored on the web here.

luga ~ $ python 
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import rlcompleter
>>> import readline
>>>

Answered by: Stella353 | Posted: 06-11-2021



Similar questions

python - How to solve this readline error in Veil module not found

I am getting a bash error, when using Windows, after running Veil.py module. Please could you help me to work out how to fix this? I have tried: pip install readline pip3 install readline pip install pyreadline However, none of these commands worked for me. Grateful for any pointers as to how to fix this?


python - Getting readline to block on a FIFO

I create a fifo: mkfifo tofetch I run this python code: fetchlistfile = file("tofetch", "r") while 1: nextfetch = fetchlistfile.readline() print nextfetch It stalls on readline, as I would hope. I run: echo "test" > tofetch And my program doesn't stall anymore. It reads the line, and then continues looping forev...


macos - Python readline and UTF-8

I'm trying to use raw_input() with readline and utf-8 encoding (on OSX 10.6.8). Imagine the following snippet: import readline while True: try: inp = raw_input('> ') except EOFError: break This works except when trying to type non standard ASCII letters, say 'å' or 'ä'. When doing this no input is written, I guess readline is filtering this somehow....


Python readline - reads only first line

#1 input_file = 'my-textfile.txt' current_file = open(input_file) print current_file.readline() print current_file.readline() #2 input_file = 'my-textfile.txt' print open(input_file).readline() print open(input_file).readline() Why is it that #1 works fine and displays the first and second line, but #2 prints 2 copies of the first line and doesn't print the same as #1 ?


python - How can I use readline method?

I have this trivial code: from sys import argv script, input_file = argv def fma(f): f.readline() current_file = open(input_file) fma(current_file) The contents of the txt file is: Hello this is a test.\n I like cheese and macaroni.\n I love to drink juice.\n \n \n I put the \n ch...


Python stdin readline over 4096 bytes?

This question already has answers here:


io - python readline by '\r\n'

How can I called f.readline() where the line delimeter is \r\n ? This CSV file is very large so I can't do f.read().split('\r\n'). Instead I'm hoping for f.readline('\r\n'). Here is a typical line: 1, "ABC", "the quick \n brown fox \n jumps over the \n lazy dogs", 5 \r\n


Why in python loop for line in file is not going through all lines after using readline before?

Why in python loop for line in file is not going through all lines after using readline before? How to achieve that after reading some line(s) for loop will go through all lines in file? file = open("file.xyz", "r") first_line = file.readline() for line in file: x,y,z = line.split()


Python binary search readline does not return whole line

There's lots of help on binary searching here, but can't find this specific problem. This is what I'm getting in IDLE and the same thing is happening in my code. First, I find the file size of the file to be searched (each line is two integers, separated by a tab and sorted by first integer): >>> import os >>> a=os.path.getsize("C:\\testfile.txt") >>> a 9341L ...


python - readline skips first line in for loop

I've got a problem with a for loop that skips the first line. I know why but I don't know how to fix it. And when I change it to a while loop, or in a matter of fact in to anything, it gives me an error: line 29, in <module> worksheet.write(row, col, float(b)) ValueError: could not convert string to float: The code is: import xlsxwriter ...


Python Serial readline doesn't work when called from within Object

So I'm writing a simple serial driver for an agilent power supply with pyserial. Everything works fine until I package it up as a class and try to run some simple serial write/read from within the object that I create. A code snippet of the class: class agilent: """ Class for controlling the chroma class power supplies Basically a wrapper around the serial interface to the chromas ...


How to improve speed of this readline loop in python?

i'm importing several parts of a Databasedump in text Format into MySQL, the problem is that before the interesting Data there is very much non-interesting stuff infront. I wrote this loop to get to the needed data: def readloop(DBFILE): txtdb=open(DBFILE, 'r') sline = "" # loop till 1st "customernum:" is found while sline.startswith("customernum: ") is False: sline = txtdb.readline() while sli...


python - Getting readline to block on a FIFO

I create a fifo: mkfifo tofetch I run this python code: fetchlistfile = file("tofetch", "r") while 1: nextfetch = fetchlistfile.readline() print nextfetch It stalls on readline, as I would hope. I run: echo "test" > tofetch And my program doesn't stall anymore. It reads the line, and then continues looping forev...


linux - Can't build readline when trying to install Python 2.6.5 in Debian 4.3.2

I am trying to install Python 2.6.5 on my web server running Debian 4.3.2.1-1. I unpacked the tarball, ran "./configure --prefix /usr/", then ran "make". I saw this message. Failed to find the necessary bits to build these modules: _bsddb _hashlib _ssl _tkinter bsddb185 bz2 dl imageop readline sunaudiodev ...


python - Ipython no readline available and pip install readline error

I installed ipython but it doesn't have the readline option. I first downloaded gnu readline and compiled and installed. DIdn't know whether it was a proper solution but was the first thing I thought of. It still wouldn't work to no avail with the same error as before: WARNING: Readline services not available on this platform. WARNING: The auto-indent feature requires the readline library Then I tried...


Problem in python using readline data into an array

so here my code arr = [] inp = open ("test.txt","r") for line in inp.readlines(): for i in line.split(): arr.append(i) print arr and the output for arr is [] and if i try to print arr[0] or anything it says the index is out of range im trying to take the data out of the text and put it into array for use or even a list i could read the data off of ive tried alot ways this seem...


macos - Python readline and UTF-8

I'm trying to use raw_input() with readline and utf-8 encoding (on OSX 10.6.8). Imagine the following snippet: import readline while True: try: inp = raw_input('> ') except EOFError: break This works except when trying to type non standard ASCII letters, say 'å' or 'ä'. When doing this no input is written, I guess readline is filtering this somehow....


python - How to install readline via buildout

I would like to configure my python project to automatically build postgresql in installation process. You can do it similar to what's described here: Buildout, psycopg2, postgresql The problem is I don't like using configure-options = --without-readline option That means I have t...


Python readline - reads only first line

#1 input_file = 'my-textfile.txt' current_file = open(input_file) print current_file.readline() print current_file.readline() #2 input_file = 'my-textfile.txt' print open(input_file).readline() print open(input_file).readline() Why is it that #1 works fine and displays the first and second line, but #2 prints 2 copies of the first line and doesn't print the same as #1 ?


python - How can I use readline method?

I have this trivial code: from sys import argv script, input_file = argv def fma(f): f.readline() current_file = open(input_file) fma(current_file) The contents of the txt file is: Hello this is a test.\n I like cheese and macaroni.\n I love to drink juice.\n \n \n I put the \n ch...


string from readline start from 1 in python?

i open a file line = file.readline() and use line[0:2] to select the first 3 chars in the line. But weired, the line[0:2] only contains 2 chars. Therefore, i must use line[0:3] to select the first 3 chars, and it works But why? I checked the file, there are no spaces at the beginning of each line Anybody know this






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top