4

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.

m.wasowski
6,3961 gold badge26 silver badges30 bronze badges
asked Mar 24, 2014 at 2:45
4
  • Hi, I tried your code and found no error... By the way, where is the function cmdloop() that you are calling? Commented Mar 24, 2014 at 2:50
  • @lowitty it is method of cmd.Cmd. on python 3.3 works fine. Commented Mar 24, 2014 at 2:53
  • @m.wasowski Thanks! Actually I tried this script and get the output: (Cmd) and no error. Commented Mar 24, 2014 at 2:55
  • So it will not work on python 3.2 ? Commented Mar 24, 2014 at 3:09

2 Answers 2

9

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.

answered Mar 24, 2014 at 4:52
Sign up to request clarification or add additional context in comments.

Comments

0

I think there exists a file with name cmd.py. Try after removing it. It works

answered Mar 29, 2024 at 7:56

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Just a summary of the existing and accepted answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.