Cmd 模块属性错误

use*_*822 3 python cmd attributeerror

我正在尝试使用 python 的 cmd 模块,并且在 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()
Run Code Online (Sandbox Code Playgroud)

我收到 cmd 模块的此错误。

AttributeError: 'module' object has no attribute 'Cmd'
Run Code Online (Sandbox Code Playgroud)

4号线。

Gni*_*ohz 8

我试过你的脚本python3.2.3,它有效。cmd.py您是否有一个与该文件位于同一目录中的文件?如果这样做,则import cmd不会导入正确的模块。相反,它将导入cmd.py到当前目录中。

cmd.py我在目录中创建了一个名为的文件,并在尝试运行脚本时遇到了与您相同的错误。因此,请删除cmd.py当前目录中的文件,或者将其命名为其他名称(如果那里有)。