我正在尝试使用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()
我在 cmd 模块中收到此错误。
AttributeError: 'module' object has no attribute 'Cmd'
4号线。
我用
python3.2.3
尝试了你的脚本,它成功了。您在与该文件所在的同一目录中是否有名为 cmd.py
的文件?如果这样做,那么 import cmd
将无法导入正确的模块。相反,它会在当前目录中导入 cmd.py
。
我在目录中创建了一个名为
cmd.py
的文件,并在尝试运行脚本时遇到了与您相同的错误。因此,删除当前目录中的 cmd.py
或将其命名为其他名称(如果那里有)。
我认为存在一个名为 cmd.py 的文件。去掉后试试。有效