Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top