r/learnpython•
mase268

CmdModule isn't working

I am trying to make a program with the CmdModule. I have used it before on an older version of python and it worked fine. My code:

import cmd
import time

class HelloWorld(cmd.Cmd):

    def do_hello(self, line):
        print("Hello user!")

    def do_setname(self, name):
        if name:
            print("Set name to: %s" % name)
            uname = name
        else:
            print("Please specify the name")

    def do_shutdown(self, line):
        raise SystemExit

if(__name__ == '__main__'):
    HelloWorld.cmdloop()

When I run it, it spits out a TypeError:

Traceback (most recent call last):
  File "C:\Users\Mason\Desktop\Orwell.py", line 19, in <module>
    HelloWorld.cmdloop()
TypeError: cmdloop() missing 1 required positional argument: 'self'

When I put self in the parenthesis it says that self is not defined:

Traceback (most recent call last):
  File "C:\Users\Mason\Desktop\Orwell.py", line 33, in <module>
    HelloWorld.cmdloop(self)
NameError: name 'self' is not defined

I've been googling it for a while and can't find anything. What do?

Btw: My OS is Windows 10 and my Python version is 3.7.3

Posted May 19, 2019, 12:30 AM UTC

52
Loading comments…