Mailing List Archive

Baffled by readline module
In an interactive command-line Python program on Linux, I want to be
able to read a line at a time from stdin, providing command line
history and editing to the user. In C, I would use GNU readline to do
that.

Python has the readline module, which appears to be a wrapper for GNU
readline. However, I've read and re-read the documenation for that
module, but I'm completely baffled. There's all sorts of stuff about
history files, but I neither need nor want a history file. Likewise
tab-completion: don't want it. All the examples not only involve
history files and tab-completion but they're somehow connected to the
interactive Python REPL, which is also completely off-base for my use
case.

Is the readline module not the right tool for an interactive Linux
command-line application that needs to provide command line recall and
editing to the user who's entering stuff on stdin from a tty?

--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-09, Grant Edwards <grant.b.edwards@gmail.com> wrote:

> In an interactive command-line Python program on Linux, I want to be
> able to read a line at a time from stdin, providing command line
> history and editing to the user. In C, I would use GNU readline to do
> that.
>
> Python has the readline module, which appears to be a wrapper for GNU
> readline. However, I've read and re-read the documenation for that
> module, but I'm completely baffled.

After googling for a while, I finally stumbled across an old Python 2
example that pointed me in the right direction. Here's the sort of
example I had hoped to find in the module's docs:

#!/usr/bin/python
import readline

readline.parse_and_bind('set editing-mode emacs')

while True:
try:
line = input('enter something (EOF to quit): ')
except EOFError:
print()
break
print('You entered: "%s"' % line)




--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On Fri, 10 Mar 2023 at 06:28, Grant Edwards <grant.b.edwards@gmail.com> wrote:
>
> In an interactive command-line Python program on Linux, I want to be
> able to read a line at a time from stdin, providing command line
> history and editing to the user. In C, I would use GNU readline to do
> that.
>
> Python has the readline module, which appears to be a wrapper for GNU
> readline. However, I've read and re-read the documenation for that
> module, but I'm completely baffled. There's all sorts of stuff about
> history files, but I neither need nor want a history file. Likewise
> tab-completion: don't want it. All the examples not only involve
> history files and tab-completion but they're somehow connected to the
> interactive Python REPL, which is also completely off-base for my use
> case.
>
> Is the readline module not the right tool for an interactive Linux
> command-line application that needs to provide command line recall and
> editing to the user who's entering stuff on stdin from a tty?
>

Not sure about the history file, and I would assume that if you don't
configure one, history is simply lost when you restart. But with tab
completion, unless you need to be able to input a tab character, it
should be safe to ignore the feature and leave it at the defaults.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-09, Chris Angelico <rosuav@gmail.com> wrote:

> Not sure about the history file, and I would assume that if you don't
> configure one, history is simply lost when you restart. But with tab
> completion, unless you need to be able to input a tab character, it
> should be safe to ignore the feature and leave it at the defaults.

Indeed, that seems to be how it works (though I never found that
stated anywhere in the docs).

What's really weird about the docs is that when it is described it
doesn't even _mention_ that it provides command-line recall and
editing:

The readline module defines a number of functions to facilitate
completion and reading/writing of history files from the Python
interpreter. This module can be used directly, or via the
rlcompleter module, which supports completion of Python
identifiers at the interactive prompt. Settings made using this
module affect the behaviour of both the interpreter’s interactive
prompt and the prompts offered by the built-in input() function.

It just talks about manipulating history files and about
tab-completion of Python identfiers. The last sentence mentions that
settings affect both the REPL prompt and the prompts offered by the
built-in input() function.

However, I also don't really care about the "prompts offered"
either. What I care about is the interactive handling of user
keystrokes vis-a-vis command line recall and editing.

Or is that what's meant by the phrase "behavior of the prompt"?

To me "the prompt" is the string that's printed _before_ the program
starts reading user keystrokes and doing the stuff I care about.

It finally dawned on me after seeing an example I found elsewhere that
you don't call some module method to fetch the next user-entered line.

