How to make a "command line basd" interactive program?

Matimus mccredie at
Fri Apr 11 23:08:31 CEST 2008

On Apr 11, 2:32 am, Evan <xdi... at> wrote:
> Hope this hasn't been posted hundreds of times. I'm new for this.
> Before using python for this kind of script, I was using TCL to write
> down a "command line based" interactive program.  it likes a "tclsh",
> or "python" command, after that, you can work under a prompt,  for
> example, " - >> ", and then you can execute any commands what you
> defined in script.
> Now, in python, are there any common way(class) to finish this work?
> or does anybody has a example to do that?
> Thanks,
> Evan

Do you want a custom shell that does whatever you want? Or do you want
an interactive python shell that has some custom commands?

For the first check out the cmd module


>>> import cmd
>>> class MyCmd(cmd.Cmd):
...  def do_echo(self, params):
...   print params
>>> MyCmd().cmdloop()
(Cmd) echo Hello World
Hello World
(Cmd) help

Undocumented commands:
echo  help

For the second, check out the code module


>>> import code
>>> def foo():
...  print "hello, this is foo"
>>> code.interact("Welcome to my python shell!", local={'bar':foo})
Welcome to my python shell!
>>> bar()
hello, this is foo

Hope this helps,


More information about the Python-list mailing list