I'm trying to use cmd module of python, and I got this test code in a website When compiling this code in python 3.2
import cmd
import os
class ShellEnabled(cmd.Cmd):
last_output = ''
def do_shell(self, line):
"Run a shell command"
print ("running shell command:", line)
output = os.popen(line).read()
print (output)
self.last_output = output
def do_echo(self, line):
"Print the input, replacing '$out' with the output of the last shell command"
# Obviously not robust
print (line.replace('$out', self.last_output))
def do_EOF(self, line):
return True
if __name__ == '__main__':
ShellEnabled().cmdloop()
I get this error for cmd module.
AttributeError: 'module' object has no attribute 'Cmd'
On line 4.
-
Hi, I tried your code and found no error... By the way, where is the function cmdloop() that you are calling?lowitty– lowitty2014年03月24日 02:50:02 +00:00Commented Mar 24, 2014 at 2:50
-
@lowitty it is method of cmd.Cmd. on python 3.3 works fine.m.wasowski– m.wasowski2014年03月24日 02:53:03 +00:00Commented Mar 24, 2014 at 2:53
-
@m.wasowski Thanks! Actually I tried this script and get the output: (Cmd) and no error.lowitty– lowitty2014年03月24日 02:55:37 +00:00Commented Mar 24, 2014 at 2:55
-
So it will not work on python 3.2 ?user3447822– user34478222014年03月24日 03:09:03 +00:00Commented Mar 24, 2014 at 3:09
2 Answers 2
I tried you script with python3.2.3 and it worked. Do you have a file named cmd.py in the same directory as this file is in? If you do, then import cmd would not import the right module. Instead, it would import cmd.py in the current directory.
I created a file called cmd.py in the directory and got the same error as you did when trying to run your script. So delete cmd.py that is in the current directory or name it something else if you have it there.
Comments
I think there exists a file with name cmd.py. Try after removing it. It works