You call the input() built-in.

Having a module modify the behavior of a built-in makes me cringe.

I suppose this way you can easily slap-on "readline" command-line
recall/editing to an existing application as long as it uses the
input() built-in instead of reading from stdin.

--
Grant


--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On Fri, 10 Mar 2023 at 08:10, Grant Edwards <grant.b.edwards@gmail.com> wrote:
> What's really weird about the docs is that when it is described it
> doesn't even _mention_ that it provides command-line recall and
> editing:
>
> It just talks about manipulating history files and about
> tab-completion of Python identfiers. The last sentence mentions that
> settings affect both the REPL prompt and the prompts offered by the
> built-in input() function.
>

Ah. Yes, that's because the **module** doesn't provide command-line
recall and editing. It does affect the input() builtin, as is
mentioned, but mainly, the module is to configure readline, not to ask
for a line of text.

It's a bit confusing, but very useful.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 09Mar2023 13:08, Grant Edwards <grant.b.edwards@gmail.com> wrote:
>On 2023-03-09, Chris Angelico <rosuav@gmail.com> wrote:
>> Not sure about the history file, and I would assume that if you don't
>> configure one, history is simply lost when you restart. But with tab
>> completion, unless you need to be able to input a tab character, it
>> should be safe to ignore the feature and leave it at the defaults.
>
>Indeed, that seems to be how it works (though I never found that
>stated anywhere in the docs).
>
>What's really weird about the docs is that when it is described it
>doesn't even _mention_ that it provides command-line recall and
>editing:
[...]

I think this might be the common case of a module which wraps another
library: there's a tension between describing everything in pointless
detail and the trite "we're just shimming this library, go read its
docs".

The module documentation needs to:
- not insult the reader by saying nearly nothing ("this is just GNU
readline, hooked to Python!") I'm looking at you, many "man" pages on
a GNU based system which say "oh just go and read GNI info, it's all
over there"
- be detailed enough to enumerate the API calls and at least summarise
their semantics
- be general enough to not overprescribe so that small shifts in the
library as it evolves don't cause falsehoods in the module docs (and a
nightmarish maintenance burden)

Similar example: the "os" modules, which largely shims the OS POSIX
calls. It lists them and has a paragraph on their purpose and the
meaning of the flags (for example). But the platform specifics and fine
grained stuff is off in "man 2 foo" for the Python "os.foo" function.

[...]
>It finally dawned on me after seeing an example I found elsewhere that
>you don't call some module method to fetch the next user-entered line.
>
>You call the input() built-in.

Ah. That's not overtly stated? [...reads...] Ah, there it is in the last
sentence of the opening paragraph. Not quite as in-your-face as I'd have
liked it. That paragraph could do with being a bullet list of use cases.

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On Fri, 10 Mar 2023 at 09:01, Cameron Simpson <cs@cskk.id.au> wrote:
> I think this might be the common case of a module which wraps another
> library: there's a tension between describing everything in pointless
> detail and the trite "we're just shimming this library, go read its
> docs".

Yeah, it's a combination of this...

> >You call the input() built-in.
>
> Ah. That's not overtly stated? [...reads...] Ah, there it is in the last
> sentence of the opening paragraph. Not quite as in-your-face as I'd have
> liked it. That paragraph could do with being a bullet list of use cases.

... and this. With a lot of "just wrap that library" functions, it can
be exactly as you say: a bit of documentation about each function, and
you can skim those to see what's available. But with readline, there
isn't a "give me a command from the user" function, because that's
input().

Maybe there should be an example added at the end?

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-09, Cameron Simpson <cs@cskk.id.au> wrote:

> [...]
>>It finally dawned on me after seeing an example I found elsewhere that
>>you don't call some module method to fetch the next user-entered line.
>>
>>You call the input() built-in.
>
> Ah. That's not overtly stated? [...reads...] Ah, there it is in the last
> sentence of the opening paragraph. Not quite as in-your-face as I'd have
> liked it.

