<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def command_exists(command, noop_invocation, exc_msg):
 """ Verify that the provided command exists. Raise CommandDoesNotExistException in case of an error or if the command does not exist. :param command: str, command to check (python 3 only) :param noop_invocation: list of str, command to check (python 2 only) :param exc_msg: str, message of exception when command does not exist :return: bool, True if everything's all right (otherwise exception is thrown) """

    try:
        found = bool(shutil.which(command))  # py3 only
    except AttributeError:  # py2 branch
        try:
            p = subprocess.Popen(noop_invocation, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        except OSError:
            found = False
        else:
            stdout, stderr = p.communicate()
            found = p.returncode == 0
            if not found:
                logger.error("`%s` exited with a non-zero return code (%s)",
                             noop_invocation, p.returncode)
                logger.error("command stdout = %s", stdout)
                logger.error("command stderr = %s", stderr)
    if not found:
        raise CommandDoesNotExistException(exc_msg)
    return True