Question

I have a Python program that produces an error:

File "myTest.py", line 34, in run
   self.output = self.p.stdout
AttributeError: RunCmd instance has no attribute 'p'

The Python code:

class RunCmd():

    def __init__(self, cmd):
        self.cmd = cmd

    def run(self, timeout):
        def target():
            self.p = sp.Popen(self.cmd[0], self.cmd[1], stdin=sp.PIPE,
                              stdout=sp.PIPE, stderr=sp.STDOUT)

        thread = threading.Thread(target=target)
        thread.start()
        thread.join(timeout)

        if thread.is_alive():
            print "process timed out"
            self.p.stdin.write("process timed out")
            self.p.terminate()
            thread.join()

        self.output = self.p.stdout             #self.p.stdout.read()?
        self.status = self.p.returncode

    def getOutput(self):
        return self.output

    def getStatus(self):
        return self.status

Here's the entire back trace.

Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
    self.run()
File "/usr/lib/python2.7/threading.py", line 505, in run
    self.__target(*self.__args, **self.__kwargs)
File "myTest.py", line 18, in target
    self.p = sp.Popen(self.cmd, stdin=PIPE,
NameError: global name 'PIPE' is not defined

Traceback (most recent call last):
File "myTest.py", line 98, in <module>
    c = mydd.ddmin(deltas)              # Invoke DDMIN
File "/home/DD.py", line 713, in ddmin
    return self.ddgen(c, 1, 0)
File "/home/DD.py", line 605, in ddgen
    outcome = self._dd(c, n)
File "/home/DD.py", line 615, in _dd
    assert self.test([]) == self.PASS
File "/home/DD.py", line 311, in test
    outcome = self._test(c)
File "DD.py", line 59, in _test
    test.run(3)
File "DD.py", line 30, in run
    self.status = self.p.returncode
AttributeError: 'RunCmd' object has no attribute 'p'

What does this error mean and what is it trying to tell me?

Was it helpful?

Solution

You didn't give all the error messages. The code in the thread fails because your call to Popen is wrong, it should be:

def target():
    self.p = sp.Popen(self.cmd, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT)

As the thread fails, it doesn't set the "p" variable, that's why you're getting the error message you're talking about.

OTHER TIPS

How to reproduce this error in Python very simply:

class RunCmd():
    def __init__(self):
        print(self.p)

r = RunCmd()

Prints:

AttributeError: 'RunCmd' object has no attribute 'p'

What's going on:

You have to learn to read and reason about the code you are dealing with. Verbalize the code like this:

I define a class called RunCmd. It has a constructor called __init__ that takes no parameters. The constructor prints out the local member variable p.

I instantiate a new object (instance) of RunCmd class. The constructor is run, and it tries to access the value of p. No such attribute p exists, so the error message is printed.

The error message means exactly what it says. You need to create something before you can use it. If you don't, this AttributeError will be thrown.

Solutions:

  1. Throw an error earlier on when your variable is not created.
  2. Put the code in a try/catch to stop the program when it's not created.
  3. Test if the variable exists before using it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top