What threw me off the track for a while was that the sentence to which
you refer says it affects the "prompts offered by input()". In my head,
that means it changes the string that's printed on stdout before stuff
is read from stdin. That's different that affecting the handling of
user input read by input().

It doesn't actually change anything about the prompts provided by
input(). It changes the handling of the user input by input().

I guess I read it too literally. I must spend too much time with
computers.

> That paragraph could do with being a bullet list of use cases.



--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On Fri, 10 Mar 2023 at 09:20, Grant Edwards <grant.b.edwards@gmail.com> wrote:
>
> On 2023-03-09, Cameron Simpson <cs@cskk.id.au> wrote:
>
> > [...]
> >>It finally dawned on me after seeing an example I found elsewhere that
> >>you don't call some module method to fetch the next user-entered line.
> >>
> >>You call the input() built-in.
> >
> > Ah. That's not overtly stated? [...reads...] Ah, there it is in the last
> > sentence of the opening paragraph. Not quite as in-your-face as I'd have
> > liked it.
>
> What threw me off the track for a while was that the sentence to which
> you refer says it affects the "prompts offered by input()". In my head,
> that means it changes the string that's printed on stdout before stuff
> is read from stdin. That's different that affecting the handling of
> user input read by input().
>
> It doesn't actually change anything about the prompts provided by
> input(). It changes the handling of the user input by input().
>
> I guess I read it too literally. I must spend too much time with
> computers.

It does actually affect the prompts, although only in subtle ways.

import readline
print("Pseudo-prompt: ", end="")
msg1 = input()
msg2 = input("Actual prompt: ")
print(repr(msg1))
print(repr(msg2))

At each of the prompts, type a bit of text, then backspace it all the
way. The actual prompt will remain, but the pseudo-prompt will get
cleared off. There'll be other small differences too.

Maybe that could be mentioned somewhere too; but this is definitely
something to be more careful with locking in, since the behaviour may
not be quite the same if it's using libedit instead of GNU readline.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-09, Grant Edwards <grant.b.edwards@gmail.com> wrote:
> On 2023-03-09, Cameron Simpson <cs@cskk.id.au> wrote:
>
>> [...]
>>>It finally dawned on me after seeing an example I found elsewhere that
>>>you don't call some module method to fetch the next user-entered line.
>>>
>>>You call the input() built-in.
>>
>> Ah. That's not overtly stated? [...reads...] Ah, there it is in the last
>> sentence of the opening paragraph. Not quite as in-your-face as I'd have
>> liked it.
>
> What threw me off the track for a while was that the sentence to which
> you refer says it affects the "prompts offered by input()". In my head,
> that means it changes the string that's printed on stdout before stuff
> is read from stdin. That's different that affecting the handling of
> user input read by input().
>
> It doesn't actually change anything about the prompts provided by
> input(). It changes the handling of the user input by input().
>
> I guess I read it too literally. I must spend too much time with
> computers.

Yeesh. What's _really_ embarassing is that I just stumbled across a
small test program with which I had apparently figured this out 10-12
years ago. Must be about time to retire...

--
Grant
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On Fri, 10 Mar 2023 at 10:04, Grant Edwards <grant.b.edwards@gmail.com> wrote:
>
> Yeesh. What's _really_ embarassing is that I just stumbled across a
> small test program with which I had apparently figured this out 10-12
> years ago. Must be about time to retire...
>

You expect yourself to remember test programs you wrote a decade ago??
I've forgotten full-on projects from that far back!

Though, congrats on being able to stumble across it. That's quite something.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 09Mar2023 13:08, Grant Edwards <grant.b.edwards@gmail.com> wrote:
>Having a module modify the behavior of a built-in makes me cringe.

Maybe. But input(), like print(), is one of those funky conveniences for
human interaction. I'm somewhat ok with that. If for no other reason
than to make keyboard arrow keys act as normal humans expect them to (as
opposed to old nerds who predate such fancies). General improvement of
the user experience.

