Sam CD
Sam CD

Reputation: 2097

AttributeError: 'Namespace' object has no attribute

I am writing a program that uses urllib2 to download CSV data from an http site. The program works fine when run within Python, however I am also trying to use argparse to be able to enter the url from the command line.

I get the following error when I run it:

File "urlcsv.py", line 51, in downloadData
    return urllib2.urlopen(url)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 396, in open
    protocol = req.get_type()
AttributeError: 'Namespace' object has no attribute 'get_type'

I guess this is part of the urllib2 library because it is not code that I have written. Has anybody else encountered similar problems with either the argparse or urllib2 modules?

The relevant part of the code is as follows:

parser = argparse.ArgumentParser()
parser.add_argument("url")


def main():
    """Runs when the program is opened"""

    args = parser.parse_args()
    if args is False:
        SystemExit
    try:
        csvData = downloadData(args)
    except urllib2.URLError:
        print 'Please try a different URL'
        raise
    else:
        LOG_FILENAME = 'errors.log'
        logging.basicConfig(filename=LOG_FILENAME,
                            level=logging.DEBUG,
                            )
        logging.getLogger('assignment2')
        personData = processData(csvData)
        ID = int(raw_input("Enter a user ID: "))
        if ID <= 0:
            raise Exception('Program exited, value <= 0')
        else:
            displayPerson(ID)
            main()

def downloadData(url):

    return urllib2.urlopen(url)

Upvotes: 37

Views: 189713

Answers (4)

user2138149
user2138149

Reputation: 16823

May be helpful for others in future.

If you are using metavar or dest make sure you understand what these arguments are for.

In short:

  • meta_var is something to do with the help message which is printed
  • dest is the name of the attribute which will be set (name of attribute which is created to store your command line argument when parsed)

Upvotes: 1

jjjshade
jjjshade

Reputation: 126

I had this problem due to a whitespace before the option sting.

Upvotes: 1

Andrii Abramov
Andrii Abramov

Reputation: 10751

Long story short.

Arguments in object returned from parser.parse_args() should be accessed via properties rather than via [] syntax.

Wrong

args = parser.parse_args()
args['method']

Correct

args = parser.parse_args()
args.method

Upvotes: 12

davidism
davidism

Reputation: 127200

You're parsing command line arguments into args, which is a Namespace with attributes set to the parsed arguments. But you're passing this entire namespace to downloadData, rather than just the url. This namespace is then passed to urlopen, which doesn't know what to do with it. Instead, call downloadData(args.url).

Upvotes: 27

Related Questions