This is the implementation of cmd.Cmd (Python 3.9). As you can see in cmdloop(), the import is already wrapped, and the readline feature can be turned off explicitly by passing None to the completekey in the constructor.
def __init__(self, completekey='tab', stdin=None, stdout=None):
"""Instantiate a line-oriented interpreter framework.
The optional argument 'completekey' is the readline name of a
completion key; it defaults to the Tab key. If completekey is
not None and the readline module is available, command completion
is done automatically. The optional arguments stdin and stdout
specify alternate input and output file objects; if not specified,
sys.stdin and sys.stdout are used.
if stdin is not None:
self.stdin = stdin
self.stdin = sys.stdin
if stdout is not None:
self.stdout = stdout
self.stdout = sys.stdout
self.cmdqueue = 
self.completekey = completekey
def cmdloop(self, intro=None):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
if self.use_rawinput and self.completekey:
self.old_completer = readline.get_completer()
From: Python-list <firstname.lastname@example.org> on behalf of Cameron Simpson <email@example.com>
Date: Friday, March 10, 2023 at 5:15 PM
To: firstname.lastname@example.org <email@example.com>
Subject: Re: Baffled by readline module
*** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***
On 10Mar2023 09:12, Grant Edwards <firstname.lastname@example.org> wrote: >On 2023-03-10, Weatherby,Gerard <email@example.com> wrote:
>> On our Linux systems, I can up-arrow to go back to prior commands
>> and use the left and right arrows to navigate a line. The
>> functionality may be provided internally by readline. I?ve never had
>> to dig into it because it meets my needs out of the box.
>Apparently the cmd.Cmd docs are wrong. It says:
> If the readline module is loaded, input will automatically
> inherit bash-like history-list editing (e.g. Control-P scrolls
> back to the last command, Control-N forward to the next one,
> Control-F moves the cursor to the right non-destructively,
> Control-B moves the cursor to the left non-destructively, etc.).
>On my Python 3.10.10 Linux system, cmd.Com itself is importing the
>readline module unconditionally when I call cmdloop(). There's no 'if'
I was wondering about that myself, whether this is an accident of
phrasing. It doesn't say "is imported", so maybe the author was thinking
"if readline's part of the install" here.
Anyway, I've got a try/import-readline/except-importerror/pass in my
cmd.Cmd wrapper, because I read this the way you read it.
Cameron Simpson <firstname.lastname@example.org>