Another thing affected by readline is the cmd module, which will use it
during the input step for completion _if the module is present_. And
use it for the input itself if it is loaded. (These might not mean
different things and instead be an accident of phrasing.) Again, a human
interaction convenience and I'm good with it.

Good enough to have written this code recently:

try:
import readline
except ImportError:
pass

just to turn it on if available.

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-09, Chris Angelico <rosuav@gmail.com> wrote:
> On Fri, 10 Mar 2023 at 10:04, Grant Edwards <grant.b.edwards@gmail.com> wrote:
>
>> Yeesh. What's _really_ embarassing is that I just stumbled across a
>> small test program with which I had apparently figured this out
>> 10-12 years ago. Must be about time to retire...
>
> You expect yourself to remember test programs you wrote a decade
> ago?? I've forgotten full-on projects from that far back!

Another thing that has happened a few times is that I'm trying to
figure out how to do something I'm pretty sure should be possible, but
I can't figure out how. After the usual resources fail, then I start
Googling with varous sets of keywords. After a while, Google eventually
finds the answer in an old mailing-list archive where there's a nice
explanation in a post...

.... by me.


> Though, congrats on being able to stumble across it. That's quite
> something.

After I figured out the answer, I realized it did seem a little
familar, so I tried a grep -r in my home directory which has stuff
lying around from as far back as 2001. When a computer dies, I
generally just cp -a (or rsync -a) $HOME to a new one.

--
Grant
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-09 at 15:02:53 -0800,
Grant Edwards <grant.b.edwards@gmail.com> wrote:

> Yeesh. What's _really_ embarassing is that I just stumbled across a
> small test program with which I had apparently figured this out 10-12
> years ago. Must be about time to retire...

Retiring doesn't help. :-)

I retired almost five years ago, and I just (within the past few days)
(re)discovered a command line parsing library I'd written about a year
and a half ago (i.e., after I retired).

Dan
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On Fri, 10 Mar 2023 at 10:51, <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
>
> On 2023-03-09 at 15:02:53 -0800,
> Grant Edwards <grant.b.edwards@gmail.com> wrote:
>
> > Yeesh. What's _really_ embarassing is that I just stumbled across a
> > small test program with which I had apparently figured this out 10-12
> > years ago. Must be about time to retire...
>
> Retiring doesn't help. :-)
>
> I retired almost five years ago, and I just (within the past few days)
> (re)discovered a command line parsing library I'd written about a year
> and a half ago (i.e., after I retired).
>

Traditional retirement: Work till you're 60 or 65 or whatever, then
society pats you on the head, calls you a "senior citizen", and lets
you go and be idle till you die (which might be prematurely soon).

Direction-change retirement: Work till you can afford to zero out your
income, then finally do what you've always wanted to do, but never had
time because you spent so much of it earning money.

Tell-the-next-generation: Work till you know so much that you're
infinitely valuable, then spend the rest of your life empowering the
next group of amazing people. See for instance: NASA.

Programmer retirement: At an early age, learn how to wield PHENOMENAL
COSMIC POWER, and spend the next X years in an itty bitty working
space, earning money. Eventually, upgrade to better living/working
space. Eventually, downgrade to a small wooden box six feet below the
ground. Never once relinquish the power. Never once abandon that
feeling of mastery.

We're not really an industry that has a concept of retirement.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 10/03/23 10:08 am, Grant Edwards wrote:
> It finally dawned on me after seeing an example I found elsewhere that
> you don't call some module method to fetch the next user-entered line.
>
> You call the input() built-in.
>
> Having a module modify the behavior of a built-in makes me cringe.

Importing the module is not modifying the built-in.

If your Python has been compiled with gnu readline support,
input() *already* provides recall and editing facilities.

You only need to import the readline module if you want to
change the configuration.

Yes, it would be helpful if the docs for the readline module
explained this. At present they seem to assume that you already
know what the readline module is for and just want a summary
of the API.

It *is* mentioned briefly in the docs for input(), but again
somebody wanting line editing functionality wouldn't necessarily
think of looking there.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 10/03/23 10:59 am, Cameron Simpson wrote:
> I think this might be the common case of a module which wraps another
> library

It's not quite the same thing, though -- the library it wraps
is already hooked into things behind the scenes in ways that
may not be obvious. (Unless you're Dutch?)

--
Greg

--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 10/03/23 11:43 am, Chris Angelico wrote:
> import readline
> print("Pseudo-prompt: ", end="")
> msg1 = input()
> msg2 = input("Actual prompt: ")
> print(repr(msg1))
> print(repr(msg2))
>
> At each of the prompts, type a bit of text, then backspace it all the
> way. The actual prompt will remain, but the pseudo-prompt will get
> cleared off. There'll be other small differences too.

Hmmm, so it seems that merely importing readline does change
things a little bit.

This is rather nasty. I'd go so far as to call it a bug in
gnu readline -- it shouldn't be erasing something that the
user didn't type in.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-10 at 11:02:52 +1100,
Chris Angelico <rosuav@gmail.com> wrote:

> Traditional retirement: Work till you're 60 or 65 or whatever, then
> society pats you on the head, calls you a "senior citizen", and lets
> you go and be idle till you die (which might be prematurely soon).

Sounds like Free Four?:

The memories of a man in his old age
Are the deeds of a man in his prime.

You shuffle in the gloom of the sickroom
And talk to yourself as you die.

Great tune. Bad life.

> Direction-change retirement: Work till you can afford to zero out your
> income, then finally do what you've always wanted to do, but never had
> time because you spent so much of it earning money.

A little bit of that. We live in the RV, and we have crisscrossed the
country more than once, coronavirus notwithstanding.

> Tell-the-next-generation: Work till you know so much that you're
> infinitely valuable, then spend the rest of your life empowering the
> next group of amazing people. See for instance: NASA.

And a little bit of that, too. NASA would have been nice; I did my most
of my time in the commercial space, with a short break in the government
contracting business.

> Programmer retirement: At an early age, learn how to wield PHENOMENAL
> COSMIC POWER, and spend the next X years in an itty bitty working
> space, earning money. Eventually, upgrade to better living/working
> space. Eventually, downgrade to a small wooden box six feet below the
> ground. Never once relinquish the power. Never once abandon that
> feeling of mastery.

I was with you until that part of the small wooden box. :-)

> We're not really an industry that has a concept of retirement.

Which is why I'm still here (on this mailing list, and a handful of
others like it).

Thanks for asking.

? https://www.azlyrics.com/lyrics/pinkfloyd/freefour.html
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 10/03/23 12:43 pm, Grant Edwards wrote:
> When a computer dies, I
> generally just cp -a (or rsync -a) $HOME to a new one.

Same here, more or less. My current machine has multiple
archaeological layers going back about 5 generations of
technology...

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-10, Greg Ewing via Python-list <python-list@python.org> wrote:
> On 10/03/23 10:08 am, Grant Edwards wrote:
>> It finally dawned on me after seeing an example I found elsewhere that
>> you don't call some module method to fetch the next user-entered line.
>>
>> You call the input() built-in.
>>
>> Having a module modify the behavior of a built-in makes me cringe.
>
> Importing the module is not modifying the built-in.
>
> If your Python has been compiled with gnu readline support,
> input() *already* provides recall and editing facilities.

That's not how Python 3.10.10 works for me. When I run the code below,
I do not get command recall and editing. If I hit arrow keys, I just
see the escape sequence echoed and returned by input(). Likewise for
things like ctrl-P, ctrl-N, etc. I have to uncomment the import
statement to get command line recall and editing to work. Without the
import, the escape sequences from arrow keys just end up in the input
data.

#!/usr/bin/python

# import readline
# readline.parse_and_bind('set editing-mode emacs')

while True:
try:
line = input('enter something (EOF to quit): ')
except EOFError:
print()
break
print('ENTERED: "%s"' % line)


> You only need to import the readline module if you want to
> change the configuration.

That's not how it acts for me. I have to "import readline" to get
command line recall and editing. The parse_and_bind, doesn't seem to
do anything -- emacs mode seems to be the default.

> Yes, it would be helpful if the docs for the readline module
> explained this. At present they seem to assume that you already
> know what the readline module is for and just want a summary
> of the API.
>
> It *is* mentioned briefly in the docs for input(), but again
> somebody wanting line editing functionality wouldn't necessarily
> think of looking there.



--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 10/03/23 1:46 pm, Grant Edwards wrote:
> That's not how it acts for me. I have to "import readline" to get
> command line recall and editing.

Maybe this has changed? Or is platform dependent?

With Python 3.8 on MacOSX I can use up arrow with input()
to recall stuff I've typed before, without having to
import anything.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On Fri, 10 Mar 2023 at 12:56, Greg Ewing via Python-list
<python-list@python.org> wrote:
>
> On 10/03/23 1:46 pm, Grant Edwards wrote:
> > That's not how it acts for me. I have to "import readline" to get
> > command line recall and editing.
>
> Maybe this has changed? Or is platform dependent?
>
> With Python 3.8 on MacOSX I can use up arrow with input()
> to recall stuff I've typed before, without having to
> import anything.
>

import sys; "readline" in sys.modules

Is it? Might be that something's pre-importing it.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-10 at 12:57:48 +1100,
Chris Angelico <rosuav@gmail.com> wrote:

> On Fri, 10 Mar 2023 at 12:56, Greg Ewing via Python-list
> <python-list@python.org> wrote:
> >
> > On 10/03/23 1:46 pm, Grant Edwards wrote:
> > > That's not how it acts for me. I have to "import readline" to get
> > > command line recall and editing.
> >
> > Maybe this has changed? Or is platform dependent?
> >
> > With Python 3.8 on MacOSX I can use up arrow with input()
> > to recall stuff I've typed before, without having to
> > import anything.
> >
>
> import sys; "readline" in sys.modules
>
> Is it? Might be that something's pre-importing it.

My ~/.pythonrc contains the following:

import readline
import rlcompleter
readline.parse_and_bind( 'tab: complete' )

IIRC, that's been there "forever," certainly back into Python2, and
probably back into Python1. On my Arch Linux system Python 3.10.9, I
get readline behavior with or without those lines.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Baffled by readline module [ In reply to ]
On 2023-03-10, 2QdxY4RzWzUUiLuE@potatochowder.com <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
> On 2023-03-10 at 12:57:48 +1100,
> Chris Angelico <rosuav@gmail.com> wrote:
>
>> On Fri, 10 Mar 2023 at 12:56, Greg Ewing via Python-list
>> <python-list@python.org> wrote:
>> >
>> > On 10/03/23 1:46 pm, Grant Edwards wrote:
>> > > That's not how it acts for me. I have to "import readline" to get
>> > > command line recall and editing.
>> >
>> > Maybe this has changed? Or is platform dependent?
>> >
>> > With Python 3.8 on MacOSX I can use up arrow with input()
>> > to recall stuff I've typed before, without having to
>> > import anything.

If you run this application from the command line, you get command
recall and editing when entering strings at the "cmd:" prompt?

#!/usr/bin/python
while True:
try:
line = input('cmd: ')
except EOFError:
print()
break
print('You entered "%s"' % line)

>> import sys; "readline" in sys.modules
>>
>> Is it? Might be that something's pre-importing it.
>
> My ~/.pythonrc contains the following:
>
> import readline
> import rlcompleter
> readline.parse_and_bind( 'tab: complete' )
>
> IIRC, that's been there "forever," certainly back into Python2, and
> probably back into Python1. On my Arch Linux system Python 3.10.9, I
> get readline behavior with or without those lines.

I "get readline behavior" in the REPL without an "import readline",
but that's irrelevent. We're talking about a command-line
application that's calling input().

--
https://mail.python.org/mailman/listinfo/python-list

1 2  View All